VMware Cloud Community
AstraMonti
Enthusiast
Enthusiast
Jump to solution

Get device ID of new nic

Hi all.

I am writing a script that will read a CSV file with a list of VMs and add an additional nic with a specified portgroup. 
Is there a way I can get the device ID of the new nic at creation time so I can include it in the rollback script in case things go south? 

Thanks.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

1) When you have the IP address and the subnetmask, you can calculate the subnet

$ipAddr = "192.168.1.1"

$mask = "255.255.255.0"


$subnet = ([IPAddress](([IPaddress]$ipAddr).Address -band ([IPAddress]$mask).Address)).IPAddressToString

$subnet

2) Then you would only need the name of the VM and the vNIC

Get-VM -Name $vmName | Get-NetworkAdapter -Name $vnicName |

Remove-NetworkAdapter -Confirm:$false

---------------------------------------------------------------------------------------------------------

Was it helpful? Let us know by completing this short survey here.


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

View solution in original post

0 Kudos
11 Replies
LucD
Leadership
Leadership
Jump to solution

When you use the New-NetworkAdapter cmdlet to add the vNIC, it will return an object for the new vNIC.

In that object you will find the Name and Id of the new vNIC.


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

0 Kudos
AstraMonti
Enthusiast
Enthusiast
Jump to solution

Thanks LucD, it only returns the name, type, network name and mac. Is there an additional switch i can add for the vNIC id?

Additionally, it's not followed by any VM name so if in the list i have 1000 vms it will get kinda messy to report it back.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The other properties are there, they are just not displayed by default.

Try something like this

Get-VM -Name MyVM | New-NetworkAdapter -Type Vmxnet3 -NetworkName MyPG |

Select @{N='VM';E={$_.Parent.Name}},Name,MacAddress,Id

If you want to see all properties that are there, you can do

Get-VM -Name MyVM | New-NetworkAdapter -Type Vmxnet3 -NetworkName MyPG |

Select *


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

0 Kudos
AstraMonti
Enthusiast
Enthusiast
Jump to solution

Thanks again LucD

Two additional questions if you don't mind.

1)   Get-VM -name $vm | Select Name, @{N="IP Address";E={@($_.guest.IPAddress[0])}}

How can I use that IP so i can create an IF on my code later to decide what portgroup to use to add the extra nic? So if my ip is on the 10.0.10.0 subnet use pg1, if at 10.10.10.0 use pg2 and so on.

2) What's in your opinion the best way to add the creation data on a file, so they can be used "as is" to have a roll back script? e.g. Remove-NetworkAdapter to remove just the one that was added.

This is what I currently have: New-NetworkAdapter -VM $vm -NetworkName $portgroup -Type Vmxnet3 -StartConnected   | Select @{N='VM';E={$_.Parent.Name}},Name,MacAddress,Id

As always, appreciate the help.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure.

1) The problem with the IP addresses is that they are collected from inside the guest OS (through the VMware Tools).
There is no direct link between a vNIC (on the VM level) and the network adapter inside the guest OS.

The best way to make that link is through the MAC address.

2) As I mentioned in 1), the MAC address is essential.

So the data you are currently collecting should be sufficient.

You can make the link to the guest OS network adapter (with netsh for example) through the MAC address.


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

0 Kudos
AstraMonti
Enthusiast
Enthusiast
Jump to solution

Thanks LucD. Maybe I didn't explain it properly.

1) I want to use the IP of the VM just in my code to decide based on the subnet what portgroup to use. Essentially, just put the IP that it is returning to a variable, and use that variable to compare it against 4 other set variables for subnets.

2) I only need the information so I can remove the nic.So If know that VM1 created nic2 and VM2 created nic3, how do i enter that in a file, so when I have to remove them, i just run something that will do: Get-VM $vm | remove-networkadapter $nic

Hope that's clearer, and thanks again.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

1) When you have the IP address and the subnetmask, you can calculate the subnet

$ipAddr = "192.168.1.1"

$mask = "255.255.255.0"


$subnet = ([IPAddress](([IPaddress]$ipAddr).Address -band ([IPAddress]$mask).Address)).IPAddressToString

$subnet

2) Then you would only need the name of the VM and the vNIC

Get-VM -Name $vmName | Get-NetworkAdapter -Name $vnicName |

Remove-NetworkAdapter -Confirm:$false

---------------------------------------------------------------------------------------------------------

Was it helpful? Let us know by completing this short survey here.


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

0 Kudos
AstraMonti
Enthusiast
Enthusiast
Jump to solution

nvm, thanks for your help LucD

0 Kudos
AstraMonti
Enthusiast
Enthusiast
Jump to solution

Actually I have one last question. Is it possible to enable the foreach loop to not wait for the task to complete before moving onto the next vm in the list? But at the same time, how do you limit so it doesn't flood the vcenter with 500 requests at the same time?

Thanks!         

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Unfortunately the New-NetworkAdapter cmdlet does not have the RunAsync switch (which is normally used for starting a task and returning immediately).

That would mean that you will need to use the vSphere API ReconfigVM_Task method directly.

If you go that path, and want to control how many of such tasks can run in parallel, you will have to keep track of the running tasks.

When the maximum number of tasks is reached, you will have to wait until starting the next task.

There should be a number of such scripts available in this community (try searching for RunAsync and maximum tasks).


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

0 Kudos
AstraMonti
Enthusiast
Enthusiast
Jump to solution

You are a star LucD​, thanks very much.

0 Kudos