DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • 13 Impressive Ways To Improve the Developer’s Experience by Using AI
  • How Web3 Is Driving Social and Financial Empowerment
  • Structured Logging
  • The Role of AI and Programming in the Gaming Industry: A Look Beyond the Tables

Trending

  • 13 Impressive Ways To Improve the Developer’s Experience by Using AI
  • How Web3 Is Driving Social and Financial Empowerment
  • Structured Logging
  • The Role of AI and Programming in the Gaming Industry: A Look Beyond the Tables
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Step-by-Step: Live Migrate Multiple (Clustered) VMs in One Line of PowerShell - Revisited

Step-by-Step: Live Migrate Multiple (Clustered) VMs in One Line of PowerShell - Revisited

Keith Mayer user avatar by
Keith Mayer
·
Mar. 03, 14 · Interview
Like (0)
Save
Tweet
Share
8.96K Views

Join the DZone community and get the full member experience.

Join For Free

A while back, I wrote an article showing how to Live Migrate Your VMs in One Line of Powershell between non-clustered Windows Server 2012 Hyper-V hosts using Shared Nothing Live Migration.  Since then, I’ve been asked a few times for how this type of parallel Live Migration would be performed for highly available virtual machines between Hyper-V hosts within a cluster. 

In this article, we’ll walk through the steps of doing exactly that … via Windows PowerShell on Windows Server 2012 or 2012 R2 or our FREE Hyper-V Server 2012 R2 bare-metal, enterprise-grade hypervisor in a clustered configuration.

Wait! Do I need PowerShell to Live Migrate multiple VMs within a Cluster?

Well, actually … No.  You could certainly use the Failover Cluster Manager GUI tool to select multiple highly available virtual machines, right-click and select Move | Live Migration …

image
Failover Cluster Manager – Performing Multi-VM Live Migration

But, you may wish to script this process for other reasons … perhaps to efficiently drain all VM’s from a host as part of a maintenance script that will be performing other tasks.

Can I use the same PowerShell cmdlets for Live Migrating within a Cluster?

Well, actually … No again.  When VMs are made highly available resources within a cluster, they’re managed as cluster group resources instead of being standalone VM resources.  As a result, we have a different set of Cluster-aware PowerShell cmdlets that we use when managing these cluster groups.  To perform a scripted multi-VM Live Migration, we’ll be leveraging three of these cmdlets:

  • Get-ClusterNode,
  • Get-ClusterGroup and
  • Move-ClusterVirtualMachineRole

Now, let’s see that one line of PowerShell!

Before getting to the point of actually performing the multi-VM Live Migration in a single PowerShell command line, we first need to setup a few variables to handle the "what" and "where" of moving these VMs. 

First, let’s specify the name of the cluster with which we’ll be working.  We’ll store it in a $clusterName variable.

$clusterName = read-host -Prompt "Cluster name"

Next, we’ll need to select the cluster node to which we’ll be Live Migrating the VMs.  Lets use the Get-ClusterNode and Out-GridView cmdlets together to prompt for the cluster node and store the value in a $targetClusterNode variable.

$targetClusterNode =
  Get-ClusterNode -Cluster $clusterName |
  Out-GridView -Title "Select Target Cluster Node" `
  -OutputMode Single

image

And then, we’ll need to create a list of all the VMs currently running in the cluster. 

We can use the Get-ClusterGroup cmdlet to retrieve this list.  Below, we have an example where we are combining this cmdlet with a Where-Object cmdlet to return only the virtual machine cluster groups that are running on any node except the selected target cluster node.  After all, it really doesn’t make any sense to Live Migrate a VM to the same node on which it’s currently running! Winking smile

$haVMs =
  Get-ClusterGroup -Cluster $clusterName |
  Where-Object {($_.GroupType -eq "VirtualMachine") `
  -and ($_.OwnerNode -ne $targetClusterNode.Name)}

We’ve stored the resulting list of VMs in a $haVMs variable.

Ready to Live Migrate!

OK … Now we have all of our variables defined for the cluster, the target cluster node and the list of VMs from which to choose.  Here’s our single line of PowerShell to do the magic …

$haVMs |
  Out-GridView -Title "Select VMs to Move" –PassThru |
  Move-ClusterVirtualMachineRole -MigrationType Live `
  -Node $targetClusterNode.Name -Wait 0

image

Proceed with care: Keep in mind that your target cluster node will need to have sufficient available resources to run the VM's that you select for Live Migration. Of course, it's best to initially test tasks like this in your lab environment first.

Here’s what is happening in this single PowerShell command line:

  • We’re passing the list of VMs stored in the $haVMs variable to the Out-GridView cmdlet. 
  • Out-GridView prompts for which VMs to Live Migrate and then passes the selected VMs down the PowerShell object pipeline to the Move-ClusterVirtualMachineRole cmdlet. 
  • This cmdlet initiates the Live Migration for each selected VM, and because it’s using a
    –Wait 0 parameter, it initiates each Live Migration one-after-another without waiting for the prior task to finish.

As a result, all of the selected VMs will Live Migrate in parallel, up to the maximum number of concurrent Live Migrations that you’ve configured on these cluster nodes. The VMs selected beyond this maximum will simply queue up and wait their turn.  Unlike some competing hypervisors, Hyper-V doesn't impose an artificial hard-coded limit on how many VMs for you can Live Migrate concurrently.  Instead, it's up to you to set the maximum to a sensible value based on your hardware and network capacity.

Do you have your own PowerShell automation ideas for Hyper-V?

Feel free to share your ideas in the Comments section below.

See you in the Clouds!

- Keith

PowerShell cluster Hyper-V Virtual Machine clustered

Published at DZone with permission of Keith Mayer, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • 13 Impressive Ways To Improve the Developer’s Experience by Using AI
  • How Web3 Is Driving Social and Financial Empowerment
  • Structured Logging
  • The Role of AI and Programming in the Gaming Industry: A Look Beyond the Tables

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: