PowerShell: Take VM Snapshots

# takeVmSnapshot.ps1

$snapShotLabel="Snapshot $(get-date -Format yyyyMMddTHHmmss)"

function takeVmSnapshot([string[]]$vmName,[string]$snapshotLabel){
    $vmClusterNodes=Get-ClusterGroup|?{$_.GroupType -eq 'VirtualMachine'}
    foreach ($vm in $vmName){
        $ownerNode=($vmClusterNodes|?{$vm -eq $_.Name}).OwnerNode
        if($ownerNode.State -eq 'Up'){
                Checkpoint-VM -VMName $vm -SnapshotName $snapShotLabel
            invoke-command -ComputerName $ownerNode.Name -ScriptBlock $setSnapShotCommand -Args $vm,$snapShotLabel
            write-warning "$vm is currently owned by DOWNED node $($ownerNode.Name). No snapshots can be taken for such guest VM."
            write-warning "$vm is does not match any Hyper-V Host in this cluster $((get-cluster).Name)"

takeVmSnapshot $vmNames $snapShotLabel

Leave a Reply

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