VMware Cloud Community
onelegtim
Contributor
Contributor
Jump to solution

PowerCLI script to list vNic types

Does anyone know of a PowerCLI script that export the following information to an Excel spreadsheet?

1. VM name

2. IP address

3. Mac address

4. Port Group

5. virtual nic type (vmxnet3, E1000, etc)

Number 1 and number 5 are the most important for us at this immediate time.  I have to identify which VMs do not have  vmxnet3 adapters, so we can change them.  Thanks in advance.

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try this

Get-VM | Select Name,
    @{N="IP addr";E={[string]::Join(',',$_.Guest.IPAddress)}},
    @{N="MAC addr";E={[string]::Join(',',($_.Guest.Nics | %{$_.MacAddress}))}},
    @{N="Portgroup";E={[string]::Join(',',($_.Guest.Nics | %{$_.NetworkName}))}},
    @{N="NIC type";E={[string]::Join(',',($_.Guest.Nics | %{$_.Device.Type}))}}


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

View solution in original post

Reply
0 Kudos
18 Replies
LucD
Leadership
Leadership
Jump to solution

Try this

Get-VM | Select Name,
    @{N="IP addr";E={[string]::Join(',',$_.Guest.IPAddress)}},
    @{N="MAC addr";E={[string]::Join(',',($_.Guest.Nics | %{$_.MacAddress}))}},
    @{N="Portgroup";E={[string]::Join(',',($_.Guest.Nics | %{$_.NetworkName}))}},
    @{N="NIC type";E={[string]::Join(',',($_.Guest.Nics | %{$_.Device.Type}))}}


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

Reply
0 Kudos
onelegtim
Contributor
Contributor
Jump to solution

Thanks, LucD.  I'm still having trouble getting what I need.  I ran the script, but it just hangs, and never completes.  I left it for four hours, and never got the results I need.  Any suggestions?   Thanks, again.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

No clue what could have gone wrong.

Let's tackle it step-by-step.

Does the following return and show results ?

Get-VM

Next step is to verify if the Guest property could be causing the hang. This properties under Guest are provided mostly by the VMware Tools.

Get-VM | select Name,@{N="Guest present";E={if($_.Guest){"OK"}else{"NOK"}}}

And then a check if the Nics property is there.

Get-VM | select Name,@{N="Nics present";E={if($_.Guest.Nics){"OK"}else{"NOK"}}}

Let's see if these get us somewhere.

Also check if some of the guests take a very long time to return with the answer.


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

Reply
0 Kudos
onelegtim
Contributor
Contributor
Jump to solution

Hey, LucD.  Thanks, again.  I'm now gettting the information I need, after trying a second time.  The problem I'm having now is that when a try >c:\vmnicst.csv, or txt, I get the following output.

Name                           Value                                          
----                           -----                                          
N                              NIC type                                       
E                              [string]::Join(',',($_.Guest.Nics | %{$_.Devi...

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You have to pipe the output to the Export-Csv cmdlet to save the results in a .csv file.

Something like this

Get-VM | Select Name,
    @{N="IP addr";E={[string]::Join(',',$_.Guest.IPAddress)}},
    @{N="MAC addr";E={[string]::Join(',',($_.Guest.Nics | %{$_.MacAddress}))}},
    @{N="Portgroup";E={[string]::Join(',',($_.Guest.Nics | %{$_.NetworkName}))}},
    @{N="NIC type";E={[string]::Join(',',($_.Guest.Nics | %{$_.Device.Type}))}} |
    Export-Csv "C:\NIC-info.csv" -NoTypeInformation    


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

onelegtim
Contributor
Contributor
Jump to solution

LucD, you're a life saver.  Thanks for your help.  

Reply
0 Kudos
harkamal
Expert
Expert
Jump to solution

How do we get vLan / subnet / gateway ?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The VLanId is easy because that is linked to the portgroup.

But for the subnet and gateway you would have to query the guest OS with Get-VMGuestNetworkInterface afaik.

And that requires passing credentials which is not so straightforward in this one-liner.

Get-VM | Select Name,
    @{N="IP addr";E={[string]::Join(',',$_.Guest.IPAddress)}},
    @{N="MAC addr";E={[string]::Join(',',($_.Guest.Nics | %{$_.MacAddress}))}},
    @{N="Portgroup";E={[string]::Join(',',($_.Guest.Nics | %{$_.NetworkName}))}},
    @{N="NIC type";E={[string]::Join(',',($_.Guest.Nics | %{$_.Device.Type}))}},
    @{N="VLANid";E={(Get-VirtualPortGroup -Name $_.NetworkName -VM $_).VLanId}} |
Export-Csv "C:\NIC-info.csv" -NoTypeInformation    


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

Reply
0 Kudos
harkamal
Expert
Expert
Jump to solution

Get-VirtualPortgroup takes 1~2 seconds for each vm, 100 vm = 2 minutes ++

why "Get-View -viewtype Network" does not show the vlan. (takes 3 seconds for entire vc)

Is there a way to know what api's a cmdlet is calling, cmdlets are slow for a large environment. I am trying to cache view locally and then use it for viobjects as i use them.

basically the mandate is reduce vmware script execution time, and so i am posting mad questions 😄

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

With the Onyx project you see a number of APIs behind the cmdlets.

I posted a short overview on the latest version in Taking the new Onyx 2.0 for a spin


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

Reply
0 Kudos
harkamal
Expert
Expert
Jump to solution

I tried connecting powercli client to onyx and fire up the cmdlet before I posted that question.

Get-VirtualPortgroup does not return anything on onyx screen, i could see lot activity with vi client though. Is there a secret setting to see what cmdlets do ?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you unselect the property collector methods under <Settings><Output> ?


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

Reply
0 Kudos
harkamal
Expert
Expert
Jump to solution

Onyx crashes upon unselecting some properties, which ones have you un-selected specifically to see cmdlet activity ?

Thanks sooooo much Smiley Happy

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

To see the activity of most Get- cmdlets, I only unselect the RetrieveProperties method.

Sometimes unselecting the CreatePropertyCollector can also be useful.


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

Reply
0 Kudos
rpiercey
Contributor
Contributor
Jump to solution

I used both scripts to get a  list of vNics by Device Type

Get-VM | select Name,@{N="Nics present";E={if($_.Guest.Nics){($_.Guest.Nics | %{$_.Device.Type})}else{"NOK"}}}

Reply
0 Kudos
cnidus
Contributor
Contributor
Jump to solution

Thanks LucD for posting the short script, helped me find the "nic.connected" parameter I was searching for.

Onyx looks really interesting too. Smiley Happy

Cheers.

Doug

Douglas Youd Senior Virtualization Engineer zettagrid
Reply
0 Kudos
qwert1235
Enthusiast
Enthusiast
Jump to solution

Luc,

Any idea why do I getting error:

Exception calling "Join" with "2" argument(s): "Value cannot be null.

Parameter name: value"

At :line:7 char:37

+ @{N="MAC addr";E={[string]::Join( <<<< ',',($_.Guest.Nics | %{$_.MacAddress}))}}

Looks like I can grab only IP info, but nothing else.

UPD: actually, looks like I cannot grab MacAddress on the VMs that are "NOK" (see below). Can you please answer on 2 questions below:

and two more questions:

1. why after running

Get-VM

| select Name,@{N="Nics present";E={if($_.Guest.Nics){"OK"}else{"NOK"

}}}

I have few VMs with NOK? I checked them and they look fine with me: OS installed, NICs present and etc...

2. What is the simplest way to check for duplicate MAC Address on VMs on VC?

Thanks a lot,

qwert

Message was edited by: qwert1235

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

1. The information about the NICs and the Disks inside the guest is retrieved through the VMware tools.

If VMware Tools is not installed or if the guest hasn't been powered on for a longer time, this information is not available.

2. See  check for duplicate Mac Address on VC


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

Reply
0 Kudos