Solved

send the results of a batch file or process via email

Posted on 2016-08-04
9
391 Views
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
0
Comment
Question by:jo80ge121
[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
  • 2
  • 2
  • +1
9 Comments
 
LVL 16

Expert Comment

by:FOX
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
0
 

Author Comment

by:jo80ge121
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?
0
 
LVL 85

Accepted Solution

by:
oBdA earned 500 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" `
	-BodyAsHtml

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

1
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.

 
LVL 16

Expert Comment

by:FOX
ID: 41742701
Test it with the send mail command with out the attachment switch
0
 
LVL 55

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.

https://www.experts-exchange.com/questions/25096423/Is-it-possible-to-send-an-email-with-an-attachment-via-the-command-prompt.html

http://scripts.dragon-it.co.uk/links/email-from-batch

~bp
0
 

Author Comment

by:jo80ge121
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.
echo *****PC NAME 2*****
dir "\\PCNAME2\reports\*.pdf"
0
 
LVL 55

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...

~bp
0
 

Author Comment

by:jo80ge121
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.
0
 

Author Comment

by:jo80ge121
ID: 41742983
got it -

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

Thanks oBdA
0

Featured Post

Independent Software Vendors: 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

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 strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

717 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