Posted On August 21, 2020

PowerShell: Remove Hyper-V VM Snapshots

kimconnect 0 comments
blog.KimConnect.com >> Codes , Virtualization >> PowerShell: Remove Hyper-V VM Snapshots

Hyper-V’s snapshot feature is a double-edged sword. It’s a convenient method to make a quick backups of a machine state prior to certain activities. However, the presence of check-points would sometimes interfere with the VM’s migration (under certain conditions) and restoration (via means other than using checkpoints). Therefore, it’s necessary for Admins to perform systems maintenance by removing Hyper-V snapshots from all guest VM’s on a regular basis, especially if there are enterprise backup systems already available to serve the purpose of business continuity.

# purgeVmSnapshots.ps1

$vmNames='testwindows'
$snapshotlabel='testsnapshot'

function purgeVmSnapshots([string[]]$vmName,[string]$snapshotLabel){
    # Usage:
    # a. purgeVmSnapshots $vmNames $snapShotLabel # This is to remove specific snapshot by a label
    # b. purgeVmSnapshots $vmNames # This will purse all snapshots matching VM names

    $vmClusterNodes=Get-ClusterGroup|?{$_.GroupType -eq 'VirtualMachine'}
    $results=[System.Collections.ArrayList]::new()
    foreach ($vm in $vmName){
        $ownerNode=($vmClusterNodes|?{$vm -eq $_.Name}).OwnerNode
        if($ownerNode.State -eq 'Up'){
            $removeSnapShotCommand={
                param($vm,$snapShotLabel)
                try{
                    if($snapShotLabel){
                        Remove-VMSnapshot -VMName $vm -Name $snapShotLabel -ea Stop
                    }else{
                        Remove-VMSnapshot -VMName $vm -ea Stop
                        }
                    return $true
                }catch{
                    Write-Error $_
                    return $false
                    }
                }
            $isSnapshotRemoved=invoke-command -ComputerName $ownerNode.Name -ScriptBlock $removeSnapShotCommand -Args $vm,$snapShotLabel
            $results+=[pscustomobject]@{'vmName'=$vm;'ownerNode'=$ownerNode.Name;'snapShotRemoved'=$isSnapshotRemoved}
        }elseif($ownerNode){
            write-warning "$vm is currently owned by DOWNED node $($ownerNode.Name). No snapshots can be taken for such guest VM."
            $results+=[pscustomobject]@{'vmName'=$vm;'ownerNode'=$ownerNode.Name;'snapShotRemoved'=$false}
        }else{
            write-warning "$vm is does not match any Hyper-V Host in this cluster $((get-cluster).Name)"
            $results+=[pscustomobject]@{'vmName'=$vm;'ownerNode'='None';'snapShotRemoved'=$false}
            }
    }
    return $results
}

#Checkpoint-VM -vmName $vmNames -SnapshotName $snapShotLabel
purgeVmSnapshots $vmNames $snapShotLabel

Leave a Reply

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

Related Post

Basic CSS: Use RGB values to Color Elements

<style>body {background-color: #F00;}</style>

SQL Failover (Simple Method)

$servers="SQL01","SQL02","SQL03"; # Dynamic Credential method 1 $who = whoami if ($who.substring($who.length-2, 2) -eq "-admin"){$username=$who;} else…

User Account Group Membership Copy

This is the quick snippet to be executed in the context of a Domain Administrator:…