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

WordPress Code Snippet Crashed My Site

Error Message: ParseError thrown syntax error, unexpected '$', expecting variable (T_VARIABLE) Resolution:1. If still login…

PowerShell: Expand Disk Volume to Maximum Size

# expandDisk.ps1 # version 0.02 $driveLetters=(Get-wmiobject win32_volume -filter 'DriveType=3 AND DriveLetter IS NOT NULL').DriveLetter function…

PowerShell: Windows Automated Disk Cleanup

##################################################################################     <#      This script is created to automate the cleanup activity. Doing so will benefit to reduce the size of disk.     This script will perform the following   1. Clear windows temp and user temp folder   2. Empty recycle bin   3. Disk Cleanup   4. Clear CBS cabinet log files   5. Clear downloaded patches   6. Clear downloaded driver   7. Clean download folder      Note:  …
Other project: DragonCoin.com