VMware Cloud Community
KapsZ28
Contributor
Contributor

PowerCLI to set multiple NICs on VM

I am moving VMs from one vCenter to another. Since we use distributed port groups, the network adapters show up as "invalid backing". For VMs with a single adapter it is easy to update with a script. What I would like to do is update all the network adapters via a script.

I am thinking there could be a couple of different ways. The VMs obviously have Network adapter 1, Network adapter 2, etc. Also the MAC address stays the same. I can't figure out any commands that work by changing the network connection based on MAC. I was able to use the following which is fine for the first adapter.

Get-VM VM | Get-NetworkAdapter | select -First 1 | Set-NetworkAdapter -NetworkName Public-Network

That changes just Network adapter 1. I could also use the -Last switch to change the last adapter. But we have VMs with 3 and 4 NICs.

I found an example on VMware's website for modifying an adapter based on MAC, but it doesn't seem to work.

Get-VM VM | Get-NetworkAdapter | Set-NetworkAdapter -MacAddress '00:50:56:a1:00:00' -WakeOnLan:$true

I figured just change -WakeOnLan to -NetworkName and I would be good, but any combination of the "Set-NetworkAdapter -MacAddress" command throws the error "The MAC address is invalid or is not in the valid range 00:50:56:00:00:00 - 00:50:56:3F:FF:FF." This doesn't really make any sense since the MAC is right.

Any suggestions?

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership

Quite a bit of questions :smileygrin:

That example with the Set-NetworkAdapter cmdlet, is not to select an adapter based on the MAC, but is used to set/change the MAC address of an adapter.

To filter out a specific vNIC you can use a Where-clause, something like

Get-VM -Name MyVM | Get-NetworkAdapter |

where{$_.MacAddress -eq '00:50:56:a1:00:00'} |

Set-NetworkAdapter -Portgroup MyPG

And you could repeat that for all the vNICs of a VM.

You could consider having this config in a CSV file, and then loop over all the rows.

The remark "invalid backing" is that because the dvSwitch these VMs are connected to, doesn't exist in the target vCenter ?

Is there a reason why you can't "migrate" the dvSwitches as well ?


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

Reply
0 Kudos
KapsZ28
Contributor
Contributor

Interesting. Based on the example I got from VMware it seemed like it was looking at the MAC to change the WakOnLAN setting for that NIC. Not that the command worked anyway. I will try the example you gave. I am using a CSV file and the script does a bunch of other things as well for the migration. It was just this part that I was getting hung up on.

The old environment is a different physical datacenter with two vCenters. It is a mess and I wanted a clean start in the new vCenter. So instead of importing 50+ distributed port groups with VLANs all over the place and 128 ports each, I created all new ones via PowerCLI.

Reply
0 Kudos
LucD
Leadership
Leadership

That should be no problem.

Just make sure the portgroups to which you want to migrate the VMs are there before you actually move the VMs.


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

Reply
0 Kudos
fnavarro7
Contributor
Contributor

KapsZ28,


I have a very similar issue and would like to know if found a solid script for this. I am migrating multiple 5.5 vCenter Servers (Windows) and consolidating them into 1 6.0 U2 vCenter Server Appliance (Linux) . 


I am using a risky method in doing so but its the quickest route to the end of the mission. At a high level the steps look like this:

  1. Export VDS from Old vCenter ( Web Client )
  2. Disconnect Hosts from Old vCenter ( scripted )
  3. Add hosts to new vCenter ( scripted )
  4. Import VDS ( Web Client )
  5. Wait for inventory aspects to sync and display properly
    • Amount of hosts listed in VDS
    • Port Groups to display accurate amount of VMs
    • VM Net adapters to display corresponding backings vs. invalid backing
  6. If the Net adapters don't go back then I have to fix all the backing manually via the VIC and a spread sheet

I am looking to run a script that takes the content of my CSV or TXT and reconfigure the backing to the port groups listed in the CSV.

I understand import-csv or import-content and point to your file but the script for committing the changes needs to grab the VM then netadapter 1 for example and point set-netadapter  -name netdapter 1 -portgroup -name what ever its port group name is.

My script is in the works and need to produce something by Friday so was wondering it there was ever any solution or script built using a netadapter name and port group name criteria vs mac address, I believe by Mac addess would work as well.

Thanks in advanced if there any good feed back on this.

Reply
0 Kudos