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
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
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
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?
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