4

我正在编写 ac# 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."
4

1 回答 1

4

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

在 PowerShell 代码中

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

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

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

警告Convert-Path仅适用于现有路径。解除该限制是GitHub 上此功能请求的主题。


在 C# 代码中:

PSCmdlet- 派生的 cmdlet 中:

  • 用于GetUnresolvedProviderPathFromPSPath()将基于 PS 驱动器的路径转换为基于本机驱动器的路径[1] unresolved,这意味着,除了翻译驱动器部分:

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

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

这是一个简单的临时编译的 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

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

于 2019-11-06T05:49:06.113 回答