0

The purpose of my code is to pull information from network cards on Windows hosts.

Unless I am mistaken, there is no built-in way of doing this. So what I want to do is to build a custom object.

The problem I am facing is that there are mutiple cards on different systems and I am not able to output the information in a human readable format for each property is an array.

$AdapterSpeed = @(Get-WmiObject Win32_NetworkAdapter | foreach-object {Get-WmiObject -namespace root/WMI -class MSNdis_LinkSpeed -filter "InstanceName='$($_.Name)'"} | Select-Object InstanceName,NdisLinkSpeed,Active)

$AdpaterProp = @(Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName . | Select-Object -Property Description,DHCPEnabled,DHCPServer,DNSDomain,DNSServerSearchOrder,DefaultIPGateway)

# How can I create an array of propertie
$myCustomObject = New-Object -TypeName PSObject -Property @{
    'Description' = $AdpaterProp.Description
    'Speed' = $AdapterSpeed.NdisLinkSpeed
    'DHCPEnabled' = $AdpaterProp.DHCPEnabled
    'DHCPServer'= $AdpaterProp.DHCPServer
    'DNSDomain'= $AdpaterProp.DNSDomain
    'DNSServerSearchOrder'= $AdpaterProp.DNSServerSearchOrder
    'DefaultIPGateway'= $AdpaterProp.DefaultIPGateway
}

What I would like to see is something like this:

Description: Intel(R) Dual Band Wireless-AC 8260
DHCPEnabled : True
DHCPServer : 10.0.0.20
DNSDomain : localhost.localdomain
DNSServerSearchOrder : {10.0.0.11, 10.0.0.12}
DefaultIPGateway : {10.0.0.1}
NdisLinkSpeed : 100000000
Active : True

1 Answer 1

1

You'll want to do this in a single loop so that you can keep track of which MSNdis_LinkSpeed and Win32_NetworkAdapterConfiguration instances are tied to which adaptor:

$allCustomObjects = foreach($adapter in Get-WmiObject Win32_NetworkAdapter){
    $AdapterSpeed = Get-WmiObject -Namespace root/WMI -Class MSNdis_LinkSpeed -Filter "InstanceName='$($adapter.Name)'"
    $AdpaterProp  = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "Index='$($adapter.Index)'"
    New-Object psobject -Property @{
        'Description' = $AdpaterProp.Description
        'Speed' = $AdapterSpeed.NdisLinkSpeed
        'DHCPEnabled' = $AdpaterProp.DHCPEnabled
        'DHCPServer'= $AdpaterProp.DHCPServer
        'DNSDomain'= $AdpaterProp.DNSDomain
        'DNSServerSearchOrder'= $AdpaterProp.DNSServerSearchOrder
        'DefaultIPGateway'= $AdpaterProp.DefaultIPGateway
    }
}

$allCustomObjects now contains multiple custom object, one for each Win32_NetworkAdapter instance on your machine

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.