Powershell Hyper-V Script : Live Migration With Task Menu
####################################################################################################
# MoveVMinBulk.ps1
#
# v01 September 2012 by Kévin KISOKA
#
# Usage :
# Menu action :
#
# Select options 1 or 2 to execute LVM and option 3 to abort operation , Default option is set
# to Safely exit the script without taking any action on your running environment.
#
# - Only the VMs running on Cluster Node or Full Cluster are moved .
# - The script go through Powershell Fail Over Cluster module to get VMs and migrates them to
# another Node .
# - Remember Network & Hardware perfomance decreases when migrating concurrently large number of VMs.
# - To calculate how many possible concurrent live migration are possible in your environment do:
#
# Number of nodes\2
#
#
#
#
#####################################################################################################
[System.Console]::ForegroundColor = [System.ConsoleColor]::White
clear-host
write-host
write-host Script for Moving VMs in bulk
write-host Usage Informations : Inputs = SourceServer,targetserver,cluster group `r -foregroundcolor:Yellow
write-host
write-host Please choose one of the following: -foregroundcolor:Green
write-host
write-host '1) Move All Started VMs on single Node to another node'
write-host '2) Move All Started VMs on All The Cluster to another node Take care of target Hostperfomances'
write-host '3) Exit the program'
$menu = Read-Host "Select an option [1-3]"
write-host "Usage Informations : Inputs = SourceServer,targetserver,cluster group.`r"
-foregroundcolor:Yellow
switch ($menu) {
1{
# Inputs
$source = read-host What is source Host NodeName
$destination = read-host What is destination Host NodeName
$cluster = read-host What is ClusterGroup Name
# import the commands from clustering module
import-module FailoverClusters
# Get all of the VMs currently hosted on the source server
$vmssorted = Get-ClusterGroup -cluster $cluster| ? {$_.OwnerNode -like $source} | ?{ $_ | Get-ClusterResource | ?{
$_.ResourceType -like "Virtual Machine" } }
# Get Current states of VMs
$vmssortedstate = $vmssorted.state
# Move all imported VMs if they are in running state
foreach ($vm in $vmssorted)
{
if ($vmssortedstate -eq "Online")
{
write-host Number of VMs to move : ($vm).Count
write-host Moving VMs ******************************************* -foregroundcolor:green
Get-Cluster $cluster | Move-ClusterVirtualMachineRole -Name $vm -node $destination -Wait 30
}
Elseif ($vmssortedstate -ne "Online")
{
Write-host Migration of All VMs Aborted ... `r -foregroundcolor:Yellow
Move-ClusterVirtualMachineRole "$vm" -Cancel
}
}
}
2{
# Inputs
$source = read-host What is source Host NodeName
$destination = read-host What is destination Host NodeName
$cluster = read-host What is ClusterGroup Name
# import the commands from clustering module
import-module FailoverClusters
# Get All of the VMs hosted on all cluster Group
$vmsall = Get-ClusterGroup | ?{ $_ | Get-ClusterResource | ?{ $_.ResourceType -like "Virtual Machine" } }
# Get Current states of VMs
$vmsallstate = $vmsall.state
foreach ($vm in $vmsall)
{
if ($vmsallstate -eq "Online")
{
write-host Number of VMs to move : ($vm).Count
write-host Moving VMs ******************************************* -foregroundcolor:green
Get-Cluster $cluster | Move-ClusterVirtualMachineRole -Name $vm -node $destination -wait 30
}
Elseif ($vmsallstate -ne "Online")
{
Write-host "Migration of All VMs Aborted ... `r" -foregroundcolor:Yellow
Move-ClusterVirtualMachineRole "$vm" -Cancel
}
}
}
3{
# Getting Out of the script
exit
}
default{
# Getting Out of the script like option 3
Write-host Press Enter key to confirm exit -foregroundcolor:Yellow -nonewline
exit
}
}