我使用的是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。