17

I want to have access to the same message that Powershell prints when you send an error record to the output stream

Example:

This is the exception message At C:\Documents and Settings\BillBillington\Desktop\psTest\exThrower.ps1:1 char:6

  • throw <<<< (New-Object ArgumentException("This is the exception"));
    • CategoryInfo : OperationStopped: (:) [], ArgumentException
    • FullyQualifiedErrorId : This is the exception

I when a get the last ErrorRecord by doing $Error[0] I can't seem to figure out how to get this information in a simple way

I found this 'Resolve-Error' function from the community extensions here which does roughly what I want but it prints a huge semi-formatted list of stuff I don't need that I have to then strip

Is there way of accessing the message that Powershell uses or failing that a simpler way of getting hash of the values I care about so I can put them into a string in a format of my choosing?

5 Answers 5

27

How about:

$x = ($error[0] | out-string)

Is that what you wanted?

Sign up to request clarification or add additional context in comments.

Comments

21

If you want a bit shorter message (more user friendly sometimes?) than @tomasr suggests this will do:

$error[0].ToString() + $error[0].InvocationInfo.PositionMessage

You will get something like:

Cannot find path 'C:\TEMP\_100804_135716\missing' because it does not exist.
At C:\TEMP\_100804_135716\test.ps1:5 char:15
+   Get-ChildItem <<<<  missing

This technical info will be excluded:

+ CategoryInfo          : ObjectNotFound: (C:\TEMP\_100804_135716\missing:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

Comments

7

I took it a bit further because I didn't like the multilines from $error[0].InvocationInfo.PositionMessage.

Function FriendlyErrorString ($thisError) {
    [string] $Return = $thisError.Exception
    $Return += "`r`n"
    $Return += "At line:" + $thisError.InvocationInfo.ScriptLineNumber
    $Return += " char:" + $thisError.InvocationInfo.OffsetInLine
    $Return += " For: " + $thisError.InvocationInfo.Line
    Return $Return
}

[string] $ErrorString = FriendlyErrorString $Error[0]
$ErrorString

You can look at what else is availible to construct your own String via:

$Error | Get-Member
$Error[0].InvocationInfo | Get-Member

1 Comment

An example of your abbreviated output format would be nice to help others see if it's what they want. Might improve your upvotes too...
1
Foreach ($Errors in $Error){
  #Log Eintrag wird zusammengesetzt und in errorlog.txt geschrieben
  "[$Date] $($Errors.CategoryInfo.Category) $($Errors.CategoryInfo.Activity) $($Errors.CategoryInfo.Reason) $($Errors.CategoryInfo.TargetName) $($Errors.CategoryInfo.TargetType) $($Errors.Exception.Message)" |Add-Content $Path\errorlog.txt -Encoding UTF8
}

You can also do this and you will get all Informations about the Error

Comments

1

Similar to @tomasr, but shorter:

$($error[0])

For all errors in a script:

$($error)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.