Powershell实现捕获系统内置EXE程序的异常
支持所有版本。
当你运行控制台EXE命令,如robocopy.exe,ipconfig.exe或类似命令。你可以用Powershell获得他们引起的错误:
try
{
$current=$ErrorActionPreference
$ErrorActionPreference='Stop'
#thiswillcauseanEXEcommandtoemitanerror
#(replacewithanyconsole-basedEXEcommand)
net.exeusernonexistentUser2>&1
$ErrorActionPreference=$current
}
catch
{
Write-Host('Erroroccured:'+$_.Exception.Message)
}
要捕获错误你需要设置$ErrorActionPreference为$stop,与此同时,你需要更改错误的输出方式添加“2>&1”
这样设置后,你就可以通过Powershell捕获.net中的错误了。
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语