Powershell实现捕获系统内置EXE程序的异常
支持所有版本。
当你运行控制台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中的错误了。
相关推荐
higheels 2020-08-03
ZoctopusD 2020-08-03
酷云的csdn 2020-08-03
higheels 2020-07-27
liushun 2020-06-28
zhendeshifeng 2020-06-22
Sabrina 2020-06-11
CARBON 2020-06-03
CARBON 2020-06-01
DBATips 2020-05-31
higheels 2020-05-29
applecarelte 2020-04-15
Yyqingmofeige 2020-03-28
yoshubom 2020-03-06
Yyqingmofeige 2020-03-02
SciRui 2020-02-26