Author Posts

October 9, 2014 at 12:13 am

I am trying to create a html report using Creating HTML Reports in PowerShell by Don Jones. What am trying to get to is I would like to get the IP address from Win32_NetworkAdapterConfiguration and NetConnectionID from Win32_NetworkAdapter

Below is the function I am using but the problem is when I output the psobject, NetConnectionID are listed together and not against the IPaddress.

For example

This is how I get

Adaptor Name IP Address Default IPGateway IPSubnet
———————————————————————————————————————————————————
Local Area Connection Local Area Connection 2 192.168.100.10 192.168.100.1 255.255.255.0
———————————————————————————————————————————————————
192.168.101.10 192.168.101.1 255.255.255.0
———————————————————————————————————————————————————

This is how I want

Adaptor Name IP Address Default IPGateway IPSubnet
———————————————————————————————————————————————————
Local Area Connection 192.168.100.10 192.168.100.1 255.255.255.0
———————————————————————————————————————————————————
Local Area Connection 2 192.168.101.10 192.168.101.1 255.255.255.0
———————————————————————————————————————————————————

Could you let me know what am doing wrong here and please help

====================================================================================================================

function Get-InfoNIC {
[CmdletBinding()]
param(
[Parameter(Mandatory=$True)][string]$ComputerName
)
$nics = Get-WmiObject -class Win32_NetworkAdapterConfiguration -ComputerName $ComputerName -Filter "IPEnabled=True"
foreach ($nic in $nics) {
$props = @{'IP Address'=$nic.IPAddress[0];
'Default IPGateway'=$nic.DefaultIPGateway[0];
'IPSubnet'=$nic.IPSubnet[0];
'Adaptor Name'=(Get-WmiObject -class Win32_NetworkAdapter -ComputerName $ComputerName | select -ExpandProperty NetConnectionID)}
New-Object -TypeName PSObject -Property $props
}
}

October 9, 2014 at 12:33 am

You need to match the adapter to the configuration which you can do by filtering in the configurations index which matches the adapters deviced id

function Get-InfoNIC {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$True)][string]$ComputerName
    )
    $nics = Get-WmiObject -class Win32_NetworkAdapterConfiguration -ComputerName $ComputerName -Filter "IPEnabled=True"
    foreach ($nic in $nics) {
        
        $index = $nic.Index
        
        $props = @{'IP Address'=$nic.IPAddress[0];
                   'Default IPGateway'=$nic.DefaultIPGateway[0];
                   'IPSubnet'=$nic.IPSubnet[0];
                   'Adaptor Name'=(Get-WmiObject -class Win32_NetworkAdapter -ComputerName $ComputerName -Filter "DeviceId = $index" | 
                                   select -ExpandProperty NetConnectionID)}
        New-Object -TypeName PSObject -Property $props
    }
}

If you don't have a default gateway set your function throws an error and you don't get anything for that adapter. This is one way to get round that issue:

function Get-InfoNIC {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$True)][string]$ComputerName
    )
    $nics = Get-WmiObject -class Win32_NetworkAdapterConfiguration -ComputerName $ComputerName -Filter "IPEnabled=True"
    foreach ($nic in $nics) {
        
        $index = $nic.Index

        if ($nic.DefaultIPGateway) {
          $gateway = $nic.DefaultIPGateway[0]
        }
        else {
          $gateway = "Not Set"
        }
        
        $props = @{'IP Address'=$nic.IPAddress[0]
                   'Default IPGateway'= $gateway
                   'IPSubnet'=$nic.IPSubnet[0]
                   'Adaptor Name'=(Get-WmiObject -class Win32_NetworkAdapter -ComputerName $ComputerName -Filter "DeviceId = $index" | 
                                   select -ExpandProperty NetConnectionID)}
        New-Object -TypeName PSObject -Property $props
    }
}

I've also tidied up the properties hash table by removing the semi colons – you don't need them when putting key-value pairs on separate lines