PowerShell:将对象管道化为管道对象

时间:2017-02-24 07:04:25

标签: powershell

我使用的是Powershell 5,如果相关

将属性添加到管道中的当前项时:

我无法引用 $ _ 直到我已将其输入另一个管道。的为什么吗

# make some objects to pass around
$test = (0..3) | %{[pscustomobject]@{key1="v$_.1";key2="v$_.2"} }
#    GM = TypeName: System.Management.Automation.PSCustomObject
# trying to use the current pipeline object
$test | Add-Member @{key3=$_.key1}
$test | ft *

key1 key2 key3
---- ---- ----
v0.1 v0.2     
v1.1 v1.2     
v2.1 v2.2     
v3.1 v3.2     

# try again, this time taking the current pipeline object...
#... and then putting it into another identical looking pipeline
$test | %{ $_ | Add-Member @{key4=$_.key1} }
$test | ft *

key1 key2 key3 key4
---- ---- ---- ----
v0.1 v0.2      v0.1
v1.1 v1.2      v1.1
v2.1 v2.2      v2.1
v3.1 v3.2      v3.1

我怀疑第一个可能是自动暗示某些东西而且我没有告诉隐形/暗示函数传递$ PSItem。

0 个答案:

没有答案