信息发布→ 登录 注册 退出

Powershell实现捕获系统内置EXE程序的异常

发布时间:2026-01-11

点击量:

支持所有版本。

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

标签:# br  # replace  # EXE  # robocopy  # span  # Stop  # command  # emit  # ErrorActionPreference  # Powershell  # nbsp  # current  # 可以通过  # 当你  # 可以用  # 你就  # 捕获系统内置EXE程序异常  
在线客服
服务热线

服务热线

4008888355

微信咨询
二维码
返回顶部
×二维码

截屏,微信识别二维码

打开微信

微信号已复制,请打开微信添加咨询详情!