Email Powershell script output in email body
-
19. června 2012 20:49
Hello,
I'd like email the output of my script to myself but have that in the body of the email rather than in an attachment. Can someone explain how to do this in simple terms?
Thanks,
Scott
Všechny reakce
-
19. června 2012 21:25
The two methods below work, but for whatever reason they don't work together (perhaps someone else can explain why). This solution isin't perfect, you now have to manage a file. I'm not sure of a way to blanket capture all output.
$emailFrom = "ME <me@mydomain.com>" $emailTo = "you@mydomain.com" $smtpServer = "outgoingserver.mydomain.com" $smtp = new-object Net.Mail.SmtpClient($smtpServer) $subject = "my subject" # method 1 Get-Date | Out-File -Append -FilePath "c:\test.txt" # method 2 Add-Content -Path "c:\test.txt" -Value "$(Get-date)" $body = Get-Content -Path "c:\test.txt" $MailMessage = new-object Net.Mail.MailMessage($emailFrom, $emailTo, $subject, $body) $smtp.Send($MailMessage)
-
19. června 2012 21:26
The body must be a string, or in an html format. You specify this with the -body parameter. If you have an array to return, you need to convert it to a string, like so:
$array -join "`r`n"
Grant Ward, a.k.a. Bigteddy
-
19. června 2012 21:40
Don't mean to double post, but I just thought of this. So you'd run the following and any output the getdate.ps1 script makes will be sent to you in an e-mail.
PS O:\scripts\powershell> .\email-launcher.ps1 .\getdate.ps1
# email-launcher.ps1 $emailFrom = "ME <me@mydomain.com>" $emailTo = "you@mydomain.com" $smtpServer = "outgoingserver.mydomain.com" $smtp = new-object Net.Mail.SmtpClient($smtpServer) $subject = "my subject" Invoke-Expression "$args" | Out-File "c:\test.txt" $body = Get-Content -Path "c:\test.txt" $MailMessage = new-object Net.Mail.MailMessage($emailFrom, $emailTo, $subject, $body) $smtp.Send($MailMessage)
# getdate.ps1 Get-Date
- Upravený Joey Piccola 19. června 2012 21:41
- Označen jako odpověď Yan Li_Microsoft Contingent Staff, Moderator 29. června 2012 9:20