Mail overview of snapshots to owners

When you allow users to create and delete snapshots for Virtual Machines, it can become very time consuming reminding them of cleaning up their own snapshots in case they forgot to do it themselves. This script can help you out here by sending an email reminder to them for you.
You can create a scheduled task to run this script daily.

# Set some variables that are used in this script
$vCenterServer = "vcenter.contoso.com"
$smtpServer = "mail.contoso.com"
$mailFrom = "vcenter@contoso.com"
$DomainName = "CONTOSO"

#################################################################
# Connect to VMware vCenter Server
#################################################################

# Adds the Power CLI app to Powershell.
if ($null -ne (Get-Module -ListAvailable -Name "VMware.VimAutomation.Core")) {
    Import-Module "VMware.VimAutomation.Core"
}

# Connect to the vCenter Server
if (!($global:DefaultVIServers.Count -gt 0)) {
    Connect-VIServer $vCenterServer
}

# Create a datatable
$table = New-Object system.Data.DataTable "Table"
$col1 = New-Object system.Data.DataColumn Name, ([string])
$col2 = New-Object system.Data.DataColumn User, ([string])
$col3 = New-Object system.Data.DataColumn Email, ([string])
$col4 = New-Object system.Data.DataColumn Desc, ([string])
$col5 = New-Object system.Data.DataColumn Date, ([string])
$col6 = New-Object system.Data.DataColumn Age, ([string])
$col7 = New-Object system.Data.DataColumn Size, ([string])
$table.columns.add($col1)
$table.columns.add($col2)
$table.columns.add($col3)
$table.columns.add($col4)
$table.columns.add($col5)
$table.columns.add($col6)
$table.columns.add($col7)

# Check every Virtual Machine for any existing Snapshots
foreach ($snap in $(Get-VM | Get-Snapshot)) {
    $snapevent = Get-VIEvent `
    -Entity $snap.VM `
    -Types Info `
    -Start $Snap.Created.AddMinutes(-1) `
    -Finish $Snap.Created.AddMinutes(1) `
    -MaxSamples 1

    # Add details of snapshot creation to the datatable.
    if ($null -ne $snapevent) {
        # Lookup the email address of the snapshot owner in Active Directory
        $Usermail = Get-ADUser ($($snapevent.UserName.replace("$DomainName\", ""))) -Properties Mail
        #Add data to the table.
        $row = $table.NewRow()
        $row.Name = $snap.vm
        $row.User = $snapevent.UserName
        $row.Email = $Usermail.Mail
        $row.Desc = $snap
        $row.Date = $snap.Created
        $row.Age = ((Get-Date) - $snap.Created).Days
        $row.Size = $snap.SizeGB -as [int]
        $table.Rows.Add($row)
    } 
}

$UserList = $Table.Rows.User | Group-Object
ForEach ($Person in $UserList) {
    $html = $Null
    # Create an HTML version of the DataTable
    $html += "This is a reminder for you to show you which snapshots still exist that you created before.<BR>"
    $html += "Please keep an eye on these snapshots yourself.<BR>"
    $html += "You will keep receiving this email until you remove the snapshot(s).<BR><BR>"
    $html += "<style>"
    $html += "BODY{background-color:white;}"
    $html += "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
    $html += "TH{border-width: 0px;width:300;cellspacing=0 ;padding: 10px;border-style: solid;border-color: black;background-color:#68a2ff;font-family: Verdana;font-size:13 }"
    $html += "TD{border-width: 0px;width:300;cellspacing=3 ;padding: 10px;border-style: solid;border-color: black;text-align: left;background-color:white;font-family: Verdana;font-size:11; text-align:center}"
    $html += "</style>"
    $html += "<table><tr><b><th>VM Name</th><th>Snapshot Description</th><th>Snapshot Creation Date</th><th>Snapshot Age (Dagen)</th><th>Snapshot Size (GB)</th><th>Account Name</th><th>Email Address</th></b></tr>"
    ForEach ($Record in $Table) {
        If ($Person.Name -eq $Record.User) {
            #Add rows from table into HTML table.
            $EmailAddress = $Record.Email
            $html += "<tr><td>" + $Record.Name + "</td><td>" + $Record.Desc + "</td><td>" + $Record.Date + "</td><td>" + $Record.Age + "</td><td>" + $Record.Size + "</td><td>" + $Record.User + "</td><td>" + $EmailAddress + "</td></tr>"
        }
    } $html += "</table>"

    # Email the users who have created snapshots.
    $msg = new-object Net.Mail.MailMessage
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    $Mailto = $emailAddress
    $msg.From = $mailFrom
    $msg.To.Add($mailto)
    $msg.Subject = "Reminder: Overview of snapshots created by you"
    $msg.IsBodyHTML = $true
    $msg.body = $html
    $smtp.Send($msg)
}