Hi,
I was looking at a post from a few months back Scheduled maintenance mode script with email status report
I tweaked it a bit to powerdown VM's before placing the host into Maintenance Mode
(couldn't vMotion them as the Hosts in this case are not part of a cluster)
Here's what I've come up with.
This works except the body of the email received is
"*
26"
I was expecting "ServerName OK", this is the content of the $report variable
$hostlist = Get-VMHost ($serverlist)
$sMaint = @{
enable = $true
timeout = 60
}
$report = @()
# If Host is in a Cluster, vMotion VM's to other Hosts in the Cluster
<# work in progress #>
# If Host is standalone, powerdown any running VM's
Foreach ($vmhost in $hostlist)
{
foreach ($vm in $vmhost)
{
$v = $vm | Get-VM
if ($v.PowerState -match 'PoweredOn' )
{
Shutdown-VMGuest $v -Confirm:$false -ErrorAction SilentlyContinue
}
}
}
# Put Host into Maintenance Mode
Foreach ($server in $serverlist)
{
$esxcli = Get-EsxCli -VMHost $server -V2
if($esxcli.system.maintenanceMode.set.Invoke($sMaint))
{
$report += "$($server) OK"
}
else
{
$report += "$($server) NOK"
}
}
# Email a report
$sMail = @{
To = 'Jason.Pierce2@emc.com'
From = 'Jason.Pierce2@emc.com'
Subject = 'Scheduled Maintenance Mode'
Body = $report | ConvertTo-Html | Out-String
BodyAsHtml = $true
SmtpServer = 'mailhub.lss.emc.com'
}
Send-MailMessage @sMail
How do I correct the email body content ?
Try creating objects with your text, instead of simple strings.
Something like this
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try creating objects with your text, instead of simple strings.
Something like this
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference