Advertisement
Heart_Crafter

Powershell Ping Gmail Alert

Apr 24th, 2019
857
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # To call from task scheduler:
  2. #   program: powershell
  3. #   args: -File "C:\Task Scheduler\PingAlert.ps1" -server ServerName -subject "Azure VPN Down" -alertees "helpdesk@mhasp.org,admin@itdata.com"
  4.  
  5. # command line: powershell -File "C:\Task Scheduler\PingAlert.ps1" -server ServerName -subject "Azure VPN Down" -alertees
  6. "helpdesk@domain.org,admin@domain.com"
  7.  
  8. Param (
  9.     [string] $server = "ServerName",
  10.     [string] $subject = "Test Subject",
  11.     [string] $alertees = "comma delimited emails")
  12.  
  13. # Function to send email - function has to be defined before it's called.
  14. function sendAlertEmail ()
  15. {
  16.     Param ([string] $message, [string] $subject)
  17.  
  18.     $user = "italerts@domain.org"    
  19.     $pass = ConvertTo-SecureString -String "P2ssw0rd" -AsPlainText -Force
  20.     $cred = New-Object System.Management.Automation.PSCredential $user, $pass
  21.     $mailParam = @{
  22.         To = $alertees
  23.         From = "IT Alerts <italerts@domain.org>"
  24.         Subject = $subject
  25.         Body = $message
  26.         SmtpServer = "smtp.gmail.com"
  27.         Port = 587
  28.         Credential = $cred
  29.     }
  30.     Send-MailMessage @mailParam -UseSsl
  31. }
  32.  
  33. if (Test-Connection $server -Quiet)
  34. {
  35.     "Passed"
  36. }
  37. else
  38. {
  39.     sendAlertEmail -message "Failed to ping $server from $env:computername" -subject "[IT Alerts] $subject"
  40.     "Email Sent, will pause the script for about an hour."
  41.     Start-Sleep -Seconds 3500
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement