我正在嘗試捕獲運行 Rename-Computer 命令后輸出的控制臺警告。我曾嘗試在輸出重定向時將其他 Stack Overflow 執行緒的點點滴滴聯系起來,但我似乎無法讓它作業。
我要做的是將遠程計算機重命名為變數。命令成功完成后,獲取警告“WARNING: The changes will take...”并將其字串放置在變數中。
Invoke-Command -ComputerName $deviceName -ScriptBlock{param($name); Rename-Computer $name -DomainCredential (Get-Credential) | Out-String -InputObject $output ;Return $output} -ArgumentList $newDeviceName
我已經在 var 中嘗試了 Invoke-Command 并查看它是否會改變任何內容。我希望將控制臺訊息放入變數而不重定向到檔案的原因是因為我在使用 WPF GUI 并希望將警告列印到我的 GUI 視窗。
uj5u.com熱心網友回復:
假設從輸出Rename-Computer正去警告流,這是你可以捕捉它:
$output = Invoke-Command -ComputerName $deviceName -ScriptBlock{
param($name)
Rename-Computer $name -DomainCredential (Get-Credential) 3>&1
} -ArgumentList $newDeviceName
$output # => Should be the warning from Rename-Computer
這是一個簡單的例子:
$capture = Write-Warning 'Hello World!' # doesn't work!
$capture = Write-Warning 'Hello World!' 3>&1 # works!
有關詳細資訊,請參閱about_Output Streams和about_Redirection。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/406809.html
標籤:
