Print error in a form application

When a compiled Powershell Studio application gets an error, the error is not echoed to the screen automatically which makes it really hard to troubleshoot what have gone wrong. This short solution checks for error and prints in to a textbox of some sort.
The "return" quits the function, remove it to continue.

#Paste this where error checking is wanted in the code
If ($Error) {PrintError -Error $Error; Return}

#Function to print the error
function PrintError {
	$textbox.AppendText("Exception: `t$($Error[0].Exception.InnerException)`n")
	$textbox.AppendText("Message: `t$($Error[0].Exception.Message)`n")
	$textbox.AppendText("Name: `t`t$($Error[0].Exception.TargetSite.Name)`n")
	$textbox.AppendText("Reason: `t`t$($Error[0].CategoryInfo.Reason)`n")
	$textbox.AppendText("Category: `t$($Error[0].CategoryInfo.Category)`n")
	$textbox.AppendText("TargetName: `t$($Error[0].CategoryInfo.TargetName)`n")
	$textbox.AppendText("TargetType: `t$($Error[0].CategoryInfo.TargetType)`n`n")
Comments (0) Trackbacks (0)

No comments yet.

Leave a comment

No trackbacks yet.