Solved

send the results of a batch file or process via email

Posted on 2016-08-04
9
153 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
  • 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 84

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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. 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 53

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 53

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Set OWA language and time zone in Exchange for individuals, all users or per database.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

791 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