VMware Cloud Community
BenLiebowitz
Expert
Expert

Script to migrate VMs from one cluster to another



I need help with this script as it's not working properly for me...  


My environment is currently running 4.1 and I have a need to migrate VMs from Cluster 1 running the Nexus 1000v to Cluster 2 running a normal vDS switch.  When i attempt to do this via the VI client, I'm forced to remove the network adapter, migrate the VM and then re-add the network adapter back...  This makes me lose my network config in windows.  I came up with this script that will shutdown the VMs in a specific folder, remove them from inventory, re-add them to inventory in on a host in the 2nd cluster, change the VM Portgroup and then power the VM back on.  However, I'm running into a problem with the 2nd half of the script, it's not able to locate the VMX file.  


Any help would be greatly appreciated!  


Thanks!


Ben 

-


  1. *** Part 1 ***</div>

  1. export list of VMs to Migrate to CSV</div>


Get-Folder -Name "Migrate" | get-vm | Select Name, @{N="Datastore";E={get-datastore -VM $_}},@{N="Network";E={get-virtualportgroup -VM $_}}| export-csv "c:\migrate.csv" -NoTypeinformation


  1. Get VMs to Migrate</div>


$migrate = Get-Folder -Name "Migrate"


  1. Shutdown VMs (cleanly)</div>


$migrate | Get-VM | Shutdown-VMGuest -Confirm:$false


  1. Wait for VMs to shutdown</div>


start-sleep -s 60


  1. Remove VMs from Inventory</div>


Get-Folder -Name "Migrate" | Get-Inventory | Remove-Inventory -Confirm:$false


  1. *** Part 2 ***</div>

  1. Set a host in Cluster2 to add them to.</div>


$esxhost = "VMHost10.domain.com"


    1. Add backto inventory &amp; Change network for VMs</div>


foreach ($row in (Import-Csv c:\Migrate.csv)) {



    $vmName = $row.Name



    $vmxfile = '[$row.Datastore] $vmName/$vmName.vmx'



    $vmNewVM = New-VM -VMFilePath $vmxfile -VMhost $esxhost


   

  1. Change Network Portgroup</div>


    get-vm -VM $row.Name | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $row.Network


   

  1. Poweron VMs</div>


    Start-VM -VM $row.Name -Confirm:$false | wait-tools  



} ## end foreach

Ben Liebowitz, VCP vExpert 2015, 2016, & 2017 If you found my post helpful, please mark it as helpful or answered to award points.
44 Replies
bliebowitz
Contributor
Contributor

That worked, it only returned the VM level folder, not the full path.  How do I know it's displaying the correct Migrate folder? 

Results: 

Name                           Type

----                                ----

Migrate                        VM

- Ben Liebowitz, VCP
0 Kudos
bliebowitz
Contributor
Contributor

That worked!!!  You're the MAN!!!!

- Ben Liebowitz, VCP
0 Kudos
LucD
Leadership
Leadership

Are you saying that the CSV file only has the VM's name under the Path property ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
bliebowitz
Contributor
Contributor

No, the CSV has the full exported path.

I put your new VI-Property into the main script and ran it.  The VMs shutdown, removed from inventory, re-added to inventory, moved to the folder they came from, changed their network and powered on successfully.

Right now, our first cluster is using the Nexus 1000v and, we've had occasional problems with it in the past, so the 2nd cluster is running the regular VDS and we needed an easy way to migrate VMs in a pinch.  This way, if there's a problem occurring, I can pop folders into the script and run it to migrate VMs from one cluster to another and have them put back into the same folder location they came from.  Smiley Happy

Thank you VERY much for all the assistance!

- Ben

- Ben Liebowitz, VCP
0 Kudos
LucD
Leadership
Leadership

You're welcome Smiley Happy


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference