VMware Cloud Community
pamiller21
Enthusiast
Enthusiast
Jump to solution

Output script to body of email

I am trying to output the script to the body of my email, any ideas:

#############################

# Connect to vCenter        #

#############################

add-pssnapin VMware.VimAutomation.Core

Connect-VIServer 127.0.0.1 -Protocol https -User <USER> -Password <PW>

#############################

#          Content          #

#############################

Get-VM -Name connectwise | Get-Snapshot | Remove-Snapshot -Confirm:$false

######################

# E-mail HTML output #

######################

Send-MailMessage -To "<ADDY>" -From "<ADDY>" -SMTPServer <SRV> -Subject "Snapshots Deleted!!" -Body "The snapshots are deleted from the VMs.  Have a great day!!! :)"

##############################

# Disconnect session from VC #

##############################

disconnect-viserver -confirm:$false

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this.
Note that I use splatting for the Send-MailMessage cmdlet parameters.

Get-VM | Get-Snapshot -OutVariable report | Remove-Snapshot -Confirm:$false

$sMail = @{

  From = 'lucd@lucd.info'

  To = 'lucd@lucd.info'

  Subject = 'Snapshots deleted'

  Body = $report.VM.Name | Out-String

  SmtpServer = 'mail.lucd.info'

}

Send-MailMessage @sMail


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

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

What kind of output from the script do you mean?

The Remove-Snapshot cmdlet doesn't produce any output afaik.

If you have an array with results, you will have to convert it to a string to place it in the body of an email.

Like this for example

Send-MailMessage -To "<ADDY>" -From "<ADDY>" -SMTPServer <SRV> -Subject "Snapshots Deleted!!" -Body ($report | Out-String)


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

0 Kudos
pamiller21
Enthusiast
Enthusiast
Jump to solution

What I would love is for it to at the least spit out the name of the VM that it deleted snapshots from. Is that at all possible?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this.
Note that I use splatting for the Send-MailMessage cmdlet parameters.

Get-VM | Get-Snapshot -OutVariable report | Remove-Snapshot -Confirm:$false

$sMail = @{

  From = 'lucd@lucd.info'

  To = 'lucd@lucd.info'

  Subject = 'Snapshots deleted'

  Body = $report.VM.Name | Out-String

  SmtpServer = 'mail.lucd.info'

}

Send-MailMessage @sMail


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

0 Kudos