避免在Powershell错误中截断错误消息


11

我收到这样的Powershell错误:

PS C:\mydirectory> $Error[0]
Get-WmiObject : 
At line:143 char:13
+           $Disk = Get-WmiObject MSCluster_Disk -ComputerName $Resource.OwnerNode -Auth    ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

有什么方法可以避免导致错误的命令(即$Disk = Get-WmiObject MSCluster_Disk ...)在错误消息中被截断?

Answers:


14

可以在错误对象中找到。$Error是一系列错误,并且[0]是最新的错误。这实际上是一个可以被询问的对象...

($Error[0]).InvocationInfo.Line

将在生成错误的脚本中为您提供整行。InvocationInfo对于命令行中的错误,该属性将不存在。

您可以获得的其他不错的属性是PSScriptRoot给您脚本文件的路径,ScriptName文件名以及ScriptLineNumber失败的脚本行。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.