The System.IO.FileInfo https://docs.microsoft.com/en-US/dotnet/api/System.IO.FileInfo / System.IO.DirectoryInfo https://docs.microsoft.com/en-US/dotnet/api/System.IO.DirectoryInfo instances output by PowerShell cmdlets have a .PSPath
property[*] that contains the instances' fully qualified path, which is the full file-system path prefixed by the PS provider name (e.g., Microsoft.PowerShell.Core\FileSystem::C:\windows
).
文件处理 cmdlets例如Get-FileHash
have a -LiteralPath
范围其别名为-PSPath
.
因为一个-LiteralPath
参数(典型值)接受来自管道的输入按属性名称,输入对象具有.PSPath
属性自动绑定到它,凭借PSPath
参数别名。
作为旁白:
-
文件处理 cmdlet 还具有-Path
参数,它将其参数解释为wildcard表达式 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Wildcards,而不是字面路径。
- 当你管道路径strings对于此类 cmdlet,它们绑定到
-Path
,这特别意味着它们确实被解释为通配符 - 虽然这通常并不重要,因为大多数文字路径不包含通配符元字符,但对于包含[
and ]
,然后被误解;避免这种误解需要将它们转义为`[
and `]
,如图所示这个答案 https://stackoverflow.com/a/70413617/45375.
-
由于错误Windows PowerShell(自从固定在PowerShell(核心)7+), Get-FileHash
具体来说,不接受字符串通过管道 - see 这个答案 https://stackoverflow.com/a/69922416/45375了解详情。
如何发现这种行为:
- Via the 在线帮助主题 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-filehash?view=powershell-7:
-
以编程方式:
- Note:
Get-Help Get-FileHash -Parameter LiteralPath | Select-Object name, aliases, pipelineinput
也适用于this情况,但这种方法通常仅限于附带的目标命令MAML https://en.wikipedia.org/wiki/Microsoft_Assistance_Markup_Language基于帮助文件,甚至那些有的帮助文件也可以是不同步与实际的命令定义。
& {
Get-Command $args[0] | % Parameters | % $args[1] |
Select-Object Name, Aliases, @{
n = 'Accepts pipeline input';
e = { $(if ($_.Attributes.ValueFromPipeline) { 'by value' }), $(if ($_.Attributes.ValueFromPipelineByPropertyName) { 'by property name' }) -join ', ' -replace '^, ' }
}
} Get-FileHash LiteralPath
Output:
Name Aliases Accepts pipeline input
---- ------- ----------------------
LiteralPath {PSPath, LP} by property name
[*] It is PowerShell's file-system provider that adds this property, among others. All PowerShell providers https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Providers decorate their output items this way, such as the Microsoft.Win32.RegistryKey
instances output by the registry provider. The underlying .NET types do not have it. See this answer https://stackoverflow.com/a/55881978/45375 for more information.