Guest User

Untitled

a guest
Apr 10th, 2018
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. # Catching a terminating error
  2.  
  3. Try
  4. {
  5. $AuthorizedUsers= Get-Content \\ FileServer\HRShare\UserList.txt -ErrorAction Stop
  6. }
  7. Catch
  8. {
  9. Send-MailMessage -From ExpensesBot@MyCompany.Com -To WinAdmin@MyCompany.Com -Subject "HR File Read Failed!" -SmtpServer EXCH01.AD.MyCompany.Com
  10. Break
  11. }
  12.  
  13.  
  14. # Access the error record
  15.  
  16. Try
  17. {
  18. $AuthorizedUsers= Get-Content \\ FileServer\HRShare\UserList.txt -ErrorAction Stop
  19. }
  20. Catch
  21. {
  22. $ErrorMessage = $_.Exception.Message
  23. $FailedItem = $_.Exception.ItemName
  24. Send-MailMessage -From ExpensesBot@MyCompany.Com -To WinAdmin@MyCompany.Com -Subject "HR File Read Failed!" -SmtpServer EXCH01.AD.MyCompany.Com -Body "We failed to read file $FailedItem. The error message was $ErrorMessage"
  25. Break
  26. }
  27.  
  28.  
  29. # Catching specific errors
  30.  
  31. Try
  32. {
  33. $AuthorizedUsers= Get-Content \\ FileServer\HRShare\UserList.txt -ErrorAction Stop
  34. }
  35. Catch [System.OutOfMemoryException]
  36. {
  37. Restart-Computer localhost
  38. }
  39. Catch
  40. {
  41. $ErrorMessage = $_.Exception.Message
  42. $FailedItem = $_.Exception.ItemName
  43. Send-MailMessage -From ExpensesBot@MyCompany.Com -To WinAdmin@MyCompany.Com -Subject "HR File Read Failed!" -SmtpServer EXCH01.AD.MyCompany.Com -Body "We failed to read file $FailedItem. The error message was $ErrorMessage"
  44. Break
  45. }
  46.  
  47.  
  48. # Using "finally"
  49.  
  50. Try
  51. {
  52. $AuthorizedUsers = Get-Content \\ FileServer\HRShare\UserList.txt -ErrorAction Stop
  53. }
  54. Catch [System.OutOfMemoryException]
  55. {
  56. Restart-Computer localhost
  57. }
  58. Catch
  59. {
  60. $ErrorMessage = $_.Exception.Message
  61. $FailedItem = $_.Exception.ItemName
  62. Send-MailMessage -From ExpensesBot@MyCompany.Com -To WinAdmin@MyCompany.Com -Subject "HR File Read Failed!" -SmtpServer EXCH01.AD.MyCompany.Com -Body "We failed to read file $FailedItem. The error message was $ErrorMessage"
  63. Break
  64. }
  65. Finally
  66. {
  67. $Time=Get-Date
  68. "This script made a read attempt at $Time" | out-file c:\logs\ExpensesScript.log -append
  69. }
Add Comment
Please, Sign In to add comment