I am new to Powershell and in learning Phase. I am facing an issue (might be it is looking as issue for me due to lack of knowledge) with below Powershell Script.
Requirement is, Script needs to be sent an email to set of people whenever application pool recycled. Script will search for Application pool event IDs from Event Viewer and will send an email if it find any events. If it's found the Event ID it will create an log file in D:\temp and send that log file to recipients.
Problem is, Script is sending an email with empty log file as attachment when no events happen. I dont want to send empty file and script should sent ONLY when event occurs. This can be resolve if we use IF condition, but not sure who to use that in my script.
Below is the script which i used to run, Currently it is sending log when event occur and not occur as well. Please help on this.
$date = Get-date -Format "MM-dd-yyyy HH-mm-ss"
$starttime = (get-date).AddMinutes(-5)
Get-WinEvent -ComputerName computer1 -Filterhashtable @{LogName='system';StartTime=$StartTime;ID=3201,5079,2262,2263,5070,5074,5075,5076,5077,5078,5080,5081,5093,5117,5186} | select machinename,timecreated,id,message | format-table machinename,timecreated,id,message -AutoSize | out-string -Width 6096 |Out-File "D:\Logs\PS\$date.txt" -NoClobber
send-mailmessage -To user1@test.com -Subject "Application Pool Recycled in Computer1" -Body "Application Pool has Recycled in Computer1" -SmtpServer smtp.com -From Apppool_Monitoring@test.com -Attachments "D:\Logs\PS\$date.txt"
Aucun commentaire:
Enregistrer un commentaire