I want to use
powershell to alert me when an error occurs in the event viewer on my new Win2k12 Standard Server, I was thinking I could have the script execute every 10mins but don't want to put any strain on the server just for event log checking, here is the
powershell script I want to use:
$SystemErrors = Get-EventLog System | Where-Object { $_.EntryType -eq "Error" }
If ($SystemErrors.Length -gt 0) {
Send-MailMessage -To "
[email protected]" -From $env:COMPUTERNAME + @company.co.nz" -Subject $env:COMPUTERNAME + " System Errors" -SmtpServer "smtp.company.co.nz" -Priority High
}
What is a safe frequency I can run this script at without hurting my server?
Hardware:
Intel Xeon E5410 @
2.33GHz x2
32GB RAM
3x 7200RPM S-ATA 1TB (2x RAID1)
Edit:
With the help of Mathias R. Jessen's answer, I ended up attaching an event to the application & system log with the following script:
Param(
[string]$LogName
)
$ComputerName = $env:COMPUTERNAME;
$To = "
[email protected]"
$From = $ComputerName + "@company.co.nz";
$Subject = $ComputerName + " " + $LogName + " Error";
$SmtpServer = "smtp.company.co.nz";
$AppErrorEvent = Get-EventLog $LogName -Newest 1 | Where-Object { $_.EntryType -eq "Error" };
If ($AppErrorEvent.Length -eq 1) {
$AppErrorEventString = $AppErrorEvent | Format-List | Out-String;
Send-MailMessage -To $To -From $From -Subject $Subject -Body $AppErrorEventString -SmtpServer $SmtpServer -Priority High;
};