Migrate Live Virtual Machines (In Clustered Environment):
# Connect to Hyper-V Host
$remoteHost="HYPERV01"
Enter-PsSession $remoteHost
# Move all VMs to another Hyper-V Host in a Clustered Environment
# Set Variables
$AllHyperVHosts={(Get-ClusterNode | Where { $_.State –eq "Up" }).Name | %{$_.ToLower()}}.Invoke() # Cast result as Array type
$otherHyperVHosts=$AllHyperVHosts -ne "$(($env:computername).ToLower())" # Operate on array
$allRunningVMs=Get-VM | Where { $_.State –eq "Running" } | select Name,Path
function selectHyperVHost{
$i=0
$otherHyperVHosts | %{"$i`. $_";$i++;}
do {
$hostIndex=Read-Host -Prompt 'Select the index number corresponding to target host name'
$GLOBAL:selectedHost=$otherHyperVHosts[$hostIndex]
} until ($hostIndex -lt $otherHyperVHosts.length)
}
# Recurse into array of VM names, move each vm to the other host
selectHyperVHost
$allRunningVMs | %{Move-ClusterVirtualMachineRole -Name $_.Name -Node $selectedHost}
# Drain role of this node
Suspend-ClusterNode -Name $env:computername -Target $selectedHost -Drain
# Resume roles of this suspended node
Resume-ClusterNode $env:computername
# Resume roles of all suspended nodes
Get-ClusterNode | Resume-ClusterNode -Failback Immediate
Sample Output
PS C:\Windows\system32> $allRunningVMs | %{Move-ClusterVirtualMachineRole -Name $_.Name -Node $selectedHost}
Name OwnerNode State
---- --------- -----
Berlin Aruba ClearPass Policy Manag... hv01 Online
Berlin DFS Namespace Server 01 hv01 Online
Berlin DHCP Core hv01 Online
Berlin Domain Controller 01 hv01 Online
Berlin Domain Controller 02 hv01 Online
Berlin Exchange 02 hv01 Online
Berlin File Server 01 hv01 Online
Berlin Management Server hv01 Online
MoscowIT Certificate Authority 01 hv01 Online
Reference commands:
# List Running VMs
Get-VM | Where { $_.State –eq "Running" }
Name State CPUUsage(%) MemoryAssigned(M) Uptime Status
---- ----- ----------- ----------------- ------ ------
Exchange 01 Running 1 32768 158.00:10:24 Operating normally
ADFS Running 0 4096 1.09:56:41 Operating normally
# Force shutdown of all running VMs
$allRunningVMs=(Get-VM | Where { $_.State –eq "Running" }).Name
$allRunningVMs | %{Stop-VM -Name $_ -Force}
# Move 1 VM to another Host
$otherHyperVHost="HYPERV02"
$destinationStoragePath="C:\ClusterStorage"
$vmToMove="SOMEVM"
# Unclustered VM move
Move-VM $vmToMove $otherHyperVHost-IncludeStorage -DestinationStoragePath "$destinationStoragePath`\$($vmToMove -replace '\s','')"
# Live migration
Move-ClusterVirtualMachineRole -Name $vmToMove -Node $otherHyperVHost
# Offline resource migration
Move-ClusterGroup -Name $vmToMove -Node $otherHyperVHost
# Move all VMs to another Hyper-V Host in an Unclustered Environment
$otherHyperVHost="HYPERV02"
$destinationStoragePath="C:\ClusterStorage"
$allVMs=(Get-VM).Name
# Recurse into array of VM names, move each vm to the other host with storage destination paths using labels of each VM (spaces removed)
$allVMs |%{Move-VM $_ $otherHyperVHost-IncludeStorage -DestinationStoragePath "$destinationStoragePath`\$($_ -replace '\s','')"}
Categories: