I am newbie in powershell. :( But I am configuring mail notifications using powershell with the gmail mail server, everything is ok but I want to add the output of a script in the mail body.
This is the image of the command to use
$username = 'test@gmail.com'
$password = '*****'
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$body = C:\Users\esanchez\Desktop\script.ps1
$hash = @{
from = "test@gmail.com"
to = "receptor@gmail.com"
subject = "test"
smtpserver = "smtp.gmail.com"
port = "587"
body = GU
credential = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
usessl = $true
verbose = $true
}
Send-MailMessage -body $body @hash
Does C:\Users\esanchez\Desktop\script.ps1
contain the script shown in the image? If so I think you're close. Just change that line to either include | Out-String
| Out-String
or | ConvertTo-Html -Fragment
| ConvertTo-Html -Fragment
at the end so it converts your output into something you can send in the body and then update the body in $hash
to use $body
. Finally fix the actual command line
$username = 'test@gmail.com'
$password = '*****'
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$body = C:\Users\esanchez\Desktop\script.ps1 | Out-String # or | ConvertTo-Html -Fragment
$hash = @{
from = "test@gmail.com"
to = "receptor@gmail.com"
subject = "test"
smtpserver = "smtp.gmail.com"
port = "587"
body = $body # update to use $body
credential = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
usessl = $true
verbose = $true
}
Send-MailMessage @hash # remove body parameter here since you are already passing it in with the hashtable
Hello if everything works correct but only with Out-String
$body = C:\Users\esanchez\Desktop\script.ps1 | Out-String
but when i use
$body = C:\Users\esanchez\Desktop\script.ps1 | ConvertTo-Html -Fragment
I have a error, How could I format the mail?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.