Ответ 1
Вы можете вызвать команду несколько иначе и использовать параметр -ErrorVariable
в PowerShell
Invoke-Expression "$command $params" -ErrorVariable badoutput
$badoutput
теперь будет содержать содержимое строки ошибки
Я хочу сохранить вывод stderr команды powershell в переменной. Я не хочу хранить его в файле, и я не хочу включать стандартный вывод, просто вывод ошибки.
Это перенаправляет файл с именем error.txt
& $command $params 2 > error.txt
Это перенаправляет stderr и stdout на переменную $output
$output = и $command $params 2 > & 1
Но я хочу сохранить только вывод ошибки в переменной (то же самое, что и содержимое файла error.txt выше), не записывая ничего в файл. Как это сделать?
Вы можете вызвать команду несколько иначе и использовать параметр -ErrorVariable
в PowerShell
Invoke-Expression "$command $params" -ErrorVariable badoutput
$badoutput
теперь будет содержать содержимое строки ошибки
To add to arco444, specific exception can be obtained by using $badoutput[0].Exception.
https://blogs.msdn.microsoft.com/powershell/2006/11/02/erroraction-and-errorvariable/ has more information on to effectively use the ErrorAction and ErrorVariable parameters built into Powershell.
Here is the easiest way to show this working:
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
Now one thing that is not obvious to people is that you can specify a "+" in front of the variable name for ErrorVariable and we will ADD the errors to that variable.
PS> [email protected]()
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
Lastly, you don’t need to type out –ErrorAction or –ErrorVariable, we have defined parameter aliases for these so you can just type –EA and -EV