[英]How to save powershell hyper-v errors to file
I am making a script to turn virtual machines on and off in hyper-v.我正在制作一个脚本来打开和关闭 hyper-v 中的虚拟机。 Sometimes the Stop-VM command fails and I need to save the bug or reflect it in some way in a log file有时 Stop-VM 命令失败,我需要保存错误或以某种方式在日志文件中反映它
I tried putting the command in a trycath but it didn't work.我尝试将命令放在 trycath 中,但没有用。
Command:命令:
Stop-VM $VMapagar
Sometimes the command gives me this error and does not turn off the machine有时命令给我这个错误并且不关闭机器
Stop-VM: Could not stop.
I would like to be able to reflect the failure in some way in a log.txt Thanks!我希望能够在 log.txt 中以某种方式反映失败 谢谢!
Use Try..Catch to trap the error by telling PS to treat it as a terminating error, then process it as you require:使用 Try..Catch 通过告诉 PS 将其视为终止错误来捕获错误,然后根据需要对其进行处理:
# Rest of your script
Try {
# Run your command, but tell PS to stop if it find an error
# You can explore the effects of the other possible values for -ErrorAction in PS documentation.
Stop-VM $VMpagar -ErrorAction Stop
# If it's got this far, then there can't have been an error so write a success message to console
Write-Host "OK"
}
Catch {
# This code will process if there was an error in the "Try" block
# By default, within the "Catch" block, the "$_" variable contains the error message
Write-Host "Error: $_"
# Write the error to a log file - "`n" tells PS to write a newline before the subsequent text
Add-Content -Path 'c:\temp\log.txt' -Value "`n$_"
# You could stop the script here using "Throw" or "Exit" commands if you want the whole script to stop on ANY error
}
# Your script will continue from this point if you haven't stopped it
Scepticalist's helpful answer shows how to capture a terminating error , by using the common -ErrorAction
( -ea
) parameter with value 'Stop'
in order to promote non-terminating errors (the most common kind) to terminating ones, which allows them to be trapped with a try
/ catch
/ finally
statement . Scepticalist乐于助人的回答显示了如何捕捉终止错误,通过使用共同-ErrorAction
( -ea
)的参数值为'Stop'
,以促进非终止错误(最常见的一种)到终止的,这使得他们成为被try
/ catch
/ finally
语句困住。
Note that this approach limits you to capturing the first non-terminating error (whereas a single cmdlet call may emit multiple ones), because it - thanks to -ErrorAction Stop
- then instantly terminates the statement and transfers control the catch
block (where the automatic $_
variable reflects the triggering error in the form of an [ErrorRecord]
instance).注意,这种方法限制您拍摄的第一个非终止错误(而单cmdlet的通话可能会发出多的),因为它-感谢-ErrorAction Stop
-然后立即终止该语句,并把控制catch
块(其中自动$_
变量以[ErrorRecord]
实例的形式反映触发错误)。
Also note that execution continues after a catch
block by default - unless you explicitly use throw
to re-throw the terminating error (or use a statement such as exit
to exit the script).另请注意,默认情况下在catch
块之后继续执行 - 除非您明确使用throw
重新抛出终止错误(或使用诸如exit
的语句退出脚本)。
To capture - potentially multiple - non-terminating errors you have two options:要捕获 - 可能是多个-非终止错误,您有两个选择:
Redirect them directly to a file , using the redirection operator >
with the number of the error stream, 2
:使用重定向运算符>
和错误流的编号2
将它们直接重定向到文件:
Stop-Vm $vms 2>errs.txt
errs.txt
;这会将任何错误悄悄地发送到文件errs.txt
; that is, you won't see them in the console.也就是说,您不会在控制台中看到它们。 If no errors occur, an empty file is created.如果没有发生错误,则会创建一个空文件。2>&1
you can capture success output (stdout) and errors (stderr) combined , and split them by their source stream later - see the bottom section of this answer .但是,使用重定向2>&1
您可以捕获成功输出 (stdout) 和错误 (stderr)组合,并稍后按其源流拆分它们 - 请参阅此答案的底部。 Use the common -ErrorVariable
( -ev
) parameter to collect any non-terminating errors in a variable - note that the target variable must be specified without the $
:使用常见的-ErrorVariable
( -ev
) 参数来收集变量中的任何非终止错误- 请注意,必须指定目标变量而不带$
:
Stop-Vm $vms -ErrorVariable errs
-ErrorAction SilentlyContinue
to prevent that.默认情况下,错误仍在,以及输出,因此打印到默认控制台(主机),但您可以添加-ErrorAction SilentlyContinue
防止。 Caveat : Do not use -ErrorAction Ignore
, as that will categorically suppress errors and prevent their collection.警告:不要使用-ErrorAction Ignore
,因为这将断然抑制误差,并防止其收藏。$errs
array (list), which is empty if no errors occurred and otherwise contains one or more [ErrorRecord]
instances, and send the collected errors to a file on demand;然后,您可以检查$errs
数组(列表),如果没有发生错误,则该数组为空,否则包含一个或多个[ErrorRecord]
实例,并根据需要将收集到的错误发送到文件; eg:例如:
if ($errs) { $errs > errs.txt }
See also:也可以看看:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.