能否通过ByPropertyName管道绑定New-Item的-Value参数?
关于New-Item的-Value参数管道ByPropertyName绑定的问题解答
通过Help New-item -parameter "Value"可以确认,-Value <System.Object>参数同时支持ByPropertyName和ByValue两种管道绑定方式,但由于ByValue的优先级高于ByPropertyName,目前无法通过管道实现对该参数的ByPropertyName绑定。
即使管道左侧的对象带有Value属性,PowerShell仍会优先通过ByValue将整个对象绑定到-Value参数,而非提取对象的Value属性值进行绑定。
示例验证
$customObject = [PSCustomObject]@{ Value = "Lorem ipsum" } $customObject | New-Item -Name BPN_value.txt
运行上述代码后,生成的BPN_value.txt文件内容为@{Value = "Lorem ipsum"}——这证明$customObject是作为一个整体通过ByValue绑定到-Value参数,而非使用其Value属性的具体值。
内容的提问来源于stack exchange,提问作者Eduardo Fernández
相关产品推荐
相关产品推荐

