Solved

Task Scheduler Sending *.log

Posted on 2014-03-05
8
460 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
  • 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
 

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Integrate social media with email signatures

Is your company active on social media? Do you also use email signatures? Including social media icons in your email signature is a great way to get fans for free. Let all your email users know you’re on social media quickly and easily, in a single click.

Join & Write a Comment

Suggested Solutions

In this article, I will show you HOW TO: Suppress Configuration Issues and Warnings Alert displayed in Summary status for ESXi 6.5 after enabling SSH or ESXi Shell.
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
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.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now