华域联盟 PowerShell Powershell实现捕获系统内置EXE程序的异常

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中的错误了。

本文由 华域联盟 原创撰写:华域联盟 » Powershell实现捕获系统内置EXE程序的异常

转载请保留出处和原文链接:https://www.cnhackhy.com/14871.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部