Network Adapter Loop

This topic contains 17 replies, has 5 voices, and was last updated by Profile photo of Stephen Owen Stephen Owen 1 year, 10 months ago.

  • Author
    Posts
  • #21715
    Profile photo of Chris Lindauer
    Chris Lindauer
    Participant

    I have been struggling for a week trying to get this to work. I need some help on how to do this looping correctly. I want to get all the properties listed for each adapter if possible. Here is the code I am trying to work with.

    #Network Configuration Start

    $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
    foreach ($network in $networks) {
    $AdapterDesc = $Network.Description
    $IPAddress = $Network.IpAddress
    $SubnetMask = $Network.IPSubnet
    $DefaultGateway = $Network.DefaultIPGateway
    $DNSServers = $Network.DNSServerSearchOrder
    $DNSDomain = $Network.DNSDomain
    $DNSSuffix = $Network.DNSDomainSuffixSearchOrder
    $FullDNSREG = $Network.FullDNSRegistrationEnabled
    $WINSLMHOST = $Network.WINSEnableLMHostsLookup
    $WINSPRI = $Network.WINSPrimaryServer
    $WINSSEC = $Network.WINSSecondaryServer
    $DOMAINDNSREG = $Network.DomainDNSRegistrationEnabled
    $DNSEnabledWINS = $Network.DNSEnabledForWINSResolution
    $TCPNETBIOSOPTION = $Network.TcpipNetbiosOptions
    $IsDHCPEnabled = $false
    If($network.DHCPEnabled) {
    $IsDHCPEnabled = $true
    }
    }

    $Networkprops = Get-NetAdapter -Name * -CimSession $computer
    foreach ($Networkprop in $Networkprops) {
    $AdapterName = $networkprop.name
    $Status = $networkprop.status
    $LinkSpeed = $networkprop.linkspeed
    $Driverinformation = $networkprop.driverinformation
    $MACAddress = $Network.MACAddress
    }

    $OutputObj = New-Object -Type PSObject
    $OutputObj | Add-Member -MemberType NoteProperty -Name AdapterName -Value $AdapterName
    $OutputObj | Add-Member -MemberType NoteProperty -Name AdapterDesc -Value $AdapterDesc
    $OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress
    $OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress
    $OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask
    $OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value $DefaultGateway
    $OutputObj | Add-Member -MemberType NoteProperty -Name "DNSServers (10.1.1.21,10.2.1.22)" -Value $DNSServers
    $OutputObj | Add-Member -MemberType NoteProperty -Name "DNSDomain (wfm.pvt)" -Value $DNSDomain
    $OutputObj | Add-Member -MemberType NoteProperty -Name "DNSSuffix (wfm.pvt,wholefoods.com)" -Value $DNSSuffix
    $OutputObj | Add-Member -MemberType NoteProperty -Name "DNS Register Connection Address in DNS (True)" -Value $FULLDNSREG
    $OutputObj | Add-Member -MemberType NoteProperty -Name "DNS Use Connection DNS Suffix in DNS (False)" -Value $DOMAINDNSREG
    $OutputObj | Add-Member -MemberType NoteProperty -Name "WINSPrimaryServer" -Value $WINSPRI
    $OutputObj | Add-Member -MemberType NoteProperty -Name "WINSSecondaryServer" -Value $WINSSEC
    $OutputObj | Add-Member -MemberType NoteProperty -Name "WINS EnableLMHostsLookup (False)" -Value $WINSLMHOST
    $OutputObj | Add-Member -MemberType NoteProperty -Name "WINS Netbios Setting (Disabled=2)" -Value $TCPNETBIOSOPTION
    $OutputObj | Add-Member -MemberType NoteProperty -Name "Disable NetBIOS over TCPIP (False)" -Value $DNSENABLEDWINS
    $OutputObj | Add-Member -MemberType NoteProperty -Name "Port Status" -Value $Status
    $OutputObj | Add-Member -MemberType NoteProperty -Name LinkSpeed -Value $LinkSpeed
    $OutputObj | Add-Member -MemberType NoteProperty -Name DriverInfo -Value $Driverinformation

    #Network Configuration End

    $OutputObj

  • #21720
    Profile photo of Matt McNabb
    Matt McNabb
    Participant

    What problems are you experiencing?

  • #21721
    Profile photo of Will Anderson
    Will Anderson
    Keymaster

    Hey there Chris,

    Give the PSCustomObject a whirl. You can loop everything through per computer, order it the way you want, and it's a little cleaner to read.

    $ComputerName = "server01","server03"

    ForEach($Computer in $ComputerName){
    $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
    $Networkprops = Get-NetAdapter -Name * -CimSession $Computer
    [PSCustomObject]@{
    AdapterDesc = $Networks.Description
    IPAddress = $Networks.IPAddress
    SubnetMask = $Networks.IPSubnet
    DefaultGateway = $Networks.DefaultIPGateway
    DNSServers = $Networks.DNSServerSearchOrder
    DNSDomain = $Networks.DNSDomain
    DNSSuffix = $Networks.DNSDomainSuffixSearchOrder
    FullDNSREG = $Networks.FullDNSRegistrationEnabled
    WINSLMHOST = $Networks.WINSEnableLMHostsLookup
    WINSPRI = $Networks.WINSPrimaryServer
    WINSSEC = $Networks.WINSSecondaryServer
    DOMAINDNSREG = $Networks.DomainDNSRegistrationEnabled
    DNSEnabledWINS = $Networks.DNSEnabledForWINSResolution
    TCPNETBIOSOPTION = $Networks.TcpipNetbiosOptions
    IsDHCPEnabled = $Networks.DHCPEnabled
    AdapterName = $networkprops.name
    Status = $networkprops.status
    LinkSpeed = $networkprops.linkspeed
    Driverinformation = $networkprops.driverinformation
    MACAddress = $Networks.MACAddress
    }#EndPSCustomObject

    }#EndForEach

  • #21729
    Profile photo of Chris Lindauer
    Chris Lindauer
    Participant

    What I am trying to accomplish is to getting the settings to display per an adapter instead of everything in one group.

  • #21732
    Profile photo of Will Anderson
    Will Anderson
    Keymaster

    Ah. I see it now. Interesting challenge.

  • #21744
    Profile photo of Will Anderson
    Will Anderson
    Keymaster

    How does this work for you? *EDIT* Nevermind. Found a bug. I'll have it fixed in a few.

    $ComputerName = "server01"

    ForEach($Computer in $ComputerName){
    $Networks = (Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer).where({$PSItem.IPEnabled})

    ForEach($NetAdapter in $Networks){
    $Networkprops = (Get-NetAdapter -CimSession $NetAdapter.PSComputerName).where({$PSItem.MACAddress -eq $NetAdapter.MACAddress})
    [PSCustomObject]@{
    Description = $NetAdapter.Description
    IPAddress = $NetAdapter.IPAddress
    SubnetMask = $NetAdapter.IPSubnet
    DefaultGateway = $NetAdapter.DefaultIPGateway
    DNSServers = $NetAdapter.DNSServerSearchOrder
    DNSDomain = $NetAdapter.DNSDomain
    DNSSuffix = $NetAdapter.DNSDomainSuffixSearchOrder
    FullDNSREG = $NetAdapter.FullDNSRegistrationEnabled
    WINSLMHOST = $NetAdapter.WINSEnableLMHostsLookup
    WINSPRI = $NetAdapter.WINSPrimaryServer
    WINSSEC = $NetAdapter.WINSSecondaryServer
    DOMAINDNSREG = $NetAdapter.DomainDNSRegistrationEnabled
    DNSEnabledWINS = $NetAdapter.DNSEnabledForWINSResolution
    TCPNETBIOSOPTION = $NetAdapter.TcpipNetbiosOptions
    IsDHCPEnabled = $NetAdapter.DHCPEnabled
    AdapterName = $networkprops.name
    Status = $networkprops.status
    LinkSpeed = $networkprops.linkspeed
    Driverinformation = $networkprops.driverinformation
    MACAddress = $NetAdapter.MACAddress
    }#ENDPSCustomObject
    }#EndNetForEach
    }#EndCompForEach

  • #21745
    Profile photo of Will Anderson
    Will Anderson
    Keymaster

    OK. I've got this working now. Tell me what you think?

    $ComputerName = "server01"

    ForEach($Computer in $ComputerName){
    $Networks = (Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer).where({$PSItem.IPEnabled})

    ForEach($NetAdapter in $Networks){
    $NetMAC = $NetAdapter.MACAddress -replace ":","-"
    $Networkprops = Get-NetAdapter -CimSession $Networks.PSComputerName | Where-Object MACAddress -EQ $NetMAC
    [PSCustomObject]@{
    System = $NetAdapter.PSComputerName
    Description = $NetAdapter.Description
    IPAddress = $NetAdapter.IPAddress
    SubnetMask = $NetAdapter.IPSubnet
    DefaultGateway = $NetAdapter.DefaultIPGateway
    DNSServers = $NetAdapter.DNSServerSearchOrder
    DNSDomain = $NetAdapter.DNSDomain
    DNSSuffix = $NetAdapter.DNSDomainSuffixSearchOrder
    FullDNSREG = $NetAdapter.FullDNSRegistrationEnabled
    WINSLMHOST = $NetAdapter.WINSEnableLMHostsLookup
    WINSPRI = $NetAdapter.WINSPrimaryServer
    WINSSEC = $NetAdapter.WINSSecondaryServer
    DOMAINDNSREG = $NetAdapter.DomainDNSRegistrationEnabled
    DNSEnabledWINS = $NetAdapter.DNSEnabledForWINSResolution
    TCPNETBIOSOPTION = $NetAdapter.TcpipNetbiosOptions
    IsDHCPEnabled = $NetAdapter.DHCPEnabled
    AdapterName = $networkprops.name
    Status = $networkprops.status
    LinkSpeed = $networkprops.linkspeed
    Driverinformation = $networkprops.driverinformation
    MACAddress = $NetAdapter.MACAddress
    }#ENDPSCustomObject
    }#EndNetForEach
    }#EndCompForEach

  • #21746
    Profile photo of Jack Neff
    Jack Neff
    Participant

    The original script works for me on Windows 8. Can you post a screenshot of the error you're getting?

  • #21747
    Profile photo of Chris Lindauer
    Chris Lindauer
    Participant

    The script is getting close. I am seeing information separated out into each adapter but the get-netadapter properties are only displaying on the first adapter.

    AdapterName : {Ethernet, Ethernet}
    Status : {Up, Up}
    LinkSpeed : {1 Gbps, 1 Gbps}
    Driverinformation : {Driver Date 2013-03-28 Version 12.6.47.1 NDIS 6.30, Driver Date 2013-03-28 Version 12.6.47.1 NDIS 6.30}

  • #21748
    Profile photo of Chris Lindauer
    Chris Lindauer
    Participant

    System : TestSystem
    Description : Intel(R) 82574L Gigabit Network Connection
    IPAddress : {10.6.1.1}
    SubnetMask : {255.255.255.0}
    DefaultGateway : {10.6.1.1}
    DNSServers : {10.6.1.2, 10.6.1.3}
    DNSDomain :
    DNSSuffix : {test.com}
    FullDNSREG : True
    WINSLMHOST : False
    WINSPRI :
    WINSSEC :
    DOMAINDNSREG : False
    DNSEnabledWINS : False
    TCPNETBIOSOPTION : 2
    IsDHCPEnabled : False
    AdapterName : {Ethernet, Ethernet}
    Status : {Up, Up}
    LinkSpeed : {1 Gbps, 1 Gbps}
    Driverinformation : {Driver Date 2013-03-28 Version 12.6.47.1 NDIS 6.30, Driver Date 2013-03-28 Version 12.6.47.1 NDIS 6.30}
    MACAddress : 00:50:56:A3:22:00

    System : TESTSystem
    Description : Microsoft Failover Cluster Virtual Adapter
    IPAddress : {169.254.1.188, fe80::e9fe:288d:1e46:bab4}
    SubnetMask : {255.255.0.0, 64}
    DefaultGateway :
    DNSServers :
    DNSDomain :
    DNSSuffix : {test.com}
    FullDNSREG : True
    WINSLMHOST : False
    WINSPRI :
    WINSSEC :
    DOMAINDNSREG : False
    DNSEnabledWINS : False
    TCPNETBIOSOPTION : 2
    IsDHCPEnabled : False
    AdapterName :
    Status :
    LinkSpeed :
    Driverinformation :
    MACAddress : 02:04:23:10:9F:00

  • #21749
    Profile photo of Will Anderson
    Will Anderson
    Keymaster

    I'm seeing that with the Microsoft Failover Cluster Virtual Adapter as well. Do you have anything with multiple standalone adapters? I wonder if it's something to do with how that one is presented.

  • #21751
    Profile photo of Chris Lindauer
    Chris Lindauer
    Participant

    Will, I fired up a VM and it seems to be working great with that. I am not sure what's up with the Cluster Adapter. Here are the results. Another weird thing is the entries show up twice for the get-netadapter properties

    System : W811
    Description : Intel(R) 82574L Gigabit Network Connection
    IPAddress : {192.168.5.129}
    SubnetMask : {255.255.255.0}
    DefaultGateway : {192.168.5.2}
    DNSServers : {192.168.5.130, 192.168.5.131}
    DNSDomain : localdomain
    DNSSuffix : {vmwarelab.pvt, localdomain}
    FullDNSREG : True
    WINSLMHOST : False
    WINSPRI : 192.168.5.2
    WINSSEC :
    DOMAINDNSREG : False
    DNSEnabledWINS : False
    TCPNETBIOSOPTION : 2
    IsDHCPEnabled : True
    AdapterName : {Ethernet, Ethernet}
    Status : {Up, Up}
    LinkSpeed : {1 Gbps, 1 Gbps}
    Driverinformation : {Driver Date 2013-03-28 Version 12.6.47.1 NDIS 6.30, Driver Date 2013-03-28 Version 12.6.47.1 NDIS 6.30}
    MACAddress : 00:0C:29:7E:4A:65

    System : W811
    Description : Intel(R) 82574L Gigabit Network Connection #2
    IPAddress : {192.168.5.16}
    SubnetMask : {255.255.255.0}
    DefaultGateway :
    DNSServers :
    DNSDomain :
    DNSSuffix : {vmwarelab.pvt, localdomain}
    FullDNSREG : True
    WINSLMHOST : False
    WINSPRI :
    WINSSEC :
    DOMAINDNSREG : False
    DNSEnabledWINS : False
    TCPNETBIOSOPTION : 0
    IsDHCPEnabled : False
    AdapterName : {Static, Static}
    Status : {Up, Up}
    LinkSpeed : {1 Gbps, 1 Gbps}
    Driverinformation : {Driver Date 2013-03-28 Version 12.6.47.1 NDIS 6.30, Driver Date 2013-03-28 Version 12.6.47.1 NDIS 6.30}
    MACAddress : 00:0C:29:7E:4A:6F

  • #21752
    Profile photo of Chris Lindauer
    Chris Lindauer
    Participant

    Will

    Thank you for all your help on this. I was able to resolve the duplicate information by adding the | Select-Object -First 1 to the end of $networkprops. I am not sure its the cleanest way but it works. Here is the code and results.

    CODE

    $ComputerName = "w811"

    ForEach($Computer in $ComputerName){
    $Networks = (Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer).where({$PSItem.IPEnabled})

    ForEach($NetAdapter in $Networks){
    $NetMAC = $NetAdapter.MACAddress -replace ":","-"
    $Networkprops = Get-NetAdapter -CimSession $Networks.PSComputerName | Where-Object MACAddress -EQ $NetMAC | Select-Object -First 1
    [PSCustomObject]@{
    System = $NetAdapter.PSComputerName
    Description = $NetAdapter.Description
    IPAddress = $NetAdapter.IPAddress
    SubnetMask = $NetAdapter.IPSubnet
    DefaultGateway = $NetAdapter.DefaultIPGateway
    DNSServers = $NetAdapter.DNSServerSearchOrder
    DNSDomain = $NetAdapter.DNSDomain
    DNSSuffix = $NetAdapter.DNSDomainSuffixSearchOrder
    FullDNSREG = $NetAdapter.FullDNSRegistrationEnabled
    WINSLMHOST = $NetAdapter.WINSEnableLMHostsLookup
    WINSPRI = $NetAdapter.WINSPrimaryServer
    WINSSEC = $NetAdapter.WINSSecondaryServer
    DOMAINDNSREG = $NetAdapter.DomainDNSRegistrationEnabled
    DNSEnabledWINS = $NetAdapter.DNSEnabledForWINSResolution
    TCPNETBIOSOPTION = $NetAdapter.TcpipNetbiosOptions
    IsDHCPEnabled = $NetAdapter.DHCPEnabled
    AdapterName = $networkprops.name
    Status = $networkprops.status
    LinkSpeed = $networkprops.linkspeed
    Driverinformation = $networkprops.driverinformation
    MACAddress = $NetAdapter.MACAddress
    }#ENDPSCustomObject
    }#EndNetForEach
    }#EndCompForEach

    RESULTS

    System : W811
    Description : Intel(R) 82574L Gigabit Network Connection
    IPAddress : {192.168.5.129}
    SubnetMask : {255.255.255.0}
    DefaultGateway : {192.168.5.2}
    DNSServers : {192.168.5.130, 192.168.5.131}
    DNSDomain : localdomain
    DNSSuffix : {vmwarelab.pvt, localdomain}
    FullDNSREG : True
    WINSLMHOST : False
    WINSPRI : 192.168.5.2
    WINSSEC :
    DOMAINDNSREG : False
    DNSEnabledWINS : False
    TCPNETBIOSOPTION : 2
    IsDHCPEnabled : True
    AdapterName : Ethernet
    Status : Up
    LinkSpeed : 1 Gbps
    Driverinformation : Driver Date 2013-03-28 Version 12.6.47.1 NDIS 6.30
    MACAddress : 00:0C:29:7E:4A:66

    System : W811
    Description : Intel(R) 82574L Gigabit Network Connection #2
    IPAddress : {192.168.5.16}
    SubnetMask : {255.255.255.0}
    DefaultGateway :
    DNSServers :
    DNSDomain :
    DNSSuffix : {vmwarelab.pvt, localdomain}
    FullDNSREG : True
    WINSLMHOST : False
    WINSPRI :
    WINSSEC :
    DOMAINDNSREG : False
    DNSEnabledWINS : False
    TCPNETBIOSOPTION : 0
    IsDHCPEnabled : False
    AdapterName : Static
    Status : Up
    LinkSpeed : 1 Gbps
    Driverinformation : Driver Date 2013-03-28 Version 12.6.47.1 NDIS 6.30
    MACAddress : 00:0C:29:7E:4A:66

  • #21755
    Profile photo of Will Anderson
    Will Anderson
    Keymaster

    Looks like the Failover Cluster Adapter doesn't appear in the Get-NetAdapter tables for some reason. I'd have to dig into the cmdlet to see where it's pulling it's data from, but wherever it is, the Failover Cluster Adapter isn't there. I'll keep plugging away at the duplicate info issue. I'd actually like to have something like this to give to my admins.:

    PS C:\Windows\System32\WindowsPowerShell\v1.0> (Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName server01).where({$PSItem.IPEnabled}) | Select-Object MACAddress,Description | Format-Table -AutoSize

    MACAddress Description
    ———- ———–
    00:17:A4:77:1C:28 Microsoft Network Adapter Multiplexor Driver
    02:27:E8:0F:88:AE Microsoft Failover Cluster Virtual Adapter

    PS C:\Windows\System32\WindowsPowerShell\v1.0> Get-NetAdapter -Name * -CimSession server01 | Format-Table -AutoSize

    Name InterfaceDescription ifIndex Status MacAddress LinkSpeed PSComputerName
    —- ——————– ——- —— ———- ——— ————–
    Ethernet 8 HP Flex-10 10Gb 2-port 530FLB Ada...#79 19 Not Present D8-9D-67-62-B8-E7 0 bps server01
    Network Team #1 Microsoft Network Adapter Multiplexor Driver 23 Up 00-17-A4-77-1C-28 20 Gbps server01
    Ethernet 7 HP Flex-10 10Gb 2-port 530FLB Ada...#72 18 Up 00-17-A4-77-1C-28 10 Gbps server01
    Ethernet 4 HP Flex-10 10Gb 2-port 530FLB Ada...#74 15 Not Present D8-9D-67-62-B8-E1 0 bps server01
    Ethernet 3 HP Flex-10 10Gb 2-port 530FLB Ada...#76 14 Not Present D8-9D-67-62-B8-E2 0 bps server01
    Ethernet 2 HP Flex-10 10Gb 2-port 530FLB Ada...#77 13 Not Present D8-9D-67-62-B8-E6 0 bps server01
    Ethernet 5 HP Flex-10 10Gb 2-port 530FLB Ada...#78 16 Not Present D8-9D-67-62-B8-E3 0 bps server01
    Ethernet HP Flex-10 10Gb 2-port 530FLB Ada...#73 12 Up 00-17-A4-77-1C-2A 10 Gbps server01
    Ethernet 6 HP Flex-10 10Gb 2-port 530FLB Ada...#75 17 Not Present D8-9D-67-62-B8-E5 0 bps server01

    EDIT: Looks like the duplicate info is due to the team being discovered as a single adapter, but the individual device information is loaded as multiple properties in that space.

  • #21810
    Profile photo of Chris Lindauer
    Chris Lindauer
    Participant

    Would it be possible to write the script results to a variable?

  • #21824
    Profile photo of Will Anderson
    Will Anderson
    Keymaster

    Hey there Chris,

    Let me play with my function for a bit and I'll let you know!

  • #21825
    Profile photo of Will Anderson
    Will Anderson
    Keymaster

    So I'll answer the best way that I can, and maybe ask some of our other community people for their input. The short answer is yes.

    If you build out the script as a function (I'm actually putting the finishing touches on mine for a blog post about PSCustomObjects), then you can call the function as a variable. For example:

    $NetAdapter = Get-AdapterConfig -Computername Server01

    Then if you call the NetAdapter variable, it will give you the output. You can use the NetAdapter variable in the pipeline as you see fit. For example:

    Test-Connection $NetAdapter.System

    This will give you a return similar to mine:

    PS C:\WINDOWS\system32> Test-Connection $NetAdapter.ComputerName

    Source Destination IPV4Address IPV6Address Bytes Time(ms)
    —— ———– ———– ———– —– ——–
    MYSYSTEM01 SERVER01 10.54.47.30 32 2
    MYSYSTEM01 SERVER01 10.54.47.30 32 16
    MYSYSTEM01 SERVER01 10.54.47.30 32 1
    MYSYSTEM01 SERVER01 10.54.47.30 32 2

    However, if you're looking to do something like:

    Get-AdapterConfig -computername server01 | Write-Object $NetAdapter

    You'll get an error like this:

    Write-Output : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input
    and its properties do not match any of the parameters that take pipeline input.

    This is something that I would kind of expect, as it would essentially be looping back on itself. But that's where I get a little lost.

  • #21830
    Profile photo of Stephen Owen
    Stephen Owen
    Participant

    Hey guys,

    Jumping in for the last part. Write-Output simply displays the content of a variable or sends it on down to the next cmdlet, not setting any values

    You'd use Set-Variable instead (although I would stay away from this approach, people very rarely set a variable at the end of a command line, normally setting the value at the start of the line).

You must be logged in to reply to this topic.