All,
I've been tasked with parsing a single task from a server scheduled task and email that to a couple individuals and a distribution list. I had the email portion working, and am now getting...
‘You cannot call a method on a null-valued expression.
$smtp.Send(&msg)
It is defined ‘$msg = new-object Net.Mail.MailMessage’
I also need to parse out only a specific task in the log. I have tried everything that I can think of and find to get to that level, but this is my first attempt at PowerShell. Here is what I have so far.
#################################
# Written by Shawn Miller #
# Parse Task Scheduler Log and #
# Email log to Regional Support #
# and End-Users #
# 4 FEB 14 ver. 1 #
#################################
# Parse scheduler log and create file
$yesterday = (get-date) - (new-timespan -day 1)
$events = get-winevent -FilterHashtable @{logname = "Microsoft-Windows-TaskScheduler/Operational"; ID = "201","202"; StartTime = $yesterday} | Format-List -Property Message | out-file "c:\stuff\test.log"
$events |foreach {
$_
}
# Create new email
$smtpServer = “phcmail.peabodyenergy.com”
$msg = new-object Net.Mail.MailMessage
#From Address
$msg.From = "Monitoring Admin <tivnoreply@peabodyenergy.com>"
#To Address, Copy the below line for multiple recipients
$msg.To.Add(“smiller3@peabodyenergy.com”)
#$msg.To.Add(“rnotich@peabodyenergy.com”)
#$msg.To.Add(“cjones@peabodyenergy.com”)
#$msg.To.Add(“dl-mdwstit@peabodyenergy.com”)
#Message Body
$msg.Body=”Attached is the status report for the EoM Midwest Engineering files copied to STLEN1.”
#Message Subject
$msg.Subject = “EoM Transfer report”
write-host "SENDING FILES"
#your file location
$files=Get-ChildItem “C:\Stuff\report\”
Foreach($file in $files)
{
Write-Host “Attaching File :- ” $file
$attachment = New-Object System.Net.Mail.Attachment –ArgumentList C:\Stuff\report\$file
$msg.Attachments.Add($attachment)
}
$smtp.Send($msg)
$attachment.Dispose();
$msg.Dispose();
write-host "Mail Sent Successfully"Any help would be greatly appreciated.
Respectfully,
Shawn