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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

BSonPoshCommented:
oy... slighty different approach if you want to do this remotely.

gimme a few to give you an example.
0
BSonPoshCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Server 2008

From novice to tech pro — start learning today.