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 生日广州男生祝福语简短