我想将PowerShell命令的stderr输出存储在变量中。我不想将其存储在文件中,我不想包含标准输出,只是错误输出。
重定向到名为error.txt
的文件:
&安培; $ command $ params 2> error.txt
这会将stderr和stdout重定向到$ output变量:
$ output =& $ command $ params 2>& 1
但我想将仅错误输出存储在一个变量中(与上面的error.txt文件的内容相同),而无需向文件写入任何内容。我该怎么做?
答案 0 :(得分:20)
您可以以稍微不同的方式调用该命令,并使用PowerShell中的-ErrorVariable
参数:
Invoke-Expression "$command $params" -ErrorVariable badoutput
$badoutput
现在将包含错误字符串的内容。
答案 1 :(得分:0)
这是Simon Wahlin使用子表达式的更简单的解决方案
<块引用>$output = & $command $params 2>&1
应该是:
$errOutput = $( $output = & $command $params ) 2>&1
答案 2 :(得分:-2)
要添加到arco444,可以使用$badoutput[0].Exception
获取特定的例外。
-ErrorAction and -ErrorVariable 提供了有关有效使用PowerShell内置的ErrorAction和ErrorVariable参数的更多信息。
以下是显示此工作的最简单方法:
PS> Stop-Process 13,23
Stop-Process : Cannot find a process with the process identifier 13.
At line:1 char:13
+ Stop-Process <<<< 13,23
Stop-Process : Cannot find a process with the process identifier 23.
At line:1 char:13
+ Stop-Process <<<< 13,23
PS> Stop-Process 13,23 -ErrorAction Stop # Only 1 error
Stop-Process : Command execution stopped because the shell variable “ErrorA
ctionPreference” is set to Stop: Cannot find a process with the process ide
ntifier 13.
At line:1 char:13
+ Stop-Process <<<< 13,23 -ErrorAction Stop # Only 1 error
PS> Stop-Process 13,23 -ErrorAction silentlycontinue # No errors
PS> Stop-Process 13,23 -ErrorAction inquire # ASK
Confirm
Cannot find a process with the process identifier 13.
[Y] Yes [A] Yes to All [H] Halt Command [S] Suspend [?] Help
(default is “Y”):h
Stop-Process : Command execution stopped because the user selected the Halt
option.
At line:1 char:13
+ Stop-Process <<<< 13,23 -ErrorAction inquire # ASK
PS>
PS>
PS> Stop-Process 13,23 -ErrorVariable a -ErrorAction SilentlyContinue
PS> $a[0]
Stop-Process : Cannot find a process with the process identifier 13.
At line:1 char:13
+ Stop-Process <<<< 13,23 -ErrorVariable a -ErrorAction SilentlyContinue
PS> $a[0] |fl * -Force
Exception : Microsoft.PowerShell.Commands.ProcessCommandExcepti
on: Cannot find a process with the process identifi
er 13.
TargetObject : 13
CategoryInfo : ObjectNotFound: (13:Int32) [Stop-Process], ProcessC
ommandException
FullyQualifiedErrorId : NoProcessFoundForGivenId,Microsoft.PowerShell.Comma
nds.StopProcessCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
PS> $a |ft TargetObject -force -auto
TargetObject
————
13
23
现在有一件事对人们来说并不明显,你可以在ErrorVariable的变量名前面指定一个“+”,我们会将错误添加到该变量中。
PS> $err=@()
PS> stop-process 13 -ea silentlycontinue -ErrorVariable err
PS> $err.count
1
PS> stop-process 23 -ea silentlycontinue -ErrorVariable +err
PS> $err.count
2
PS> $err
Stop-Process : Cannot find a process with the process identifier 13.
At line:1 char:13
+ stop-process <<<< 13 -ea silentlycontinue -ErrorVariable err
Stop-Process : Cannot find a process with the process identifier 23.
At line:1 char:13
+ stop-process <<<< 23 -ea silentlycontinue -ErrorVariable +err
最后,您不需要键入-ErrorAction或-ErrorVariable,我们为这些定义了参数别名,因此您只需键入–EA
和-EV
。