c# - 如何从 System.IO.File 调用访问 PSDrive?

标签 c# powershell new-psdrive

我正在编写一个 c# cmdlet,它将文件从一个位置复制到另一个位置(类似于 rsync)。它甚至支持 ToSession 和 FromSession。

我希望它与使用文件系统提供程序的 PSDrives 一起工作,但它目前从 System.IO.File.GetAttributes("psdrive:\path") 抛出错误

我真的很想在 PSDrive 上使用来自 System.IO 的调用。

像 copy-item 这样的东西是如何做到这一点的?

我已经执行了从 C# 访问 PSDrives 的搜索,但没有返回任何结果。

这相当于我的代码

new-psdrive -name mydrive -psprovider filesystem  -root \\aserver\ashare -Credential domain\user
$attributes=[system.io.file]::GetAttributes("mydrive:\path\")

返回

Exception calling "GetAttributes" with "1" argument(s): "The given path's format is not supported."

最佳答案

.NET 对 PowerShell 驱动器一无所知(通常也有不同的工作目录),因此转换为 本地文件系统路径是必需的:

在 PowerShell 代码中:

使用Convert-Path将基于 PowerShell 驱动器的路径转换为 ​​.NET 类型理解的 native 文件系统路径:

$attributes=[System.IO.File]::GetAttributes((Convert-Path "mydrive:\path\"))

默认情况下(使用位置参数)和 -PathConvert-Path 执行通配符解析;要抑制后者,请使用 -LiteralPath 参数。

警告:Convert-Path 仅适用于现有 路径。取消该限制是 GitHub issue #2993 中功能请求的主题.


在 C# 代码中:

PSCmdlet -派生的 cmdlet:

  • 使用 GetUnresolvedProviderPathFromPSPath()将基于 PS 驱动器的路径转换为基于 native 驱动器的路径[1] 未解决,这意味着,除了转换驱动器部分:

    • 未验证路径是否存在(但驱动器名称必须存在)
    • 并且执行通配符解析。
  • 使用 GetResolvedProviderPathFromPSPath() 将基于 PS 驱动器的路径解析为基于 native 驱动器的路径,这意味着除了翻译驱动器部分之外:

    • 执行通配符解析,可能产生多个路径,甚至没有。
    • 文字路径组件必须存在
  • 使用 CurrentProviderLocation()使用提供程序 ID “FileSystem” 获取当前文件系统位置路径作为 System.Management.Automation.PathInfo 实例的方法;该实例的 .Path 属性和 .ToString() 方法返回路径的 PS 形式;使用 .ProviderPath 属性获取 native 表示。

这是一个简单的临时编译的 cmdlet,它可以使用这两种方法:

# Compiles a Get-NativePath cmdlet and adds it to the session.
Add-Type @'
    using System;
    using System.Management.Automation;
    [Cmdlet("Get", "NativePath")]
    public class GetNativePathCommand : PSCmdlet {

        [Parameter(Mandatory=true,Position=0)]
        public string PSPath { get; set; }

        protected override void ProcessRecord() {
            WriteObject("Current directory:");
            WriteObject("  PS form:      " + CurrentProviderLocation("FileSystem"));
            WriteObject("  Native form:  " + CurrentProviderLocation("FileSystem").ProviderPath);
            //
            WriteObject("Path argument in native form:");
            WriteObject("  Unresolved:");
            WriteObject("    " + GetUnresolvedProviderPathFromPSPath(PSPath));
            //
            WriteObject("  Resolved:");
            ProviderInfo pi;
            foreach (var p in GetResolvedProviderPathFromPSPath(PSPath, out pi))
            {
              WriteObject("    " + p);
            }
        }
    }
'@ -PassThru | % Assembly | Import-Module

您可以按如下方式进行测试:

# Create a foo: drive whose root is the current directory.
$null = New-PSDrive foo filesystem .

# Change to foo:
Push-Location foo:\

# Pass a wildcard path based on the new drive to the cmdlet
# and have it translated to a native path, both unresolved and resolved;
# also print the current directory, both in PS form and in native form.
Get-NativePath foo:\*.txt

如果您当前的目录是 C:\Temp 并且它恰好包含文本文件 a.txtb.txt,那么您将看到以下输出:

Current directory:
  PS form:      foo:\
  Native form:  C:\Temp\
Path argument in native form:
  Unresolved:
    C:\Temp\*.txt
  Resolved:
    C:\Temp\a.txt
    C:\Temp\b.txt

[1] 如果在输入路径中引用的 PS 驱动器(使用 New-PSDrive 创建)是根据 UNC 路径定义的,则结果 native 路径也将是 UNC 路径。

关于c# - 如何从 System.IO.File 调用访问 PSDrive?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58721850/

相关文章:

c# - IIS 高且不稳定的 TTFB

c# - 如何即时将 lambda 表达式转换为 Sql 查询?

c# - Excel 工作表不显示导出的图表图像 - ASP.NET

c# - 在 Windows 服务程序中记录事件

powershell - 使用 PowerShell 获取文件的证书链?

powershell - 将变量传递给 Foreach-Object PowerShell 7

powershell - Asp.net 5.0 MVC6 EF6 迁移脚本

powershell - $<drivename> : (such as `$code:` ) in Powershell? 是什么类型的对象