Hi Friends,
Trying to write a script to move VMs in mass from one vcenter to another using the vm-move cmdlet. It works great excepts on Vms with multiple nics. It is not associating the correct port group of the DVS to the correct Nic.
foreach ($vm in $sourcehostVMS)
{
$networkadapters=@()
$networkadapters=Get-NetworkAdapter -vm $vm
$networkadapters | Format-Table -AutoSize
$sourcePortGroup=($vm | Get-VirtualPortGroup).name
$destinationPortgroup=@()
$destinationPortgroup=Get-VirtualPortGroup -Server $destinationvcenter -Name $sourcePortGroup
$destinationPortgroup | Format-Table -AutoSize
$SourceDatastore=(get-vm $vm -Server $sourcevcenter | get-datastore).name
$destinationDatastore=(Get-Datastore -Name $SourceDatastore -Server $destinationvcenter).name
Move-VM -VM $vm -Server $destinationvcenter -Destination $destinationhost -Datastore $destinationDatastore -NetworkAdapter $networkadapters -PortGroup $destinationPortgroup
}
This is the output prior to actually running the move-vm command. For example the vm-net-853 is getting associated with NIC 2 instead of 3
Name Type NetworkName MacAddress WakeOnLanEnabled
---- ---- ----------- ---------- ----------------
Network adapter 1 Vmxnet3 Corp|DB|DB-Back 00:50:56:be:ff:eb True
Network adapter 2 Vmxnet3 vm-net-438 00:50:56:be:9a:dc True
Network adapter 3 Vmxnet3 vm-net-853 00:50:56:be:e2:f6 True
Name Key VLanId PortBinding NumPorts
---- --- ------ ----------- --------
Corp|DB|DB-Back dvportgroup-153 Static 8
vm-net-853 dvportgroup-17 Static 512
vm-net-438 dvportgroup-16 Static 1024
Any help is appreciated
Did you already try with
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I think the issues I am having is when trying to the port group on the destination vcenter . Since the Vm doesn’t exist it is just grabbing the ports groups and ordering them in the way they are ordered on the DVS and not in the way they are ordered on the VM.
VM move problems I am trying to solve:
VM with multiple Nics
So you are experiencing behavior that does not correspond with what is described in Spotlight on the Move-VM Cmdlet including PowerCLI 6.5 Enhancements ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Since I am moving 2000 + VMs I am trying to find a way so that I do not have to do hundreds of possible combinations manually.
In that example is there a way to take the static values in the array for $destinationportgroup and have it automatically populated in the correct order from the values in $network adapters.
You could work with a translation table (that could be a hash table).
Can you create a table with the 'old' portgroups and to which 'new' portgroups they should be mapped?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
If this statement in the article is true I think this may work
"One key thing to note is that the first item in each array will be referenced together, then the second items will be referenced together, and so on and so forth"
$sourcehostVMS= get-vm -Server $sourcevcenter | where name -Match wiery-test
$nic1='Network adapter 1'
$nic2='Network adapter 2'
$nic3='Network adapter 3'
$nic4='Network adapter 4'
foreach ($vm in $sourcehostVMS){
$networkadapters=@()
$networkadapters=Get-NetworkAdapter -vm $vm
$networkadapters | Format-Table -AutoSize
$destinationPortgroup=@()
if ($networkadapters.name -eq $nic1){$destinationPortgroup += Get-VirtualPortGroup -Server $destinationvcenter -Name (Get-NetworkAdapter -VM $vm | where name -EQ $nic1).NetworkName}
if ($networkadapters.name -eq $nic2){$destinationPortgroup += Get-VirtualPortGroup -Server $destinationvcenter -Name (Get-NetworkAdapter -VM $vm | where name -EQ $nic2).NetworkName}
if ($networkadapters.name -eq $nic3){$destinationPortgroup += Get-VirtualPortGroup -Server $destinationvcenter -Name (Get-NetworkAdapter -VM $vm | where name -EQ $nic3).NetworkName}
if ($networkadapters.name -eq $nic4){$destinationPortgroup += Get-VirtualPortGroup -Server $destinationvcenter -Name (Get-NetworkAdapter -VM $vm | where name -EQ $nic4).NetworkName}
$destinationPortgroup | Format-Table -AutoSize
$SourceDatastore=(get-vm $vm -Server $sourcevcenter | get-datastore).name
$destinationDatastore=(Get-Datastore -Name $SourceDatastore -Server $destinationvcenter).name
Move-VM -VM $vm -Server $destinationvcenter -Destination $destinationhost -Datastore $destinationDatastore -NetworkAdapter $networkadapters -PortGroup $destinationPortgroup}
Correct, in fact you can make that part a bit more concise and avoid the hard-coded 4 vNICs.
Something like this for example
$destinationPortgroup = Get-NetworkAdapter -VM $vm | Sort-Object -Property Name |
ForEach-Object -Process {
$networkadapters += $_
Get-VirtualPortGroup -Name $networkadapters[-1].NetworkName -Server $destinationvcenter
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference