Powershell script to email event triggers

I'm new to powershell but would like to create a powershell script that emails me all errors from the event log.
I wish to use this to mointor any errors that occur on our servers. If you can help me with the scripting as I said I'm a beginner with powershell.
Cheers

Simon
WycombeAbbeyAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
BSonPoshConnect With a Mentor Commented:
Try something like this. Fill in the values and cut/paste in to the shell and make sure it works.

- If it does work save to a file call something.ps1.
- Run this command in your shell
Set-ExecutionPolicy RemoteSigned
- You can then schedule a task to call the "something.ps1" script.

NOTE: This will take a bit to run
function Send-mail{
    param($smtpServer,$to,$from,$subject,$msg)
    $smtp = new-Object system.Net.Mail.SmtpClient($smtpServer)
    $mail = new-Object system.Net.Mail.MailMessage($from,$to,$subject,$msg)
    $smtp.Send($mail)
}
 
$computers = Get-Content C:\ServerListFile.txt
 
foreach($computer in $computers)
{
    $events = [system.Diagnostics.EventLog]::GetEventLogs($computer) | %{$_.entries} | ?{$_.EntryType -eq "Error"} | Format-List | out-String
    Send-mail -smtpServer "<Relay Server>" `
              -to "<to addy>" `
              -from "<from addy>" `
              -subject "Errors from Computer [$Computer]" `
              -msg $events 
}

Open in new window

0
 
BSonPoshCommented:
Is powershell going to be installed locally to the machine you want to check? Would you prefer to have a central machine doing the checking/emailing?

0
 
BSonPoshCommented:
Here is an example of doing it local
function Send-mail{
    param($smtpServer,$to,$from,$subject,$msg)
    $smtp = new-Object system.Net.Mail.SmtpClient($smtpServer)
    $mail = new-Object system.Net.Mail.MailMessage($from,$to,$subject,$msg)
    $smtp.Send($mail)
}
 
# To get all the errors from the local event log
$events = Get-EventLog -List | %{$_.entries} | ?{$_.EntryType -eq "error"} | out-string

Open in new window

0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
WycombeAbbeyAuthor Commented:
I would want to run it from a central location.
Sorry to ask a dumb question but how do I apply/run this script? Do i run once at it pulls all event errors?
Can I do it so it pulls the last 7 days as well? Also where do I set my smtp server, receipt, etc.
cheers
0
 
BSonPoshCommented:
oy... slighty different approach if you want to do this remotely.

gimme a few to give you an example.
0
 
WycombeAbbeyAuthor Commented:
I'm get this error
Exception calling "Send" with "1" argument(s): "Failure sending mail."
At C:\errors.ps1:5 char:15
+     $smtp.Send( <<<< $mail)
0
 
BSonPoshCommented:
Hmmm... it may not like the size of the message.

can you try to use the send-mail with a generic message and see if that sends?

    Send-mail -smtpServer "<Relay Server>" `
              -to "<to addy>" `
              -from "<from addy>" `
              -subject "Errors from Computer [$Computer]" `
              -msg "Test Event"
0
 
WycombeAbbeyAuthor Commented:
Ok the send-mail works on its own
0
 
WycombeAbbeyAuthor Commented:
Just to check I dont need to replace any of the $from, $to, $smtpserver, etc
0
 
BSonPoshCommented:
No... you are using send-mail as a black box and passing the data from the outside.
0
 
WycombeAbbeyAuthor Commented:
Ok it seems to be working now for some of the servers. I guess theres a problem with size on the others. Is there anyway I can limit it to a certain number of days?
0
 
BSonPoshCommented:
Sure can... change the eventlog collection line to this instead. Replace the -1 with whatever amount of days back you want to go.


$events = [system.Diagnostics.EventLog]::GetEventLogs($computer) | %{$_.entries} | ?{($_.EntryType -eq "Error") -and ($_.TimeGenerated -lt (Get-date).AddDays(-1))} | Format-List | out-String

Open in new window

0
 
WycombeAbbeyAuthor Commented:
Nice one thanks for your help Bsonposh exactly what I wanted
0
 
WycombeAbbeyAuthor Commented:
Perfect replys couldnt of asked for anymore help
0
All Courses

From novice to tech pro — start learning today.