VMware Cloud Community
wiery
Contributor
Contributor

move-vm script placing port groups on wrong adapters for VMs with multiple nics

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

0 Kudos
7 Replies
LucD
Leadership
Leadership

Did you already try with

$sourcePortGroup= Get-VirtualPortGroup -Name (Get-NetworkAdapter -VM $vm).NetworkName


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

0 Kudos
wiery
Contributor
Contributor

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

  • Scenario 1 – Same port group associated to All Nics
  • Scenario 2 – Same port group associated to two of three Nics
  • Scenario 3 – Different port group associated with all nics
0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
wiery
Contributor
Contributor

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. 

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
wiery
Contributor
Contributor

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}

0 Kudos
LucD
Leadership
Leadership

Correct, in fact you can make that part a bit more concise and avoid the hard-coded 4 vNICs.
Something like this for example

$networkadapters = @()

$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