簡體   English   中英

PowerShell:將對象管道化為管道對象

[英]PowerShell: Pipeline objects into pipeline objects

我正在使用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。

無論在管道中是否在語法上使用,所有參數都在命令啟動之前在當前 作用域中進行計算。 所以$test | Add-Member @{key3=$_.key1} $test | Add-Member @{key3=$_.key1}使用當前作用域中的$_ ,這意味着它不是$test的元素。

$_對於管道中的每個元素進行評估,一個新的范圍應為每個元素通過腳本塊(在大括號中的代碼)中創建ForEachWhere1..2 | ForEach { $_ } 1..2 | ForEach { $_ } ,或在select @{N='calculated property'; E={$_.foo}} E xpression等ScriptBlock參數中select @{N='calculated property'; E={$_.foo}} select @{N='calculated property'; E={$_.foo}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM