Automation

 View Only
Expand all | Collapse all

Get device ID of new nic

  • 1.  Get device ID of new nic

    Posted Aug 06, 2019 10:01 AM

    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.



  • 2.  RE: Get device ID of new nic

    Posted Aug 06, 2019 10:18 AM

    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.



  • 3.  RE: Get device ID of new nic

    Posted Aug 06, 2019 10:47 AM

    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.



  • 4.  RE: Get device ID of new nic

    Posted Aug 06, 2019 11:18 AM

    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 *



  • 5.  RE: Get device ID of new nic

    Posted Aug 13, 2019 11:07 AM

    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.



  • 6.  RE: Get device ID of new nic

    Posted Aug 13, 2019 11:11 AM

    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.



  • 7.  RE: Get device ID of new nic

    Posted Aug 13, 2019 11:18 AM

    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.



  • 8.  RE: Get device ID of new nic
    Best Answer

    Posted Aug 13, 2019 11:36 AM

    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.



  • 9.  RE: Get device ID of new nic

    Posted Aug 16, 2019 11:49 AM

    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!         



  • 10.  RE: Get device ID of new nic

    Posted Aug 16, 2019 12:00 PM

    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).



  • 11.  RE: Get device ID of new nic

    Posted Aug 16, 2019 12:19 PM

    You are a star LucD​, thanks very much.



  • 12.  RE: Get device ID of new nic

    Posted Aug 16, 2019 09:41 AM

    nvm, thanks for your help LucD