12

I'm getting a Powershell error like this:

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

Is there a way I can avoid the command that triggered the error (i.e., $Disk = Get-WmiObject MSCluster_Disk ...) being truncated in the error message?

15

This can be found in the error object. $Error is an array of errors, and [0] is the most recent. That's actually an object that can be interrogated...

($Error[0]).InvocationInfo.Line

Will give you the full line in the script that generated the error. The InvocationInfo property will not exist for errors from the command line.

Other nice properties you can get are PSScriptRoot which gives you the path to the script file, ScriptName which gives you the file name, and ScriptLineNumber which gives you the line in the script that failed.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.