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

Notable Features of C#

This language is part of the .NET framework that can be used to build applications…

IIS Mime Types

One of the features of IIS security is to enforce file access by its associated…

Hyper-V Logon Failure: the user has no been granted the requested logon type at this computer (0x80070569)

Issue: When performing a live migration of a guest VM, this error occurs: '...Failed to…
Other project: DragonCoin.com