Automation

 View Only
  • 1.  Detailed Network Information - Revisited

    Posted Apr 26, 2011 09:15 PM
      |   view attached

    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

    Attachment(s)

    ps1
    get_hostnic4.ps1   1023 B 1 version


  • 2.  RE: Detailed Network Information - Revisited

    Posted Apr 27, 2011 05:38 AM

    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.



  • 3.  RE: Detailed Network Information - Revisited

    Posted Apr 27, 2011 05:48 PM
      |   view attached

    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.

    Attachment(s)

    ps1
    get_detailednetwork.ps1   1 KB 1 version


  • 4.  RE: Detailed Network Information - Revisited
    Best Answer

    Posted Apr 28, 2011 08:47 AM

    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


  • 5.  RE: Detailed Network Information - Revisited

    Posted Apr 28, 2011 01:33 PM
      |   view attached

    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.

    Attachment(s)

    ps1
    get_detailednetwork2.ps1   1 KB 1 version


  • 6.  RE: Detailed Network Information - Revisited

    Posted Apr 28, 2011 01:39 PM

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



  • 7.  RE: Detailed Network Information - Revisited

    Posted Mar 14, 2014 01:24 AM

    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.

    Attachment(s)

    zip
    NetworkINfoScript1.txt.zip   764 B 1 version
    zip
    networkscript.txt.zip   921 B 1 version