Email Alerts When A VMware Snapshot Is Older Than A Day

IT Support Forum Forums Virtualization VMware vCenter General Discussion Email Alerts When A VMware Snapshot Is Older Than A Day

This topic contains 0 replies, has 1 voice, and was last updated by  Webmaster 1 year, 3 months ago.

  • Author
    Posts
  • #1329

    Webmaster
    Keymaster

    Here’s a great PowerShell script created by my colleague, which gets a list of snapshots from VMware vSphere that are older than a day and emails the people that created them.

    To use it in your environment, edit the domain, email from and SMTP server details, then save it as a ps1 file and set a scheduled task to run the script each day (or however often you need).

    This is a particularly useful script for environments using VMware snapshots, because if VMs have old snapshots, they grow in size and can cause problems. Such problems might include running out of space or difficulty managing the VM as a result of the presence of a large snapshot. In addition to this, snapshots that are very old take a very long time to delete, which can cause issues itself.

    ####################################################################################################################
    #Date: 25th May 2015
    #This script looks for all snapshots on the VMWare platform and gets there ID#
    #Then by using the ID number it finds the details of the snapshot (Created by whom, date, etc).
    #The script then looks up the user details in AD and gets their email address
    #Finally, it creats a table for each person and emails them a list of their snapshots.
    ####################################################################################################################
    
    ####################################################################################################################
    #Adds the Power CLI app into Powershell.
    if ((Get-PSSnapin -Name "Vmware.VimAutomation.Core" -ErrorAction SilentlyContinue) -eq $null)
    {
        Add-PSSnapin "Vmware.VimAutomation.Core"
    }
    
    #Check to see if there are any currently connected servers
    if (!($global:DefaultVIServers.Count -gt 0)) {
        Connect-VIServer "vSphereServerName"
    }
    ####################################################################################################################
    
    ####################################################################################################################
    #This line clears variables that powershell has since starting the session.
    Get-Variable -Exclude PWD,*Preference | Remove-Variable -ErrorAction SilentlyContinue
    ####################################################################################################################
    
    # 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])
    $table.columns.add($col1)
    $table.columns.add($col2)
    $table.columns.add($col3)
    $table.columns.add($col4)
    $table.columns.add($col5)
    
    #For evey snapshot get the details of it (VM Name\Creation Date\Description\User who created it).
    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 | Where-Object {
        $_.FullFormattedMessage -imatch 'Task: Create virtual machine snapshot'
      }
      #Add details of snapshot creation to the datatable.
      if ($snapevent -ne $null) {
        #$Get AD details of person who created the snapshot
        $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
        $table.Rows.Add($row)
      }
    }
    
    $UserList = $Table.Rows.User | Group-Object
    
    ForEach ($Person in $UserList) {
      $html = $Null
      $Final_System = @()
      # Create an HTML version of the DataTable
      $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:#43B2B2;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}"
      $html += "</style>"
      $html += "<table><tr><b><td>VM Name</td><td>Snapshot Description</td><td>Snapshot Creation Date</td><td>User Account</td></b></tr>"
      ForEach ($Record in $Table) {
        If ($Person.Name -eq $Record.User) {
            #Add rows from table into HTML table.
            $html += "<tr><td>" + $Record.Name + "</td><td>" + $Record.Desc + "</td><td>" + $Record.Date + "</td><td>" + $Record.User + "</td></tr>"
            $EmailAddress = $Record.Email
        }
      }
      $html += "</table>"
      
      #Email the users who have created snapshots.
      $smtpServer = "SMTPServerName"
      $msg = new-object Net.Mail.MailMessage
      $smtp = new-object Net.Mail.SmtpClient($smtpServer)
      $MailFrom = "[email protected]"
      $Mailto = $EmailAddress
      $msg.From = $MailFrom
      $msg.To.Add($Mailto) 
      $msg.Subject = "A list of Snapshots you have created."
      $msg.IsBodyHTML = $true
      $msg.body = $html
      $smtp.Send($msg)
    }

You must be logged in to reply to this topic.