Solved

Task Scheduler Sending *.log

Posted on 2014-03-05
8
472 Views
Last Modified: 2014-03-10
I have been working on a script to do some vmware related things and when it completes it produces a .log with the time and date included in the filename.  I then have setup a task in the windows task scheduler to run the script and then the second step is to email me about it and include the file as an attachment.  The problem I have is with it consistently changing the filename I cannot point the task to just one file.

I attempted to tell it to send *.log and it did not send anything.  Has anyone attempted this or found a solution?
0
Comment
Question by:nextechexchadmin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 4

Expert Comment

by:Jason Ryberg
ID: 39906867
You could select the latest modified file in your scheduled script like so:

$Path = \\server\backup_directory
$latestlog = Get-ChildItem $Path | sort LastWriteTime | Select -Last 1

Open in new window

0
 

Author Comment

by:nextechexchadmin
ID: 39906933
I'm not sure I understand.  I have a scheduled task with two actions, first run my vmware script and second it to email me the log it produces. Inside the email action I cannot find a way to have it attach the newest file.
Task.PNG
0
 
LVL 4

Expert Comment

by:Jason Ryberg
ID: 39906963
Can you post the script you're having issues with?
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 

Author Comment

by:nextechexchadmin
ID: 39906976
I am not having problems with the script.  It is doing its task.  I just need the second action, the one sending the email, to attach a file correctly.

I have sent a pic of what the action of sending an email looks like.
email.PNG
0
 
LVL 4

Accepted Solution

by:
Jason Ryberg earned 30 total points
ID: 39907238
I would schedule a powershell script to send the email instead of using task schedule itself to send the email.  That way you can select the log file you want.

$Path = \\server\backup_directory
$latestlog = Get-ChildItem $Path | sort LastWriteTime | Select -Last 1

[string]$Emailbody = get-content C:\emailbody.txt
$attachments = $latestlog
$emailFrom = "admin@contoso.com"
$emailTo = "admin@contoso.com"
$subject = "Report"
$smtpServer = "10.10.254.1"
Send-MailMessage -To $emailTo -From $emailFrom -Subject $subject -Body $Emailbody -SmtpServer $smtpServer -Attachments $Latestlog

Open in new window

0
 

Author Comment

by:nextechexchadmin
ID: 39907923
I attempted this and I got the script to work without the attachment line, but when I include it I receive an odd message.  The dir where the report is generated is c:/power cli scripts/reports/ and the error is saying it cannot find the file in another dir, c:/users/ME/<FILE>.

The odd part is the file is correct, it can locate the correct and newest log but then when the time calls for it to grab it and email it is looks for it under my user dir.

Attached may explain more as well.
close.PNG
0
 
LVL 4

Expert Comment

by:Jason Ryberg
ID: 39907949
My apologies, you need the Fullpath of the attachment.  Change line 5 from the script above to:

$attachments = $latestlog.FullName
0
 

Author Comment

by:nextechexchadmin
ID: 39917720
It seems that works! Thank you for your hard work!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

No single Antivirus application (despite claims by manufacturers) will catch or protect you from all Virus / Malware or Spyware threats. That doesn't stop you from further protecting yourself however - and this article is to show you how.
When asking a question in a forum or creating documentation, screenshots are vital tools that can convey a lot more information and save you and your reader a lot of time
This video shows you how easy it is to boot from ISO images for virtual machines with the ISO images stored on a local datastore on the ESXi host.
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question