Solved

send the results of a batch file or process via email

Posted on 2016-08-04
9
70 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 83

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
 
LVL 16

Expert Comment

by:FOX
ID: 41742701
Test it with the send mail command with out the attachment switch
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 51

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 51

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Synchronize a new Active Directory domain with an existing Office 365 tenant
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

706 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

18 Experts available now in Live!

Get 1:1 Help Now