VMware Cloud Community
mrray
Enthusiast
Enthusiast
Jump to solution

Set-NetworkAdapter for multiple vmnics in a single VM

Hi all,

 

I have been tasked with a (somewhat unusual) migration between vCenters, where I basically have to power down VMs and re-register them in the new vCenter.
What I need help figuring out is how to set the PortGroup parameter for VMs with multiple nics?

 

Here is what I have (yes, this is rudimentary, bear with me) :

#Get the info we need:


Get-VM "VMName"

Select Name,

    @{E={$_.ExtensionData.Config.Files.VmPathName};L="VM Path"},

    @{N='PortgroupName';E={(Get-NetworkAdapter -VM $_).NetworkName -join '|'}},

    @{N='PortgroupVlanId';E={(Get-VirtualPortGroup -Name (Get-NetworkAdapter -VM $_).NetworkName -VMHost $_.VMHost).VlanId -join '|'}}


Disconnect-VIServer "Old-vCenter.name"


#Now, to make the new VM in the proper place

Connect-Viserver "New-vCenter.name"
 

#Register VM

New-VM -VMFilePath         "[DATASTORE] VMName/VMname.vmx" -VMHost esxi.hostname.net


#Set the proper PG for our VM

Get-Networkadapter -VM         VMName | Set-NetworkAdapter -PortGroup PG_whatever -Confirm:$false

Now, this works just fine for VMs with one NIC, but VMs with multiple NICs fail…

Since most of these are set up with multiple NICS, is there a way to do this?

 

Let me know if there is any more info needed?
 

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Ah, ok.
In that case you could do

Import-Csv -Path .\vm-migrate.csv -UseCulture |
Group-Object -Property VM -PipelineVariable vm |
ForEach-Object -Process {
  New-VM -VMFilePath $vm.Group[0].VMX -VMHost <target-ESXi-node>
  $vm.Group | ForEach-Object -Process {
    $pg = Get-VirtualPortGroup -Name $_.PortgroupName -VMHost <target-ESXi-node>
    Get-NetworkAdapter -VM $vm.Name -Name $_.vNIC |
    Set-NetworkAdapter -Portgroup $pg
  }
}


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

View solution in original post

6 Replies
LucD
Leadership
Leadership
Jump to solution

You could use a CSV file to contain the migration info

To export

Get-VM | Get-NetworkAdapter |
Select @{N='VM';E={$_.Parent.Name}},
    @{N='VMX';E={$_.Parent.ExtensionData.Config.Files.VmPathName}},
    @{N='vNIC';E={$_.Name}},
    @{N='PortgroupName';E={$_.NetworkName}},
    @{N='PortgroupVlanId';E={(Get-VirtualPortGroup -Name $_.NetworkName -VMHost $_.Parent.VMHost).VlanId}} |
    Export-Csv -Path .\vm-migrate.csv -NoTypeInformation -UseCulture


To import

Import-Csv -Path .\vm-migrate.csv -UseCulture |
Group-Object -Property VM -PipelineVariable vm |
ForEach-Object -Process {
  New-VM -VMFilePath $vm.Group[0].VMX -VMHost <target-ESXi-node>
  $vm.Group | ForEach-Object -Process {
    Get-NetworkAdapter -VM $vm.Name -Name $_.vNIC |
    Set-NetworkAdapter -Portgroup <target-PG>
  }
}


Note that you will have to find some logic for the target portgroup.
Or do all vNICs on all VMs go to the same portgroup?


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

Reply
0 Kudos
raymove
Contributor
Contributor
Jump to solution

..

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

In that case you will have to include some logic to assign the correct portgroup on the destination.


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

Reply
0 Kudos
mrray
Enthusiast
Enthusiast
Jump to solution

Wait, do you mean port groups with the same name in the new vCenter?
The port group names pre and post migration is identical, meaning I shold be OK.

Thank you.

 

 

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ah, ok.
In that case you could do

Import-Csv -Path .\vm-migrate.csv -UseCulture |
Group-Object -Property VM -PipelineVariable vm |
ForEach-Object -Process {
  New-VM -VMFilePath $vm.Group[0].VMX -VMHost <target-ESXi-node>
  $vm.Group | ForEach-Object -Process {
    $pg = Get-VirtualPortGroup -Name $_.PortgroupName -VMHost <target-ESXi-node>
    Get-NetworkAdapter -VM $vm.Name -Name $_.vNIC |
    Set-NetworkAdapter -Portgroup $pg
  }
}


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

mrray
Enthusiast
Enthusiast
Jump to solution

Thank you!

Reply
0 Kudos