PowerShell script to get VM’s restarted by HA in last 24 hours on Mail.

################## Snapshot Report ##################
#Import VMware moduld
Get-Module -Name VMware* -ListAvailable | Import-Module
 
#Connect to our vCenter Server using the logged in credentials
Connect-VIServer <vCenterIP/FQDN> -User '<Username@domain.com>' -Password '<password>'

write-host "Connected to vCenter, collecting details please wait...."
$head = @'
<style>
body { background-color:#FFFFFF;
       font-family:calibri;
       font-size:11pt; }
td, th { border:1px solid black; 
         border-collapse:collapse; }
th { color:white;
     background-color:black; }
table, tr, td, th { padding: 2px; margin: 0px }
table { margin-left:50px; }
</style>
'@
$Output = Get-VIEvent -MaxSamples 100000 -Start (Get-Date).AddDays(-1) -Type Warning | Where {$_.FullFormattedMessage -match “restarted”} |select CreatedTime,FullFormattedMessage | sort CreatedTime –Descending | ConvertTo-Html -PreContent "<h1>VM's Restarted by HA in last 24 hours </h1>"
$finalout = ConvertTo-HTML -head $head -PostContent $Output | Out-String
 
#Send email to Admins

$smtpServer = "<IP Address>"
$smtpFrom = "email@domain.com"
$smtpTo =  $smtpTo =  "email@domain.com,email@domain.com"
$messageSubject = "List of VM's restarted by HA in last 24 Hrs"
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true
$message.Body = "<head><pre>$style</pre></head>"
$message.Body += $finalout
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)

#DissConnect from our vCenter Server
#disconnect-viserver -confirm:$false

Leave a comment