我的情況:ErrorActionPreference和ErrorAction SilentlyContinue爲GET-PSSessionConfiguration
$ErrorActionPreference = "Stop";
"1 - $ErrorActionPreference;"
Get-ChildItem NoSuchFile.txt -ErrorAction SilentlyContinue;
"2 - $ErrorActionPreference;"
Get-ChildItem NoSuchFile.txt -ErrorAction Stop;
"3 - $ErrorActionPreference;"
輸出:
1 - 停止;
2 - 停止;
並顯示錯誤...
現在,
$ErrorActionPreference = "Stop";
"1 - $ErrorActionPreference;"
(Get-PSSessionConfiguration -Name "MyShellUri" -ErrorAction SilentlyContinue)
"2 - $ErrorActionPreference;"
輸出:
1 - 停止;
並顯示錯誤...
爲什麼不爲GET-PSSessionConfiguration工作-ErrorAction SilentlyContinue)?
更新:
現在,
$ErrorActionPreference = "Continue"
"1 - $ErrorActionPreference;"
(Get-PSSessionConfiguration -Name "MyShellUri" -ErrorAction SilentlyContinue)
"2 - $ErrorActionPreference;"
輸出:
1 - 繼續;
2 - 繼續;
現在,
$ErrorActionPreference = "SilentlyContinue"
"1 - $ErrorActionPreference;"
(Get-PSSessionConfiguration -Name "MyShellUri" -ErrorAction SilentlyContinue)
"2 - $ErrorActionPreference;"
輸出:
1 - SilentlyContinue;
2 - 默默繼續;
的ErrorAction
無處不參數可用於使用參數值SilentlyContinue
沉默非終止錯誤,它可以用於轉換使用參數值Stop
非終止錯誤終止錯誤。然而,它無法幫助您忽略終止錯誤,在這種情況下,Stop-Transcript會拋出終止錯誤。如果你想忽略,使用一個try/catch例如爲:
try { Stop-Transcript } catch {}
我不知道'catch'獲得異常仍需要'-ErrorAction Stop':| – AceMark 2015-01-29 02:58:28