Solved

send the results of a batch file or process via email

Posted on 2016-08-04
9
121 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This article will help you understand what HashTables are and how to use them in PowerShell.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

822 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