Posted On August 20, 2020

PowerShell: Take VM Snapshots

kimconnect 0 comments
blog.KimConnect.com >> Codes , Virtualization >> PowerShell: Take VM Snapshots
# takeVmSnapshot.ps1

$vmNames='Test-VM1','Cowabunga-VM4'
$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'){
            $setSnapShotCommand={
                param($vm,$snapShotLabel)
                Checkpoint-VM -VMName $vm -SnapshotName $snapShotLabel
                }
            invoke-command -ComputerName $ownerNode.Name -ScriptBlock $setSnapShotCommand -Args $vm,$snapShotLabel
        }elseif($ownerNode){
            write-warning "$vm is currently owned by DOWNED node $($ownerNode.Name). No snapshots can be taken for such guest VM."
        }else{
            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 *

Related Post

Arrays and Objects

Array.prototype- Allows additional properties of all array objectsArray.from- Creates a new array from an array-like…

PowerShell: Install Windows Exporter

Simple Version $windowsExporterUrl='https://github.com/prometheus-community/windows_exporter/releases/download/v0.20.0/windows_exporter-0.20.0-amd64.msi' $stageFolder='C:\Temp\' # Download the file Import-Module BitsTransfer $fileName=[regex]::match($windowsExporterUrl,'[^/\\&\?]+\.\w{3,4}(?=([\?&].*$|$))') $msiFile=join-path $stageFolder $fileName if(!(test-path…

PowerShell: Check Servers on Domain to Locate A Domain Account Being Set to Run Services and Scheduled Tasks

# Obtain the name of the default domain Administrator account (this account is expected to…