Heart_Crafter

Powershell Ping Gmail Alert

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