I was tasked with creating a script to email out the daily snapshots within a particular vCenter instance. So far this has been working flawlessly, but I was asked to add another column with the VM Notes section. I thought this would be an easy task, and it probably is, but I am still new to PowerShell and keep hitting roadblocks.
I tried adding another "$vmtable.columns.add($col4)" section but changing it to $col5 and then adding a $row.Notes = $vm.VM.notes within the row section but it's not populating anything. I have verified there are notes in vSphere on the respective VM's before running, but emailed results fail to include a Notes column or any Notes output.
Any direction you can provide would be much appreciated.
Set-PowerCLIConfiguration -InvalidCertificateAction ignore -Confirm:$false
Connect-VIServer HOSTNAME -user USERNAME -password **********
# create table to capture VM snapshot info
$vmtable = New-Object system.Data.DataTable "VMTable"
$col1 = New-Object system.Data.DataColumn VM,([string])
$col2 = New-Object system.Data.DataColumn Snapshot,([string])
$col3 = New-Object system.Data.DataColumn SizeMB,([string])
$col4 = New-Object system.Data.DataColumn Created,([string])
$col5 = New-Object system.Data.DataColumn VMNotes,([string])
$vmtable.columns.add($col1)
$vmtable.columns.add($col2)
$vmtable.columns.add($col3)
$vmtable.columns.add($col4)
#examine all VMs on vCenter, if snapshot found - add it to the table
ForEach ($vm in (Get-VM | Sort-Object -Property Name))
{
ForEach ($snapshot in (Get-Snapshot -VM $vm.Name | Sort-Object -Property Name))
{
$created = -split $snapshot.Created
$row = $vmtable.NewRow()
$row.VM = $vm.Name
$row.Snapshot = $snapshot.Name
$row.SizeMB = "{0:N0}" -f $snapshot.SizeMb
$row.Created = $created[0]
$vmtable.Rows.Add($row)
}
}
# Create an HTML version of the VM Table
$vmhtml = "<table><tr align='center' style='font-weight: bold'><td>VM</td><td>Snapshot</td><td>Size</td><td>Created</td></tr>"
ForEach ($row in $vmtable.Rows)
{
$vmhtml += "<tr><td>" + $row[0] + "</td><td>" + $row[1] + "</td><td align = 'right'>" + $row[2] + "</td><td>" + $row[3] + "</td></tr>"
}
$vmhtml += "</table>"
$writeTime = get-date -format "MM/dd/yyyy"
$body = "<h2 align='center'>vCenter Daily Report $writeTime</h2><hr><br /><br />Existing Snapshots:<br /><br />" + $vmhtml + "<br /><br />End of Report<br /><br />"
#+ $dshtml
# send email in HTML format
#Send-MailMessage -smtpserver $smtpserver -from $from -to $to -subject $subject -body $body -bodyashtml
$sendreport = @{
# Adding multiple recipients here
to = 'EMAIL_Address'
cc = 'EMAIL_Address'
from = 'username.com'
body = $body
subject = 'Snapshot Report'
smtpserver = 'smtp.server'
}
Send-MailMessage @sendreport -BodyAsHtml