Automating HCX Cutover using PowerCLI #HCX #PowerCLI #vExpert


So I was provided with a script from my buddy Charlie at Tower Associates, and then myself and Dean Lewis edited it a bit to make it work alongside the scripts we had already produced in our previous posts.

I’ve always been an advocate of doing the cutover manually. Cutting over 200 VMs or so in one go is daunting and lacks the control I would personally like. I like to be able to tell customers, ok we have cut over these 20 VMs for this app, please get them to test it, then when they are happy move to the next set. It is nice and controlled and instils confidence in the migration process.

That is not to say a mass cutover is not possible and has its use cases, and that is what this script will do for you.

If you have watched our VMworld Code session, you will have seen us show it working and give a breakdown of what it was doing:

VMworld Code Session on Automating HCX Migrations

Now let’s talk about the actual code!

Connect-HCXServer -Server <source hcx manager>
$HCXVMS = Import-CSV 'location of .csv'
foreach ($HCXVM in $HCXVMS){
$HCXVMName = $HCXVM.VM_NAME
$starttime = [datetime]::now.addminutes(2)
$endtime = [datetime]::now.addHours(12)
$migration = get-hcxmigration -id (get-hcxmigration | ?{$_.state -like 'WAIT*' -and $_.vm -match "^$HCXVMName$"} | select -expandproperty id)
if ($migration.count -eq 1){
set-hcxmigration -migration $migration -schedulestarttime $starttime -scheduleendtime $endtime 
}
}
Disconnect-HCXServer -Server <source hcx manager> -Confirm:$false 

So what this does is it looks at the CSV file, which is the same as the CSV file we have used in the previous PowerCLI posts I have done. Example Below:

This image has an empty alt attribute; its file name is image.png
Example CSV file

It looks for the name of the VM listed in the VM_NAME column and then verifies it is in the state of “WAITING FOR CUTOVER” and then after it has confirmed that, it will adjust the cutover time, to two mins from the current time. It will then cycle through the foreach loop till there are no VMs listed in the CSV file.

Pretty simple right? Also, you can control what VMs are in the csv as you see fit.


Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.