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. } 
  27. catch [System.DivideByZeroException] { 
  28. "Exception caught in catch block" 
  29. $err = $Error[0] 
  30.  
  31. # display $err contents ($error[0]) 
  32. "Contents of Error[0]" 
  33. $Err | fl * -Force 
  34. "" 
  35.  
  36. # now - show exception 
  37. "Exception details:" 
  38. $err.Exception | fl * -Force 
  39. "" 
  40.  
  41. # To get exception try: 
  42. "Exception:" 
  43. $err.Exception.InnerException | fl * -Force 
  44. "" 
  45.  
  46. # To get actual exception in this case: 
  47. "Specific exception name:" 
  48. $err.Exception.InnerException.tostring().split(":")[0] 
  49. } 
  50. catch { 
  51. "Some other error occurred" 
  52. $Error[0] 
  53. } 
  54. # End of script 
Technorati Tags: ,,

No comments: