Scripting

PowerShell to send an email via sendgrid or authentificated SMTP when something happens in the Event Log.

If you need to get notified when something happen in the event log

How it works:

1) In Event viewer, select the event you want to get notified. Right click it and select “Attach a Task To this Event…”

Powershell (attach task to this log)
Powershell (attach task to this log)

 

2) Follow the wizard.  Give a Name

Create Basic Wizard Step 2
Create Basic Wizard Step 2

3 ) Click on next

Create Basic Wizard Step 3
Create Basic Wizard Step 3

4 ) Step 4 :

Create Basic Wizard Step 4

  1. Program/script = c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe
  2. Add Argument  .\senderror.ps1 “Application”        (The Windows Logs name, I setup the “Application” log)
  3. Start in =  c:\temp
  4. Next

5 ) Step 5 :

Create Basic Wizard Step 4
Create Basic Wizard Step 4

Click on next

Create Basic Wizard Step 5
Create Basic Wizard Step 5

 

 

5) Save the script file in c:\temp with the name senderror.ps1 .

—————————————————————————————

# Pass The event Name value to $NAmeEV
$NameEV = $args[0]

# Delect the newest log
$event = Get-EventLog $NameEV -newest 1

# “Error” – send only error
if ($event.EntryType -eq “Error”)
{
$PCName = $env:COMPUTERNAME

$EmailBody = $event | Sort-Object EntryType,Message,TimeGenerated | Out-String -width 4096

$EmailFrom = “$PCName <[email protected]$PCName>”
$EmailTo = “Youremailaddress”
$EmailSubject = “New Event Log ($NameEV) [$PCName]”
$SMTPServer = “smtp.sendgrid.net”

$secpasswd = ConvertTo-SecureString “yourpassword” -AsPlainText -Force

$credentials=New-Object System.Management.Automation.PSCredential(”yourusername”,$secpasswd)

Write-host “Sending Email”
Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $EmailSubject -body $EmailBody -SmtpServer smtp.sendgrid.net -Credential $credentials
}


About Sendgrid 

 

Sources : audain.net