Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


send the results of a batch file or process via email

Posted on 2016-08-04
Medium Priority
Last Modified: 2016-08-04
Hello, I have a need to have my simple batch file send me an email of the result query when ran on a scheduled task.  

The batch file is a simple dir command of a few folders to verify the files are up to date which is critical in case of downtime, adding the results to a email instead of having to manually run and view the results would help us in this process.  

The batch file is a .bat so my question is, do I move the commands to list the files of a folder to a powershell script and build off a email send or do I build a separate powershell script to run the batch and email the results?  or do I do something completely different to get me a automatic daily email with the file time stamp results of a directory
Question by:jo80ge121
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
  • 2
  • 2
  • +1
LVL 16

Expert Comment

ID: 41742686
You can add the send mail command to the bottom of your batch file.  You will need to know your smtp server, the location of where the attachment will be pulling from after your batch file is run, etc.

ref link for syntax: https://technet.microsoft.com/en-us/library/hh849925.aspx

put the batch file with the send command in a .txt file and rename it .ps1 to save it as a powershell command

Author Comment

ID: 41742695
is there any way to put the simple 6 line results output into the body of the email instead of creating an attachment?
LVL 85

Accepted Solution

oBdA earned 2000 total points
ID: 41742699
Just put it all into PS. You didn't provide your batch, so this just collects from two folders:
$FileList = Get-ChildItem 'C:\Temp', 'C:\Test' -File | Select-Object -Property FullName, LastWriteTime
Send-MailMessage `
	-Subject 'Directory Listing' `
	-Body $($FileList | ConvertTo-HTML | Out-String) `
	-From "filelist@acme.com" `
	-To "admin@acme.com" `
	-SmtpServer "smtp.acme.com" `

Open in new window

If you want to save it as .cmd for easier scheduling, add these as the first two lines of the script above:
@PowerShell.exe -Command "Invoke-Expression -Command ((Get-Content -Path '%~f0' | Select-Object -Skip 2) -join [environment]::NewLine)"
@exit /b %Errorlevel%

Open in new window

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!

LVL 16

Expert Comment

ID: 41742701
Test it with the send mail command with out the attachment switch
LVL 58

Expert Comment

by:Bill Prew
ID: 41742745
If you want to do this from the BAT script itself, then a couple of options would be to use the free BLAT utility, or leverage a VBS or PS1 script from the BAT script to actually do the emailing.  Here is a reference to an earlier solution (there are many on EE) using BLAT, and a link to one of our experts reference info on using it, or using a VBS helper script.




Author Comment

ID: 41742889
@ oBdA - it looks good but I should have said before that I needed to check a few directories in different paths.  Below is the basic manual script I run from my pc but I like the PS get-childitem format if we can keep it but also show more directories in the body of the email.  How can I add additional directory outputs as you showed me above?    

echo *****PC NAME 1*****
dir "\\PCNAME1\reports\*.pdf"
dir "\\PCNAME1\reports1\*.pdf"
echo *****PC NAME 2*****
dir "\\PCNAME2\reports\*.pdf"
LVL 58

Expert Comment

by:Bill Prew
ID: 41742925
I'm sure oBdA can hook you up with a full PS solution, but I did want to ask one question.  Based on your initial question, and now seeing your BAT script, I wonder if you could take it a step further?  Rather than just list the files and have a person manually review them, would it be possible to automate the verification piece?  Could we compare the date of certain files, or the most recent one to the current date, or some other date?  What does a human do to make the determination?  If we can script that logic, then the email could be a simple "ok" or "fail" type email, or perhaps you only email if the files are out of date - sort of exception based email.  Makes it easy for whoever has to monitor the files via the emails.  Just a thought...


Author Comment

ID: 41742944
Bill Prew - I was thinking the same when i presented this question.  

The time stamps have to be within 6 hours of the check.  If there is script logic out there to do this and present a OK or Fail message I would love to see it.  At least for now, I will take a daily email until i can find a Ok or Fail logic script.

Author Comment

ID: 41742983
got it -

get-childitem c:\temp, d:\other, e:\additional, f:\etc, "then the pipe"

Thanks oBdA

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

609 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