Multiple Network Adapters

This topic contains 3 replies, has 3 voices, and was last updated by  Helder Guzman 2 years ago.

  • Author
    Posts
  • #30891

    Helder Guzman
    Participant

    I'm running a query for network adapters. I need get some properties from the one or more adapter.

    I can retrieve one object but I can's seem to get the loop to work to get the other network adapter properties.

    Here is the code

    function Get-HGNetworkInfo{

    [cmdletbinding()]

    param( [string]$ComputerName )

    Write-Verbose "Getting Network Info.." -Verbose
    $networkData = Get-WmiObject -Computer $ComputerName Win32_Networkadapter -Credential $cred -Filter "NetEnabled=True"

    $adapter = 1
    foreach($network in $networkData){

    $props = @{ 'Adapter' = $adapter;
    'Manufacturer' = $networkData.Manufacturer;
    'Description' = $networkData.Description;
    'Connection' = $networkData.NetConnectionID;
    'SpeedGB' = [string]$networkData.Speed/1000/1000/1000 }

    $adapter += 1

    $NetworkInfo = New-Object -TypeName psobject -Property $props
    }

    $IPinfo = Get-WmiObject win32_networkadapterconfiguration -ComputerName $ComputerName -Credential $cred -Filter "IPEnabled=True"
    $winServers = 0
    $winServers = ($IPinfo.WinsPrimaryServer -join ","),($IPinfo.WinsSecondaryServer -join ",")

    $netSpeed = Get-HGNetworkSpeedDuplex -ComputerName $servername
    $IPv6 = Get-HGIPv6Status -ComputerName $servername

    Add-Member -InputObject $NetworkInfo -MemberType NoteProperty -Name 'IPAddress' -Value ($IPinfo.IPAddress -join ",") -Force
    Add-Member -InputObject $NetworkInfo -MemberType NoteProperty -Name 'Submask' -Value ($IPinfo.IPSubnet -join ",") -Force
    Add-Member -InputObject $NetworkInfo -MemberType NoteProperty -Name 'Gateway' -Value ($IPinfo.DefaultIPGateway -join ",") -Force
    Add-Member -InputObject $NetworkInfo -MemberType NoteProperty -Name 'DNSServers' -Value ($IPinfo.DnsServerSearchOrder-join ",") -Force
    Add-Member -InputObject $NetworkInfo -MemberType NoteProperty -Name 'WinServers' -Value ($winServers -join ",") -Force
    Add-Member -InputObject $NetworkInfo -MemberType NoteProperty -Name 'DomainSuffixes' -Value ($IPinfo.DNSDomainSuffixSearchOrder -join ",") -Force
    Add-Member -InputObject $NetworkInfo -MemberType NoteProperty -Name 'IPv6Enabled' -Value ($IPV6 -as [string]) -Force
    Add-Member -InputObject $NetworkInfo -MemberType NoteProperty -Name 'SpeedDuplex' -Value ($netSpeed -as [string]) -Force

    Write-Output $NetworkInfo
    }

    as you can see, this return only one item.

    can you check, and let me know what I need to change to be able to return more than one network adapter in a computer

  • #30893

    Curtis Smith
    Participant

    Well, your foreach loop only consists of this section, and each time it loops, you are overwriting the object before outputting it anywhere.

    foreach($network in $networkData){
    
    $props = @{ 'Adapter' = $adapter;
    'Manufacturer' = $networkData.Manufacturer;
    'Description' = $networkData.Description;
    'Connection' = $networkData.NetConnectionID;
    'SpeedGB' = [string]$networkData.Speed/1000/1000/1000 }
    
    $adapter += 1
    
    $NetworkInfo = New-Object -TypeName psobject -Property $props
    }
    

    Instead of ending your loop there, you probably want it to finish building out all of your data and not end it until after your Write-Output. That way you are outputting the data for the current NIC before you start building your data for the second, etc. NIC.

  • #30903

    Richard Siddaway
    Moderator

    Hi

    Your script can be simplified as below. I've also corrected a couple of errors eg using the collection instead of iteration variable in your foreach when creating the properties. The trick here is that the DEviceId property on Win32_NetworkAdapter has the same value as Index on Win32_NetworkAdapterConfiguration so you can use that to match adapters and IP data

    I've commented out the lines using your functions for speed etc and I've commented out the 2 Get-WmiObject calls as I'm running this locally. You see the original lines and swap the uncommented and commented lines

    function Get-HGNetworkInfo{
    
        [cmdletbinding()]
    
        param( [string]$ComputerName )
    
        $networkinfo = @()
    
        Write-Verbose "Getting Network Info for $computername" -Verbose
        #$networks = Get-WmiObject -Computer $ComputerName Win32_Networkadapter -Credential $cred -Filter 'NetEnabled=True'
        $networks = Get-WmiObject Win32_Networkadapter -Filter 'NetEnabled=True'
        
        $adapter = 0
        foreach($network in $networks){
    
            $id = $network.DeviceId
        
            #$IPinfo = Get-WmiObject win32_networkadapterconfiguration -ComputerName $ComputerName -Credential $cred -Filter "Index = $id"
            $IPinfo = Get-WmiObject win32_networkadapterconfiguration -Filter "Index = $id"
            $winServers = 0
            $winServers = ($IPinfo.WinsPrimaryServer -join ','),($IPinfo.WinsSecondaryServer -join ',')
    
            #$netSpeed = Get-HGNetworkSpeedDuplex -ComputerName $servername
            #$IPv6 = Get-HGIPv6Status -ComputerName $servername 
            $netSpeed = ' '
            $IPV6 = ' '
    
            $adapter += 1
    
            $props = @{ 
                'Adapter' = $adapter;
                'Manufacturer' = $network.Manufacturer;
                'Description' = $network.Description;
                'Connection' = $network.NetConnectionID;
                'SpeedGB' = [string]$network.Speed / 1GB
                'IPAddress' = $IPinfo.IPAddress -join ','
                'Submask' = $IPinfo.IPSubnet -join ','
                'Gateway' = $IPinfo.DefaultIPGateway -join ','
                'DNSServers' = $IPinfo.DnsServerSearchOrder-join ','
                'WinServers' = $winServers -join ','
                'DomainSuffixes' = $IPinfo.DNSDomainSuffixSearchOrder -join ','
                'IPv6Enabled' = $IPV6 -as [string]
                'SpeedDuplex' = $netSpeed -as [string]
            }
        
            $networkinfo += New-Object -TypeName psobject -Property $props
        }
        $networkinfo
    }
    Get-HGNetworkInfo -ComputerName $env:COMPUTERNAME
    
  • #30935

    Helder Guzman
    Participant

    Thanks that worked great!

You must be logged in to reply to this topic.