Script to parse last 24 hours from TaskScheduler Log Cannot figure out how to prase a single task.

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 <[email protected]>"
#To Address, Copy the below line for multiple recipients
$msg.To.Add(“[email protected]”)
#$msg.To.Add(“[email protected]”)
#$msg.To.Add(“[email protected]”)
#$msg.To.Add(“[email protected]”)
#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

I would use PowerShell to do this but you need to fix a lot of the code you have copied from various places.  Most of it is old.
Start with this:
#Parse scheduler log and create file
$yesterday =[DateTime]::Today.AddDays(-1)
get-winevent -FilterHashtable @{logname = "Microsoft-Windows-TaskScheduler/Operational"; ID = "201","202"; StartTime = $yesterday} |
Format-List -Property Message |
out-file c:\stuff\test.log
$mailprops=@{
SmtpServer='phcmail.peabodyenergy.com'
From ='Monitoring Admin <[email protected]>'
To='[email protected]','[email protected]'
Body='Attached is the status report for the EoM Midwest Engineering files copied to STLEN1.'
Subject='EoM Transfer report'
$files=Get-ChildItem C:\Stuff\report\
Send-MailMessage @mailprops -Attachments $files
write-host "Mail Sent Successfully"
It is more likely to work assuming you have access to an SMTP server which is not likely in a domain.  Check with your domain admins to find out how to gain access.
¯\_(ツ)_/¯

Similar Messages

Maybe you are looking for

  • How to access external drives while in Windows

    Using VMWare Fusion as the system hosting Windows XP. Have specific programs in Windows only but of course they work much better on the IMac. I edit a ton of video and have the vids all stored on eternal drives. These drives are read by the Mac and a

  • Target file of the same name as source file

    Hi,     I am working on a file to file scenario. I need the target file name to be same as the source file name.     I checked the adapter specific message attributes in the sender as well as receiver file adapter,and also checked the file name.    

  • Monitor looks like a negative

    My monitor looks like a negative. My text is white and the background is black. I looked through the system preferences to convert it back and can't seem to figure it out. I also looked through this website and couldn't find in any information. Can s

  • Keynote Remote Doesn't work with iPhone, iPad

    Hi, I have the latest versions of Keynote on my iPhone 5S, iPad 4 and MacBook Pro 13" with Retina Display. All three devices on the same WiFi network. Using WiFi I was able to remotely control/play the presetatnion between iPhone 5S and MacBook. Howe

  • Class loading and reflection

    Hi, I am trying to use a custom class loader to dynamically load Java classes so that I can get some info (e.g. methods) about the classes via reflection. My problem is that my class loader is written to load Java core classes (i.e. java.lang, java.u