Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 481
  • Last Modified:

Task Scheduler Sending *.log

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
nextechexchadmin
Asked:
nextechexchadmin
  • 4
  • 4
1 Solution
 
Jason RybergCommented:
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
 
nextechexchadminAuthor Commented:
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
 
Jason RybergCommented:
Can you post the script you're having issues with?
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
nextechexchadminAuthor Commented:
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
 
Jason RybergCommented:
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
 
nextechexchadminAuthor Commented:
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
 
Jason RybergCommented:
My apologies, you need the Fullpath of the attachment.  Change line 5 from the script above to:

$attachments = $latestlog.FullName
0
 
nextechexchadminAuthor Commented:
It seems that works! Thank you for your hard work!
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now