Friday 23 January 2009

Get-ErrorDetails.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Shows detals of an $Error record for an exception 
  4. .DESCRIPTION 
  5.     Creates an exception then shows how to find out details.     
  6. .NOTES 
  7.     File Name  : Get-ErrorDetails.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell V2 CTP3 
  10. .LINK 
  11.     http://pshscripts.blogspot.com/2009/01/get-errordetailsps1.html
  12. .EXAMPLE 
  13.     PS C:\foo> .\Get-Errordetails.ps1' 
  14.     Output - left as an exercise for the reader 
  15. #> 
  16.  
  17. ### 
  18. # Start of Script 
  19. ### 
  20.  
  21. # Create divide by zero error 
  22.  
  23. try { 
  24. $zero = 0 
  25. 1/$zero   # will throw an exception 
  26. catch [System.DivideByZeroException] { 
  27. "Exception caught in catch block" 
  28. $err = $Error[0] 
  29.  
  30. # display $err contents ($error[0]) 
  31. "Contents of Error[0]" 
  32. $Err | fl * -Force 
  33. "" 
  34.  
  35. # now - show exception 
  36. "Exception details:" 
  37. $err.Exception | fl * -Force 
  38. "" 
  39.  
  40. # To get exception try: 
  41. "Exception:" 
  42. $err.Exception.InnerException | fl * -Force 
  43. "" 
  44.  
  45. # To get actual exception in this case: 
  46. "Specific exception name:" 
  47. $err.Exception.InnerException.tostring().split(":")[0] 
  48. catch { 
  49. "Some other error occurred" 
  50. $Error[0] 
  51. # End of script 
Technorati Tags: ,,

No comments: