Function to remove old VMWare snapshots

Reffering to my last article about snapshots creation, I want to show you how to remove old VMWare snapshots.
Below function will remove snapshots which are older than provided number of days.
To work, function requires PowerCLI module to be installed on machine from which you want execute it.

NOTE:
Be aware that script will remove snapshots from all VMs to which your account has access!!

function Remove-OldVMSnapshots{
       
        param
        (
            [Parameter(Position=0, Mandatory=$true, HelpMessage="vCenter server", ValueFromPipeline = $true)]
            [string]
            $vCenter,
            [Parameter(Position=1, Mandatory=$true, HelpMessage="Port", ValueFromPipeline = $true)]
            [int]
            $Port,
            [Parameter(Position=2, Mandatory=$true, HelpMessage="Domain\Username", ValueFromPipeline = $true)] 
            [string]
            $User,
            [Parameter(Position=3, Mandatory=$true, HelpMessage="Password", ValueFromPipeline = $true)]
            [string]
            $Password,
            [Parameter(Position=4, Mandatory=$true, HelpMessage="Days back", ValueFromPipeline = $true)] 
            [int]
            $DaysBack
           
        ) 

        Try
        {
            Add-PsSnapin VMware.VimAutomation.Core
        }
        Catch
        {
            return "Error: PowerCLI module not installed or can not be loaded." 
        }

        Try
        {
            $vCenterConnection = Connect-VIServer -Server $vCenter -Port $Port -User $User -Password $Password -WarningAction Ignore
            $Date = (get-Date).addDays(-$DaysBack)
            $Snapshots = Get-VM | Get-Snapshot | Select-Object Name, Created, VM | Where-Object {$_.Created -lt $Date} 
            
            If($Snapshots)
            {
                $Results = @()
                foreach($snapshot in $Snapshots)
                {
                    $snap = Get-Snapshot -Name $snapshot.Name -VM $snapshot.VM 
                    Remove-Snapshot -Snapshot $snap -Confirm:$false
                    $Object = New-Object PSCustomObject
                    $Object | Add-Member -MemberType NoteProperty -Name "VM" -Value $snapshot.VM
                    $Object | Add-Member -MemberType NoteProperty -Name "Snapshot name" -Value $snapshot.Name
                    $Object | Add-Member -MemberType NoteProperty -Name "Snapshot creation date" -Value $snapshot.created
                    $Object | Add-Member -MemberType NoteProperty -Name "Days back date" -Value $Date
                    $Object | Add-Member -MemberType NoteProperty -Name "Status" -Value "Removed"
                    $Results += $Object
                }

                $Results | ft -AutoSize
            }

            else
            {
                Return "No snapshots found to remove."
            }
            Disconnect-VIServer $vCenterConnection -Confirm:$False
            Remove-PSSnapin VMware.VimAutomation.Core
        }
        Catch
        {
            return 'Error:' + $_.Exception.Message
        }
}

In result you should receive report with infromation about which snapshots have been removed.

I hope it will be usefull for some of you 😉
Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.