支持所有版本。
當你運行控制臺EXE命令,如robocopy.exe, ipconfig.exe或類似命令。你可以用Powershell獲得他們引起的錯誤:
代碼如下:
try
{
$current = $ErrorActionPreference
$ErrorActionPreference = 'Stop'
# this will cause an EXE command to emit an error
# (replace with any console-based EXE command)
net.exe user nonexistentUser 2>&1
$ErrorActionPreference = $current
}
catch
{
Write-Host ('Error occured: ' + $_.Exception.Message)
}
要捕獲錯誤你需要設置$ErrorActionPreference 為$stop,與此同時,你需要更改錯誤的輸出方式添加“2>&1”
這樣設置后,你就可以通過Powershell捕獲.net中的錯誤了。
新聞熱點
疑難解答