支持所有版本。
当你运行控制台EXE命令,如robocopy.exe, ipconfig.exe或类似命令。你可以用获得他们引起的异常:
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捕获内置命令中的错误了。
原文地址:Catching Errors in Native EXEs (Part 1)
本文链接: https://www.pstips.net/catching-errors-in-native-exes-part-1.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
