PowerShell: Remove Virtual Machine Snapshots in VMM

Base Cmdlets:

$vmmServer='SOMETHINGHERE'
$vmName='VMNAMEHERE'
$snapshots=Get-SCVMCheckpoint -vmmserver $vmmServer -vm $(get-scvirtualmachine $vmName)
$snapshots|%{Remove-SCVMCheckpoint -VMCheckpoint $_}

Automation:

# removeVmSnapshotsInVmm.ps1

$vmmServers=@(
    'vmm01.kimconnect.com',
    'vmm02.kimconnect.com'
)
$daysThreshold=30
$maxConfirmationsCount=3           

function removeVmSnapshotsInVmm{
    param(
        $vmmServers=$env:computername,
        $daysThreshold=30,
        $maxConfirmationsCount=3
    )
    foreach($vmmServer in $vmmServers){
        $session=new-pssession -computername $vmmServer -credential (get-credential -message "Admin Credential to $vmmServer")
        if($session.State -eq 'Opened'){
            invoke-command -session $session{
                param($daysThreshold,$maxConfirmationsCount)
            
                function confirmation($content,$testValue="I confirm",$maxAttempts=3){
                    $confirmed=$false;
                    $attempts=0;        
                    $content|write-host
                    write-host "Please review this content for accuracy.`r`n"
                    while ($attempts -le $maxAttempts){
                        if($attempts++ -ge $maxAttempts){
                            write-host "A maximum number of attempts have reached. No confirmations received!`r`n"
                            break;
                            }
                        $userInput = Read-Host -Prompt "Please type in this value => $testValue <= to confirm. Input CANCEL to skip this item.";
                        if ($userInput.ToLower() -eq $testValue.ToLower()){
                            $confirmed=$true;
                            write-host "Confirmed!`r`n";
                            break;                
                        }elseif($userInput -like 'cancel'){
                            write-host 'Cancel command received.'
                            $confirmed=$false
                            break
                        }else{
                            cls;
                            $content|write-host
                            write-host "Attempt number $attempts of $maxAttempts`: $userInput does not match $testValue. Try again or Input CANCEL to skip this item`r`n"
                            }
                        }
                    return $confirmed;
                }
                
                $snapshotsToRemove=Get-SCVMCheckpoint -vmmserver localhost|?{$_.AddedTime -lt (get-date).adddays(-$daysThreshold)}
                foreach($snapshot in $snapshotsToRemove){
                    $snapshotName=$snapshot.Name
                    $vm=$snapshot.VM
                    $vmState=$vm.Status
                    $confirmed=if($maxConfirmationsCount-- -gt 1){confirmation "Delete snapshot $snapshotName ?"}else{$true}
                    if($confirmed){
                        $ready=if($vmState -eq 'Running'){
                            $true
                        }else{
                            try{                                
                                    Repair-SCVirtualMachine -VM $vm -Dismiss                                
                                    $true
                                }catch{
                                    write-warning $_
                                    $false
                                }
                        }
                        if($ready){Remove-SCVMCheckpoint -VMCheckpoint $snapshot}                        
                    }else{
                        write-host "Snapshot $snapshotName NOT removed."
                        break
                    }
                }
            } -Args $daysThreshold,$maxConfirmationsCount
            Remove-PSSession $session
        }else{
            write-warning "Unable to connect to VMM Server $vmmServer."
        }
    }
}

removeVmSnapshotsInVmm $vmmServers $daysThreshold $maxConfirmationsCount

Leave a Reply

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