cmdlet 的 LiteralPath 选项 [英] LiteralPath option for cmdlet
问题描述
在我在教程和书籍中看到的大多数示例中,默认情况下几乎从不使用 -LiteralPath 选项(似乎首选 -Path 选项).因为 -LiteralPath 选项允许使用保留字符(例如 []),我不明白为什么不经常使用它(如果不是,则一直使用).是因为更喜欢手动转义保留字符,因为它的性能成本很高,因为不是所有的cmdlet都支持这个选项还是因为别的什么?
需要考虑的一点是 -Path
最有可能是通过管道传入字符串时使用的参数.
以 Get-Item
cmdlet 为例:
-LiteralPath 必需的?真的位置?命名接受管道输入?真(按属性名称)参数集名称 LiteralPath别名 PSPath动态的?错误的-路径<字符串[]>必需的?真的位置?0接受管道输入?true (ByValue, ByPropertyName)参数集名称 路径别名 无动态的?错误的
管道化一个字符串让你-Path
.您的对象必须具有 LiteralPath
(或 PSPath
)属性,以便 Get-Item
使用它.如果您的对象同时具有,则使用 -Path
.
我想这不一定能回答问题;说-Path
使用更频繁,因为它更受欢迎"有点循环.一个更好的问题可能是,为什么 -Path
的行为首先不像 -LiteralPath
?"
In most example that I see in tutorials and books, the -LiteralPath option is almost never used by default (the -Path option seems to be preferred). Because the -LiteralPath option allows to use reserved characters (e.g. []), I don't understand why it is not used more often (if not, all the time). Is it because it is preferred to escape reserved characters manually, because it has a high performance cost, because not all cmdlet support this option or because something else?
One thing to consider is that -Path
is most likely to be the parameter that used when a string is passed in through the pipeline.
Looking at the Get-Item
cmdlet for instance:
-LiteralPath <string[]> Required? true Position? Named Accept pipeline input? true (ByPropertyName) Parameter set name LiteralPath Aliases PSPath Dynamic? false -Path <string[]> Required? true Position? 0 Accept pipeline input? true (ByValue, ByPropertyName) Parameter set name Path Aliases None Dynamic? false
Piping a string gets you -Path
. Your object(s) would have to have a LiteralPath
(or PSPath
) property for Get-Item
to use it. If your object has both, -Path
is used.
I guess this doesn't necessarily answer the question; it's a bit circular to say "-Path
is used more often because it's more popular". A better question might be, "Why doesn't -Path
behave like -LiteralPath
in the first place?"
这篇关于cmdlet 的 LiteralPath 选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!