这是一个令人惊讶的复杂领域,但我在这里有很多经验。简而言之,有一些 cmdlet 直接从 System.IO API 接受 win32 路径,这些通常使用 -FilePath 参数。如果您想编写一个表现良好的“powershelly”cmdlet,您需要 -Path 和 -LiteralPath,以接受管道输入并使用相对和绝对提供程序路径。这是我前段时间写的一篇博客文章的摘录:
[起初] PowerShell 中的路径很难理解。PowerShell 路径 - 或PSPaths,不要与 Win32 路径混淆 - 在它们的绝对形式中,它们有两种不同的风格:
- 提供商资格:
FileSystem::c:\temp\foo.txt
- PSDrive 认证:
c:\temp\foo.txt
很容易混淆 provider-internal(ProviderPath
已解析的属性System.Management.Automation.PathInfo
- 上面提供者限定路径右侧的部分::
)和驱动器限定路径,因为如果您查看默认的 FileSystem 提供程序驱动器,它们看起来相同. 也就是说,PSDrive 与本机后备存储 Windows 文件系统 (C) 具有相同的名称 (C)。因此,为了让自己更容易理解这些差异,请为自己创建一个新的 PSDrive:
ps c:\> new-psdrive temp filesystem c:\temp\
ps c:\> cd temp:
ps temp:\>
现在,让我们再看一遍:
- 提供商资格:
FileSystem::c:\temp\foo.txt
- 驱动合格:
temp:\foo.txt
这次更容易看到这次有什么不同。提供程序名称右侧的粗体文本是 ProviderPath。
因此,您编写接受路径的通用提供程序友好 Cmdlet(或高级函数)的目标是:
- 定义
LiteralPath
别名为的路径参数PSPath
- 定义一个
Path
参数(将解析通配符/glob)
- 始终假设您正在接收 PSPath,而不是本机提供程序路径(例如 Win32 路径)
第三点尤其重要。此外,显然LiteralPath
并且Path
应该属于互斥的参数集。
相对路径
一个很好的问题是:我们如何处理传递给 Cmdlet 的相对路径。正如您应该假设给您的所有路径都是 PSPaths,让我们看看下面的 Cmdlet 的作用:
ps temp:\> write-zip -literalpath foo.txt
该命令应假定 foo.txt 在当前驱动器中,因此应立即在 ProcessRecord 或 EndProcessing 块中解决此问题,例如(使用此处的脚本 API 进行演示):
$provider = $null;
$drive = $null
$pathHelper = $ExecutionContext.SessionState.Path
$providerPath = $pathHelper.GetUnresolvedProviderPathFromPSPath(
"foo.txt", [ref]$provider, [ref]$drive)
现在,您已经有了重新创建 PSPath 的两种绝对形式所需的一切,并且您还拥有了本机绝对 ProviderPath。要为 foo.txt 创建提供者限定的 PSPath,请使用$provider.Name + “::” + $providerPath
. 如果$drive
不是$null
(您的当前位置可能是提供者限定的,在这种情况下$drive
将是$null
),那么您应该使用$drive.name + ":\" + $drive.CurrentLocation + "\" + "foo.txt"
来获取驱动器限定的 PSPath。
快速入门 C# 骨架
下面是一个 C# 提供程序感知 cmdlet 的框架,可帮助您进行操作。它已内置检查以确保已将文件系统提供程序路径传递给它。我正在为 NuGet 打包它,以帮助其他人编写行为良好的提供程序感知 Cmdlet:
using System;
using System.Collections.Generic;
using System.IO;
using System.Management.Automation;
using Microsoft.PowerShell.Commands;
namespace PSQuickStart
{
[Cmdlet(VerbsCommon.Get, Noun,
DefaultParameterSetName = ParamSetPath,
SupportsShouldProcess = true)
]
public class GetFileMetadataCommand : PSCmdlet
{
private const string Noun = "FileMetadata";
private const string ParamSetLiteral = "Literal";
private const string ParamSetPath = "Path";
private string[] _paths;
private bool _shouldExpandWildcards;
[Parameter(
Mandatory = true,
ValueFromPipeline = false,
ValueFromPipelineByPropertyName = true,
ParameterSetName = ParamSetLiteral)
]
[Alias("PSPath")]
[ValidateNotNullOrEmpty]
public string[] LiteralPath
{
get { return _paths; }
set { _paths = value; }
}
[Parameter(
Position = 0,
Mandatory = true,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true,
ParameterSetName = ParamSetPath)
]
[ValidateNotNullOrEmpty]
public string[] Path
{
get { return _paths; }
set
{
_shouldExpandWildcards = true;
_paths = value;
}
}
protected override void ProcessRecord()
{
foreach (string path in _paths)
{
// This will hold information about the provider containing
// the items that this path string might resolve to.
ProviderInfo provider;
// This will be used by the method that processes literal paths
PSDriveInfo drive;
// this contains the paths to process for this iteration of the
// loop to resolve and optionally expand wildcards.
List<string> filePaths = new List<string>();
if (_shouldExpandWildcards)
{
// Turn *.txt into foo.txt,foo2.txt etc.
// if path is just "foo.txt," it will return unchanged.
filePaths.AddRange(this.GetResolvedProviderPathFromPSPath(path, out provider));
}
else
{
// no wildcards, so don't try to expand any * or ? symbols.
filePaths.Add(this.SessionState.Path.GetUnresolvedProviderPathFromPSPath(
path, out provider, out drive));
}
// ensure that this path (or set of paths after wildcard expansion)
// is on the filesystem. A wildcard can never expand to span multiple
// providers.
if (IsFileSystemPath(provider, path) == false)
{
// no, so skip to next path in _paths.
continue;
}
// at this point, we have a list of paths on the filesystem.
foreach (string filePath in filePaths)
{
PSObject custom;
// If -whatif was supplied, do not perform the actions
// inside this "if" statement; only show the message.
//
// This block also supports the -confirm switch, where
// you will be asked if you want to perform the action
// "get metadata" on target: foo.txt
if (ShouldProcess(filePath, "Get Metadata"))
{
if (Directory.Exists(filePath))
{
custom = GetDirectoryCustomObject(new DirectoryInfo(filePath));
}
else
{
custom = GetFileCustomObject(new FileInfo(filePath));
}
WriteObject(custom);
}
}
}
}
private PSObject GetFileCustomObject(FileInfo file)
{
// this message will be shown if the -verbose switch is given
WriteVerbose("GetFileCustomObject " + file);
// create a custom object with a few properties
PSObject custom = new PSObject();
custom.Properties.Add(new PSNoteProperty("Size", file.Length));
custom.Properties.Add(new PSNoteProperty("Name", file.Name));
custom.Properties.Add(new PSNoteProperty("Extension", file.Extension));
return custom;
}
private PSObject GetDirectoryCustomObject(DirectoryInfo dir)
{
// this message will be shown if the -verbose switch is given
WriteVerbose("GetDirectoryCustomObject " + dir);
// create a custom object with a few properties
PSObject custom = new PSObject();
int files = dir.GetFiles().Length;
int subdirs = dir.GetDirectories().Length;
custom.Properties.Add(new PSNoteProperty("Files", files));
custom.Properties.Add(new PSNoteProperty("Subdirectories", subdirs));
custom.Properties.Add(new PSNoteProperty("Name", dir.Name));
return custom;
}
private bool IsFileSystemPath(ProviderInfo provider, string path)
{
bool isFileSystem = true;
// check that this provider is the filesystem
if (provider.ImplementingType != typeof(FileSystemProvider))
{
// create a .NET exception wrapping our error text
ArgumentException ex = new ArgumentException(path +
" does not resolve to a path on the FileSystem provider.");
// wrap this in a powershell errorrecord
ErrorRecord error = new ErrorRecord(ex, "InvalidProvider",
ErrorCategory.InvalidArgument, path);
// write a non-terminating error to pipeline
this.WriteError(error);
// tell our caller that the item was not on the filesystem
isFileSystem = false;
}
return isFileSystem;
}
}
}
Cmdlet 开发指南 (Microsoft)
以下是一些更通用的建议,从长远来看应该可以帮助您:http:
//msdn.microsoft.com/en-us/library/ms714657%28VS.85%29.aspx