PowerCLI – vMotion VM between clusters

Shares

Thanks to my college Sebastian, here his linkedin profile , I have an opportunity to post very nice PowerCLI script. Script do vMotion VM between clusters \ vDS switches and storage.

What script does:

  1. Import list of the virtual machines to migrate
  2. reconfigure VM to use standard switch
  3. migrate VM to new cluster
  4. reconfigure VM to use new vDS switch and portgroup on new cluster

[box type=”info”]

  • variable $vCenter is not needed when you work only with one vCenter server
  • clusters belongs to different virtual distributed switches
  • storage between clusters isn’t shared

[/box]

#Author: Sebastian Barylo
#general scope variables, customize migration targets (vhost, datastore, portgroup)if needed

$batch_input = "<path to input file>"
$vCenter = 'vc01.lab.local'
$target_vhost = Get-vmhost -name esx02.lab.local -server $vCenter
$target_datastore = get-datastore -name DS01 -server $vCenter
$target_pg = get-vdswitch -name DV01 -server $vCenter | get-vdportgroup -name 'PG-01' -server $vCenter

#load names of vms to be processed from file

$vm_names = Get-Content $batch_input

#for each name, retrieve vm object and process it

foreach ($vm_name in $vm_names) {

	$vm = get-vm -name $vm_name -server $vCenter

	#reconfigure vnic to standard vswitch to enable migration

	get-networkadapter -vm $vm -name "Network adapter 1" -server $vCenter |
	set-networkadapter -networkname 'VM Network' -connected:$false -confirm:$false|
	Out-Null

	#(s)vmotion the vm

	move-vm -vm $vm -destination $target_vhost -datastore $target_datastore -server $vCenter |
	Out-Null

	#retrieve the vm object again and connect it to target vdswitch, set-networkadapter is invoked twice
	#because it doesn't seem to allow connecting to vdswitch and changing link state to "connected" in one go (bug?)

	get-vm -name $vm_name -server $vCenter |
	get-networkadapter -name "Network adapter 1"|
	set-networkadapter -connected:$true -confirm:$false|
	set-networkadapter -portgroup $target_pg -confirm:$false|
	Out-Null

	Write-Host "$vm_name --!!!DONE!!!---"

}

 

0 0 votes
Article Rating

Artur Krzywdzinski

Artur is Consulting Architect at Nutanix. He has been using, designing and deploying VMware based solutions since 2005 and Microsoft since 2012. He specialize in designing and implementing private and hybrid cloud solution based on VMware and Microsoft software stacks, datacenter migrations and transformation, disaster avoidance. Artur holds VMware Certified Design Expert certification (VCDX #077).

You may also like...

Subscribe
Notify of
guest
4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Rostislav Soukup

Hi Artur,
can I use your script to vmotion VMs between 2 vcenters using vDS? When I tried to
migrate VM, powershell says that move-vm is supported only on one VI-Server.

I will explain my situation: due to several upgrades, my vCenter db performs bad, so I
decided
to start almost from scratch.I have deployed a new vCenter, now I am
trying copy VI structures (vm folders, roles, etc..) and than migrate VMs –
they are using vDS

Rostislav

4
0
Would love your thoughts, please comment.x
()
x

FOR FREE. Download Nutanix port diagrams

Join our mailing list to receive an email with instructions on how to download 19 port diagrams in MS Visio format.

NOTE: if you do not get an email within 1h, check your SPAM filters

You have Successfully Subscribed!

Pin It on Pinterest