Get-Process 通過管道 (ByPropertyName) 接受 -computername 引數。我匯入的檔案如下所示:
computername
sql01
sql02
sql03
當我通過管道傳輸它時,我只會從串列中的第一臺計算機(sql01)獲取行程?
$a = Import-Csv C:\temp\computers.txt
$a | get-process -name dwm # output only from the first computer in the list
get-process dwm -ComputerName $a.computername # here correct output from 3 computers
uj5u.com熱心網友回復:
看起來您遇到了Windows PowerShell中的錯誤- 有關詳細資訊,請參閱底部部分。
該錯誤不太可能得到修復,因為 Windows PowerShell 只會看到未來的關鍵修復。
您已經知道解決方法:將計算機名稱陣列作為引數傳遞給
-ComputerName而不是通過管道。# Windows PowerShell only. Get-Process dwm -ComputerName $a.computername從更廣泛的角度來看,考慮切換到使用PowerShell的遠程處理,其中只有通用 cmdlet使用現代、防火墻友好的傳輸來
Invoke-Command促進遠程執行任意命令。類似地,使用相同傳輸的CIM cmdlet(例如Get-CimInstance)應該優先于它們取代的過時 WMI cmdlet(例如Get-WmiObject)。# Works in both PowerShell editions, assuming the target computers # are set up for PowerShell remoting. # Note: Invoke-Command does NOT support passing computer names via the pipeline. Invoke-Command -ComputerName $a.computername { Get-Process dwm -ComputerName }- 請注意,特定用途cmdlet
-ComputerName上的引數(例如Get-Process和Restart-Computer不再在 PowerShell (Core) 7 中可用, WMI cmdlet 也不再可用,因為它們基于.NET Remoting,一種與 PowerShell 無關的遠程處理形式)已被宣布過時,因此不屬于 .NET Core / .NET 5 。根據定義,手頭的錯誤不會影響 PowerShell(核心)。
- 請注意,特定用途cmdlet
漏洞詳情:
該錯誤特定于以下組合:
通過管道提供具有
.ComputerName屬性的物件,以便將屬性值系結到-ComputerName引數按名稱定位行程,通過(可能是位置隱含的)
-Name引數,或定位所有行程(既不傳遞 a-Name也不傳遞-Id引數)
換句話說:通過引數通過 PID(行程 ID)定位行程不受影響- 但只有在您之前從給定計算機獲得 PID 并且僅針對一臺計算機時,遠程使用才有用。-Id-Id
在 Windows PowerShell 中,Get-Process的-ComputerName引數旨在將通過管道提供的具有.ComputerName屬性的物件系結到-ComputerName引數:
WinPS> Get-Help Get-Process -Parameter ComputerName
-ComputerName <System.String[]>
# ...
Accept pipeline input? True (ByPropertyName)
# ...
但是,正如您所觀察到的那樣,只有以這種方式系結的第一個Get-Process計算機名稱與結合使用-Name- 任何剩余的計算機名稱都會被忽略:
# Target the local machine (.) and a NON-EXISTENT machine ('NOSUCH')
WinPS> [pscustomobject] @{ ComputerName='.' },
[pscustomobject] @{ ComputerName='NOSUCH' } |
Get-Process -Name powershell
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
970 48 170860 29120 56.41 4656 1 powershell
也就是說,本地機器的 PowerShell 行程被報告,不存在的計算機名稱被悄悄地忽略。注意:任何后續計算機名稱都將被忽略,無論它們是否參考現有計算機。使用一個不存在的應該明顯地作為一個錯誤浮出水面。如果您替換-Name powershell為-Id $PID,您確實會看到錯誤。
問題不是引數系結之一,因為Trace-Command呼叫的輸出顯示:
WinPS> Trace-Command -pshost -name ParameterBinding {
[pscustomobject] @{ ComputerName='.' },
[pscustomobject] @{ ComputerName='NOSUCH' } | Get-Process -Name powershell
}
DEBUG: ParameterBinding Information: 0 : BIND NAMED cmd line args [Get-Process]
# ...
DEBUG: ParameterBinding Information: 0 : BIND POSITIONAL cmd line args [Get-Process]
# ...
DEBUG: ParameterBinding Information: 0 : CALLING BeginProcessing
DEBUG: ParameterBinding Information: 0 : BIND PIPELINE object to parameters: [Get-Process]
DEBUG: ParameterBinding Information: 0 : PIPELINE object TYPE = [System.Management.Automation.PSCustomObject]
# ...
DEBUG: ParameterBinding Information: 0 : Parameter [ComputerName] PIPELINE INPUT ValueFromPipelineByPropertyName NO COERCION
DEBUG: ParameterBinding Information: 0 : BIND arg [.] to parameter [ComputerName]
# ...
DEBUG: ParameterBinding Information: 0 : BIND arg [System.String[]] to param [ComputerName] SUCCESSFUL
# ...
DEBUG: ParameterBinding Information: 0 : BIND PIPELINE object to parameters: [Get-Process]
DEBUG: ParameterBinding Information: 0 : PIPELINE object TYPE = [System.Management.Automation.PSCustomObject]
DEBUG: ParameterBinding Information: 0 : BIND arg [NOSUCH] to parameter [ComputerName]
# ...
DEBUG: ParameterBinding Information: 0 : BIND arg [System.String[]] to param [ComputerName] SUCCESSFUL
# ...
DEBUG: ParameterBinding Information: 0 : CALLING EndProcessing
BIND arg [.]和BIND arg [NOSUCH],后跟SUCCESSFUL狀態,表示兩個計算機名稱都已正確系結。
換句話說:錯誤必須在隨后使用正確系結引數的代碼中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/478648.html
標籤:电源外壳 powershell 远程处理
