PowerShell:使用 Invoke-Expression 管理错误 [英] PowerShell: Manage errors with Invoke-Expression

查看:115
本文介绍了PowerShell:使用 Invoke-Expression 管理错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何确定使用 Invoke-Expression 抛出的命令是否失败.甚至变量 $?、$LASTEXITCODE 或 -ErrorVariable 也帮不了我.

I try to figure how to determine if a command throw with Invoke-Expression fail. Even the variable $?, $LASTEXITCODE or the -ErrorVariable don't help me.

例如:

PS C:\>$cmd="cat c:\xxx.txt"

使用 Invoke-Expression 调用 $cmd

Call $cmd with Invoke-Expression

PS C:\>Invoke-Expression $cmd -ErrorVariable err

Get-Content : 找不到路径C:\xxx.txt",因为它不存在.在第 1 行字符:4<代码>+猫<<<<<

$?是真的

PS C:\>$?

$LASTEXITCODE 为 0

The $LASTEXITCODE is 0

PS C:\>$LASTEXITCODE

0

$err 是空的

PS C:\>$错误

PS C:\>

我发现的唯一方法是重定向文件中的 STD_ERR 并测试此文件是否为空

The only way I found is to redirect STD_ERR in a file and test if this file is empty

PS C:\>调用表达式 $cmd 2>err.txt

PS C:\>猫错误.txt

Get-Content : 找不到路径C:\xxx.txt",因为它不存在.在行:1 字符:4+ 猫 <<<<c:\xxx.txt+ CategoryInfo : ObjectNotFound: (C:\xxx.txt:String) [Get-Content], ItemNotFoundExcep重刑+ FullQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

Get-Content : Cannot find path 'C:\xxx.txt' because it does not exist. At line:1 char:4 + cat <<<< c:\xxx.txt + CategoryInfo : ObjectNotFound: (C:\xxx.txt:String) [Get-Content], ItemNotFoundExcep tion + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

这是唯一和最好的方法吗?

推荐答案

我疯狂地试图将 STDERR 流捕获到变量中.我终于解决了.invoke-expression 命令中有一个怪癖,它会使整个 2&>1 重定向失败,但如果省略 1,它会做正确的事情.

I was going crazy trying to make capturing the STDERR stream to a variable work. I finally solved it. There is a quirk in the invoke-expression command that makes the whole 2&>1 redirect fail, but if you omit the 1 it does the right thing.

 function runDOScmd($cmd, $cmdargs)
 {
 # record the current ErrorActionPreference
     $ep_restore = $ErrorActionPreference
 # set the ErrorActionPreference  
     $ErrorActionPreference="SilentlyContinue"

 # initialize the output vars
     $errout = $stdout = ""

 # After hours of tweak and run I stumbled on this solution
 $null = iex "& $cmd $cmdargs 2>''"  -ErrorVariable errout -OutVariable stdout
 <#                       these are two apostrophes after the >
     From what I can tell, in order to catch the stderr stream you need to try to redirect it,
     the -ErrorVariable param won't get anything unless you do. It seems that powershell
     intercepts the redirected stream, but it must be redirected first.
 #>
 # restore the ErrorActionPreference
 $ErrorActionPreference=$ep_restore

 # I do this because I am only interested in the message portion
 # $errout is actually a full ErrorRecord object
     $errrpt = ""
     if($errout)
     {
         $errrpt = $errout[0].Exception
     }

 # return a 3 member arraylist with the results.
     $LASTEXITCODE, $stdout, $errrpt
 }

这篇关于PowerShell:使用 Invoke-Expression 管理错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆