Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Catching a terminating error
- Try
- {
- $AuthorizedUsers= Get-Content \\ FileServer\HRShare\UserList.txt -ErrorAction Stop
- }
- Catch
- {
- Send-MailMessage -From ExpensesBot@MyCompany.Com -To WinAdmin@MyCompany.Com -Subject "HR File Read Failed!" -SmtpServer EXCH01.AD.MyCompany.Com
- Break
- }
- # Access the error record
- Try
- {
- $AuthorizedUsers= Get-Content \\ FileServer\HRShare\UserList.txt -ErrorAction Stop
- }
- Catch
- {
- $ErrorMessage = $_.Exception.Message
- $FailedItem = $_.Exception.ItemName
- 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"
- Break
- }
- # Catching specific errors
- Try
- {
- $AuthorizedUsers= Get-Content \\ FileServer\HRShare\UserList.txt -ErrorAction Stop
- }
- Catch [System.OutOfMemoryException]
- {
- Restart-Computer localhost
- }
- Catch
- {
- $ErrorMessage = $_.Exception.Message
- $FailedItem = $_.Exception.ItemName
- 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"
- Break
- }
- # Using "finally"
- Try
- {
- $AuthorizedUsers = Get-Content \\ FileServer\HRShare\UserList.txt -ErrorAction Stop
- }
- Catch [System.OutOfMemoryException]
- {
- Restart-Computer localhost
- }
- Catch
- {
- $ErrorMessage = $_.Exception.Message
- $FailedItem = $_.Exception.ItemName
- 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"
- Break
- }
- Finally
- {
- $Time=Get-Date
- "This script made a read attempt at $Time" | out-file c:\logs\ExpensesScript.log -append
- }
Add Comment
Please, Sign In to add comment