手机
当前位置:查字典教程网 >脚本专栏 >PowerShell >Powershell实现捕获系统内置EXE程序的异常
Powershell实现捕获系统内置EXE程序的异常
摘要:支持所有版本。当你运行控制台EXE命令,如robocopy.exe,ipconfig.exe或类似命令。你可以用Powershell获得他们...

支持所有版本。

当你运行控制台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中的错误了。

【Powershell实现捕获系统内置EXE程序的异常】相关文章:

Powershell小技巧之获取注册表值的类型

Powershell后台作业、异步操作实例

PowerShell脚本实现创建桌面快捷方式的方法

PowerShell函数中限制数组参数个数的例子

PowerShell函数实现类似重载功能实例

Powershell小技巧之使用Copy-Item添加程序到开机启动

Powershell小技巧之删除不规则字符

PowerShell小技巧之定时记录操作系统行为

Powershell小技巧之从文件获取系统日志

PowerShell脚本trap语句捕获异常写法实例

精品推荐
分类导航