Powershell output in Email is Possible
-
I am getting the Date output in powershell is possible to come in Email(Outlook) through .bat ?
-
This is to automate the mail automatically and click the link from the site.Can anyone help on the same
-
I'm a little confused. You want a batch file to call a powershell script that generates an email with the date inserted in to it?
-
But not skip the .bat and just use PowerShell?
-
You can use the Send-Mailmessage command to do this in PS.
-
@mike-davis said in Powershell output in Email is Possible:
I'm a little confused. You want a batch file to call a powershell script that generates an email with the date inserted in to it?
My idea is to call the powershell from .bat to which the output should be inserted in the mail.And then going to internal site to download the excel file automatically!
-
@lakshmana said in Powershell output in Email is Possible:
@mike-davis said in Powershell output in Email is Possible:
I'm a little confused. You want a batch file to call a powershell script that generates an email with the date inserted in to it?
My idea is to call the powershell from .bat to which the output should be inserted in the mail.And then going to internal site to download the excel file automatically!
But why, once you have PowerShell, you never need to use .bat files again. Why use two languages when you can use just one?
-
What would be calling the script? As Scott says, just call the .ps1 and forget about using a .bat
-
I do that in a few places. A .bat file that calls a .ps1. And within that .ps1, I use this to send an email:
# Gets date: $emailDate = Get-Date # -------------------- Specify Email Variables Below ------------------------- # # Specifies who the notification email goes to: $emailTo = "[email protected]" # Specifies who the notification email comes from: $emailFrom = "Server Alert <[email protected]>" # Specifies the Subject of the notification email: $emailSubject = "Your email subject" # Specifies the Body of the notification email: $emailBody = "Body content in HTML format... how about an $emailDate.<br><br>Attached is a copy of the attachment.<br><br>More text:<br><br><font face='tahoma' size='5'>$servList</font>" # Specifies the attachment to include in the notification email: $emailAttachment = "$dir\$latestBUErrorLog" # Specifies the name of the SMTP Server to use: $emailSMTPServer = "smtpServer" # Specifies the SMTP Server Port to use: $emailSMTPServerPort = "25" # ------------------- End Email Variables ------------------------------------ # Send-MailMessage -To "$emailTo" -From "$emailFrom" -Subject "$emailSubject" -Body "$emailBody" -BodyAsHtml -Attachments "$emailAttachment" -SmtpServer "$emailSMTPServer" -Port "$emailSMTPServerPort"
And of course, you'll need to define those variables in the script before that line.
Edit: I'm including the variable definitions so Jared doesn't yell at me.
-
@tim_g This script in Powershell is good.How to call this from .Bat ?
I am in start command but the powershell closes automatically! Any suggestion here -
I need .bat because i will schedule this job in Windows Scheduler
-
If you are using Task Scheduler, then you call powershell.exe and run the ps1 script as an option of the command. Just make sure you run it as an administrator.
-
@lakshmana said in Powershell output in Email is Possible:
I need .bat because i will schedule this job in Windows Scheduler
You don't need BAT for that.
-
@lakshmana said in Powershell output in Email is Possible:
@tim_g This script in Powershell is good.How to call this from .Bat ?
I am in start command but the powershell closes automatically! Any suggestion hereBatch files are "deprecated", don't use an old approach to do new things.
-
Then need to save this file and how to run this powershell command automatically ?any suggeations?
-
@lakshmana said in Powershell output in Email is Possible:
Then need to save this file and how to run this powershell command automatically ?any suggeations?
I'm confused. What's the question? Just use PowerShell instead of BATCH. This is the purpose of PowerShell.
-
@scottalanmiller How to automate this powershell according some time interval in the machine ?
-
@lakshmana said in Powershell output in Email is Possible:
@scottalanmiller How to automate this powershell according some time interval in the machine ?
With the Task Scheduler.
https://technet.microsoft.com/en-us/library/cc748993(v=ws.11).aspx
-
Ok Thanks
-
@nerdydad said in Powershell output in Email is Possible:
If you are using Task Scheduler, then you call powershell.exe and run the ps1 script as an option of the command. Just make sure you run it as an administrator.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@lakshmana said in Powershell output in Email is Possible:
Then need to save this file and how to run this powershell command automatically ?any suggeations?