Snapshot notification Powercli

HI All,

Over the week i have prepared useful PowerCLi script that will help you to inform your End-users, IT teammates about existing snapshots on vCenter.

In my environment, the vCenter filled with some custom attributes like (VM Onwer) and using this attributes i am sending them emails everyday with he following details:

 

Assuming you have same fields you can use this for you as well.

 

Formal email:

 

 

Powershell Scripts:

Connect-VIServer "YourvCenter"

$Header = @"
<style>
BODY{font-family: Arial; font-size: 12pt;}"
TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #6495ED;}
TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
</style>
"@

$Date = Get-Date
$AllVMs = Get-VM -Location "clustername"
$owners = $AllVMs.CustomFields | Where-Object -Property key -EQ "VM Owner" | select -Unique

foreach($owner in $owners)
{
$vms = $allvms | where {$_.CustomFields.value -eq $owner.value}
$Result=foreach($vm in $vms)
{

    $VMOwner = ($VM.CustomFields | Where-Object -Property key -EQ "VM Owner").Value.Replace('@domain.com', '')
    $VMSnapshots = $VM | Get-Snapshot
     $e= $VMOwner+"@domain.com"
    foreach ($Snapshot in $VMSnapshots)
    {
        [pscustomobject]@{
            "VM Name:"                     = $VM.Name
            "VM Owner:"                    = $VMOwner
            "Snapshot Name:"              = $Snapshot.Name
            "Snapshot Description:"       = $Snapshot.Description
            "Total Snapshots in this VM:" = ($VMSnapshots |  Measure-Object).Count
            "Date Creation:"              = $Snapshot.created
            "Snapshot age (Days):"         = [System.Math]::Round( ($Date - $Snapshot.Created).TotalDays / 1)
            "Snapshot SizeGB"            = [System.Math]::Round($Snapshot.SizeGB, 1)
            "Total Snapshots SizeGB:"     = [System.Math]::Round( ($VMSnapshots | Measure-Object -Sum SizeGB).Sum, 1)

 
}


   }
}
$Email = $Result| ConvertTo-Html -Head $Header 
$e= ($VM.CustomFields | Where-Object -Property key -EQ "VM Owner").value

  if($result -eq "" -or $result -eq $null)
  {
write host "no snap on $vm"
}
else
{
Send-MailMessage -Subject "Your VM's have snapshot!" -Body " <h1 style=color:black;font-size:20px;>Dear, $VMOwner</h1><span  style=color:black;font-size:16px;> We notice that the following VM includes snapshots, <br />
this means that snapshots are utilized storage capacity and degraded the server performance.<br /> Please login to the vCenter and delete unnecessary snapshots, otherwise, you can send us email at [email protected] and we'll do it for you. <br /> <br />  - If snapshot is required, send us email to exclude this vm from alert.<br /><br /> Thank you very much.<br /><br /></p> </span ></style> $Email " -BodyAsHtml -SmtpServer "EmailServer" -From [email protected] -to  $e
}
}