VMware Cloud Community
EXPRESS
Enthusiast
Enthusiast
Jump to solution

output to body of email

Is their a way to write an Out-File or Export- information directly to the body of an email. So instead of getting an attachment which I would then have to open on my blackberry I would like to simply open the email and see the information in the body.

This is what I use now and it sends me the attachment, which is fine. I rather have the information put straight into the body of the email.

send-mailmessage

-SmtpServer "smtp.abc.com" `

-from NoBody DoNotReply@noreply.com `

Thank you, Express
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The 2nd script use the Get-Content cmdlet to read the content of the file and redirects it to the Body of the email.

The first script directed the content of the variable to the Body of the email.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
12 Replies
LucD
Leadership
Leadership
Jump to solution

Suppose your report is in variable $report then you can do

$emailFrom = "<from-addr>" 
$emailTo
= "<to-addr>"
$subject = "Your subject"
$body
= $report | Out-String $smtpServer = "<smtp server>"
$smtp
= New-Object Net.Mail.SmtpClient($smtpServer) $smtp.Send($emailFrom,$emailTo,$subject,$body)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
EXPRESS
Enthusiast
Enthusiast
Jump to solution

Thanks for the quick reply, I am getting an email with the path of the logfile:

So in the body all i see is this;

Path                                                                                                                      

----                                                                                                                      

C:\logs\VM_s_12-14-2010_09.06AM.log                               

                        

Thank you, Express
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, if the variable contains the path to the file, then you could do it like this

$emailFrom = "<from-addr>" 
$emailTo
= "<to-addr>"
$subject = "Your subject"
$body = Get-Content -Path $report | Out-String
$smtpServer
= "<smtp server>"
$smtp = New-Object Net.Mail.SmtpClient($smtpServer) $smtp.Send($emailFrom,$emailTo,$subject,$body)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
EXPRESS
Enthusiast
Enthusiast
Jump to solution

Sry I lost you there, isnt that the same thing as the first example?

Thank you, Express
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The 2nd script use the Get-Content cmdlet to read the content of the file and redirects it to the Body of the email.

The first script directed the content of the variable to the Body of the email.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
EXPRESS
Enthusiast
Enthusiast
Jump to solution

LucD,

My bad my screen only saw the last three lines of the script. I do see the entire script now. 

Tested and of course you know it worked! Thanks so much.

Would you be able to point me to where I can find some information on formating the body, the information sent is good but not aligned on the email.

thanks again.

Thank you, Express
Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You can use the ConvertTo-Html cmdlet to convert the report to html and send it as a html e-mail. Like this:

# Set the SMTP Server address
$SmtpServer = "mysmtpserver.mydomain.local"
# Set the Email address to recieve from
$EmailFrom = "me@mydomain.local"
# Set the Email address to send the email to
$EmailTo = "me@mydomain.local"

$Body = Get-VM | Select-Object -property Name,PowerState,NumCpu,MemoryMB | ConvertTo-Html 
$Subject = "Get-VM Report"

$mailer = new-object Net.Mail.SMTPclient($SmtpServer)
$msg = new-object Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$msg.IsBodyHTML = $true
$mailer.send($msg)

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
EXPRESS
Enthusiast
Enthusiast
Jump to solution

I like that nice idea, I will try it and get back you...

Thanks in advance.

Thank you, Express
Reply
0 Kudos
EXPRESS
Enthusiast
Enthusiast
Jump to solution

Worked like a charm, thanks for that.

How can I go about editing the list not to include certain VM's? So for instance I run the following script and part of the results have about 10 VM's which I know will be there all the time because they are test VM's. So how can I exclude them?

Is that possible?

Here's the Script which Rob helped me set this up.

Get-VM |

`

Select-Object

-Property Name,@{N="State";E={$_.Guest.State}},@{N="ToolsStatus";E=$_.Guest.ExtensionData.ToolsStatus}} | `

Where-Object {$_.ToolsStatus -eq "toolsNotRunning" -or $_.ToolsStatus -eq "toolsOld" -or $_.ToolsStatus -eq "toolsNotInstalled"} | ConvertTo-Html

so i.e. vm names are vm-abc, vm-xyz

thanks again,

Thank you, Express
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can filter out these VMs with an additional Where-Object cmdlet.

Something like this

$excludedVMs = "vm1","vm2","vm3"

Get-VM |`
    where {$excludedVMs -notcontains $_.Name} | `
    Select-Object -Property Name,         @{N="State";E={$_.Guest.State}},         @{N="ToolsStatus";E={$_.Guest.ExtensionData.ToolsStatus}} | `
    Where-Object {$_.ToolsStatus -eq "toolsNotRunning" -or $_.ToolsStatus -eq "toolsOld" -or $_.ToolsStatus -eq "toolsNotInstalled"} | ConvertTo-Html


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

EXPRESS
Enthusiast
Enthusiast
Jump to solution

Thanks for the quick response I am testing now.

Thank you, Express
Reply
0 Kudos
EXPRESS
Enthusiast
Enthusiast
Jump to solution

Perfect! thanks it worked.

Thank you,

Thank you, Express
Reply
0 Kudos