I'm back again for more help, this time with producing output in an email. I can get output in the console, but I cannot get the same output in an email.
I found and modified the script posted below. It searches your snapshots, puts them in an array, and then compares their creation date and snapshot count against what you set. All of that works great and produces the following:
![results.jpg]()
I want this same output sent in an email. I want it to look exactly like what you see in the picture. I can't figure it out. What I can get is an email with a body that says:
*
89
That's it. I don't know if it's because of how I declared $Summary, $MaxAge, and $MaxCount, or if the problem is with my email parameters. Could someone take a look and see what I'm doing wrong? I would greatly appreciate it.
# vCenter server name or IP
$vcenter = 'MyVCServer'
# Max number of snapshots to allow per VM
$maxsnapcount = 2
# Max age of any snapshot to allow in days
$maxsnapage = 200
# Display all VM messages? Setting to $false lists only VMs with warnings
$displayall = $false
# Establish credentials
$Username = "domain\adminuser"
$SecurePassword = Get-Content C:\automation\creds.txt | ConvertTo-SecureString
$UserCredential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$SecurePassword
#region Code
# cls
### Static Variables
# Array containing all snapshots found
[array]$allsnapshots = $null
# Current date minus snapshot max date
$date = (Get-Date).AddDays(-1 * $maxsnapage)
### Gather Inventory
Connect-ViServer -server $vcenter -Credential $UserCredential | Out-Null
$vms = Get-VM | Get-View
Foreach ($vm in $vms)
{
If ($vm.Snapshot -ne $null)
{
# Inventory the VM snapshots
[array]$vmsnapshots = Get-VM -Id $vm.MoRef | Get-Snapshot
# Add the snapshots to the master array
$allsnapshots += $vmsnapshots
# Check to see if the VM has exceeded the max snapshot age
$MaxAge = Foreach ($snap in $vmsnapshots)
{
If ($snap.Created -le $date) {Write-Host -ForegroundColor:Red "[Old Snapshots] $($vm.name) contains an old snapshot named ""$($snap.Description)"" created on $($snap.Created)"}
}
# Check to see if the VM has exceeded the max snapshot count
$MaxCount = If (($vmsnapshots.count) -gt $maxsnapcount)
{
Write-Host -ForegroundColor:Red "[Max Snapshots] $($vm.name) has $($vmsnapshots.count) snapshots";
}
}
ElseIf ($displayall -eq $true) {Write-Host -ForegroundColor:Green "$($vm.name) has no snapshots"}
}
# Summary Information
Write-Host "==============================`nSummary Information`nFound $($vms.count) VMs`nFound $($allsnapshots.count) snapshots"
#endregion
# Email information
# Specify who gets notified
$notificationto = "me@company.com"
$notificationcc = "him@company.com"
# Specify where the notifications come from
$notificationfrom = "notification@company.com"
# specify the SMTP server
$smtpserver = "blah-blah-company.com"
# Summary
$summary = "==============================
`nSummary Information
`nFound $($vms.count) VMs
`nFound $($allsnapshots.count) snapshots"
# Send email
# Declare some mail message params
$mailMessageParams = @{
Body = $Summary, $MaxAge, $MaxCount | ConvertTo-Html | Out-String
To = $notificationto
# CC = $notificationcc
From = $notificationfrom
Subject = "VM Snapshot Report"
SmtpServer = $smtpServer
}
#
# Send email
Send-MailMessage @mailMessageParams -BodyAsHtml
# Disconnect from server
Disconnect-ViServer -Server $vcenter -Confirm:$false