VMware Cloud Community
Walfordr
Expert
Expert
Jump to solution

Detailed Network Information - Revisited

I need to grab the following network information from VC 2.5/ESX 3.5 farm:

Hostname

vSwitch

PortGroupName

VLANID

vNIC

pNIC

MAC

IPAddress

I can pull the information individually but I have no idea how to link them together.  I would like to export to a csv or output to a table on screen.

I have used Alan's script before which give me the below.  I am just learning powercli and need some help or pointers.

HostvSwitchPNicSpeedMACDeviceIDPortIDObservedVLAN
esxlab01.mylab.labvSwitch0vmnic0100000:0c:29:9b:09:83

10.0.0.1-10.15.255.254
esxlab01.mylab.labvSwitch0vmnic1100000:0c:29:9b:09:8d

10.0.0.1-10.15.255.254
esxlab01.mylab.labvSwitch1vmnic2100000:0c:29:9b:09:97

10.0.0.1-10.15.255.254
esxlab01.mylab.labvSwitch1vmnic3100000:0c:29:9b:09:a1

10.0.0.1-10.15.255.254









I tried combining the script here http://ict-freak.nl/2011/03/27/powercli-host-networking-device-info/ to pull the other information but I am failing.

I attached my failed script.  I know the loops is wrong.

Here is the output: completely incorrect.

HostName           vNic   PortGroupName   ip         MAC               pNic vSwitch  vPGName
--------           ----   -------------   --         ---               ---- -------  -------
esxlab01.mylab.lab vswif0 Service Console 10.9.8.210 00:50:56:41:41:eb      vSwitch0 VM Network
esxlab01.mylab.lab vswif0 Service Console 10.9.8.210 00:50:56:41:41:eb      vSwitch0 VM Network
esxlab01.mylab.lab vswif0 Service Console 10.9.8.210 00:50:56:41:41:eb      vSwitch0 VM Network

Thanks,

Robert

Robert -- BSIT, VCP3/VCP4, A+, MCP (Wow I haven't updated my profile since 4.1 days) -- Please consider awarding points for "helpful" and/or "correct" answers.
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Since you apparently also want to include the virtual NICs, the script needs a few changes.

Try this version

#define array to store information 
$vmHostNicInfo = @() foreach($esx in Get-VMHost){     foreach($nic in (Get-VMHostNetworkAdapter -VMHost $esx)){         $Details = "" | Select HostName,vNic,IPAddress,MACAddress,pNic,vSwitch,vPGName, vLANID         $Details.HostName = $esx.Name         $Details.vNic = $nic.Name         $Details.IPAddress = $nic.IP         $Details.MACAddress = $nic.Mac         $Details.pNic = $nic.DeviceName         if($nic.PortGroupName){             $sw = Get-VirtualSwitch -VMHost $esx | where {(Get-VirtualPortGroup -VirtualSwitch $_ | %{$_.Name}) -contains $nic.PortGroupName}             $pg = Get-VirtualPortGroup -Name $nic.PortGroupName -VirtualSwitch $sw        }         else{             $sw = Get-VirtualSwitch -VMHost $esx | where {$_.Nic -contains $nic.Name}             if($sw){                 $pg = Get-VirtualPortGroup -virtualswitch $sw            }         }         $Details.vLANID = [string]::Join(',',($pg | %{$_.VLANID}))         if($sw){             $Details.vSwitch = $sw.Name         }         if($pg){             $Details.vPGName = [string]::Join(',',($pg | %{$_}))         }         $vmHostNicInfo += $Details    } }
#export data to screen
$vmHostNicInfo
| Format-Table -AutoSize
#export data to csv
$vmHostNicInfo
| Export-Csv -NoTypeInformation ..\output\detailnetwork_info.csv


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

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

$vmHostNicInfo = @()

foreach($esx in Get-VMHost){
    foreach($nic in (Get-VMHostNetworkAdapter -VMHost $esx -Physical)){
        $Details = "" | Select HostName,vNic,PortGroupName,ip,MAC,pNic, vSwitch, vPGName
       
$Details.HostName = $esx.Name         $Details.vNic = $nic.Name         $Details.ip = $nic.IP         $Details.MAC = $nic.Mac         $Details.pNic = $nic.DeviceName         $sw = Get-VirtualSwitch -VMHost $esx | where {$_.Nic -contains $nic.Name}         $pg = Get-VirtualPortGroup -VirtualSwitch $sw | where {$_.Extensiondata.Spec.Policy.NicTeaming.NicOrder.ActiveNic -contains $nic.Name}         if($sw){$Details.vSwitch = $sw.Name}         if($pg){$Details.vPGName = [string]::Join(',',($pg | %{$_.Name}))}         $vmHostNicInfo += $Details
    } } $vmHostNicInfo

The 2 if statements are required because you can have NICs on an ESX server that are not assigned to a vSwitch and/or portgroup.


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

0 Kudos
Walfordr
Expert
Expert
Jump to solution

LucD,

Your script ran and provided info, but some of the items are missing. IP, vPGName and PortGroupName.

HostName           vNic   PortGroupName ip MAC               pNic   vSwitch  vPGName
--------           ----   ------------- -- ---               ----   -------  -------
esxlab01.mylab.lab vmnic0                  00:0c:29:9b:09:83 vmnic0 vSwitch0
esxlab01.mylab.lab vmnic1                  00:0c:29:9b:09:8d vmnic1 vSwitch0
esxlab01.mylab.lab vmnic2                  00:0c:29:9b:09:97 vmnic2 vSwitch1
esxlab01.mylab.lab vmnic3                  00:0c:29:9b:09:a1 vmnic3 vSwitch1

It did put me on the right track but need some more slight modification.  The modified script is now pulling the portgroup, but the pg name for the vswif0 is incorrect.

Output of attached script is below:

HostName           vNic   IPAddress  MACAddress        pNic   vSwitch  vPGName                    vLANID
--------           ----   ---------  ----------        ----   -------  -------                    ------
esxlab01.mylab.lab vmnic0            00:0c:29:9b:09:83 vmnic0 vSwitch0 Service Console,VM Network 0,0
esxlab01.mylab.lab vmnic1            00:0c:29:9b:09:8d vmnic1 vSwitch0 Service Console,VM Network 0,0
esxlab01.mylab.lab vmnic2            00:0c:29:9b:09:97 vmnic2 vSwitch1 Virtual Machine Network    0
esxlab01.mylab.lab vmnic3            00:0c:29:9b:09:a1 vmnic3 vSwitch1 Virtual Machine Network    0
esxlab01.mylab.lab vswif0 10.9.8.210 00:50:56:41:41:eb vswif0          Virtual Machine Network    0

I'll continue ot work on it...but maybe you'll spot the issue right away.

Robert -- BSIT, VCP3/VCP4, A+, MCP (Wow I haven't updated my profile since 4.1 days) -- Please consider awarding points for "helpful" and/or "correct" answers.
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Since you apparently also want to include the virtual NICs, the script needs a few changes.

Try this version

#define array to store information 
$vmHostNicInfo = @() foreach($esx in Get-VMHost){     foreach($nic in (Get-VMHostNetworkAdapter -VMHost $esx)){         $Details = "" | Select HostName,vNic,IPAddress,MACAddress,pNic,vSwitch,vPGName, vLANID         $Details.HostName = $esx.Name         $Details.vNic = $nic.Name         $Details.IPAddress = $nic.IP         $Details.MACAddress = $nic.Mac         $Details.pNic = $nic.DeviceName         if($nic.PortGroupName){             $sw = Get-VirtualSwitch -VMHost $esx | where {(Get-VirtualPortGroup -VirtualSwitch $_ | %{$_.Name}) -contains $nic.PortGroupName}             $pg = Get-VirtualPortGroup -Name $nic.PortGroupName -VirtualSwitch $sw        }         else{             $sw = Get-VirtualSwitch -VMHost $esx | where {$_.Nic -contains $nic.Name}             if($sw){                 $pg = Get-VirtualPortGroup -virtualswitch $sw            }         }         $Details.vLANID = [string]::Join(',',($pg | %{$_.VLANID}))         if($sw){             $Details.vSwitch = $sw.Name         }         if($pg){             $Details.vPGName = [string]::Join(',',($pg | %{$_}))         }         $vmHostNicInfo += $Details    } }
#export data to screen
$vmHostNicInfo
| Format-Table -AutoSize
#export data to csv
$vmHostNicInfo
| Export-Csv -NoTypeInformation ..\output\detailnetwork_info.csv


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

0 Kudos
Walfordr
Expert
Expert
Jump to solution

Thanks LucD.  The updated script is perfect.

For anyone that copies it, the forum messed up the formatting.  The first variable definition is commented out and the output at the end as well.

I attached the updated version.

Robert -- BSIT, VCP3/VCP4, A+, MCP (Wow I haven't updated my profile since 4.1 days) -- Please consider awarding points for "helpful" and/or "correct" answers.
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The formatting of the script on my previous reply has been updated.


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

0 Kudos
jlcjr2k8
Contributor
Contributor
Jump to solution

I tried this script but kept getting error Exception calling join with 2 arguments: Value cannot be null Parameter name: value.

Can you look at the attached scripts and help me combine them into one script and export to csv. Both scripts give me the info I need but I don't understand how to combine them together in order to have just one script.

The info I would like are Hostname, vSwitch, Portgroup, Activenics, StandbyNics, vLan ID for each Portgroup,  vSwitchports, vSwitchportinuse, Pnic, Speed, Mac, DeviceID, PortID, Observed.

Any help you can give is appreciated.

0 Kudos