2

I need to get a list of properties for multiple server, but I'm stuck with the output of second command in my loop:

$(foreach ( $Net in $Nets ) {
Get-NetAdapterBinding -ComponentID  ms_msclient,ms_server,ms_tcpip6 -ErrorAction SilentlyContinue | select Name,DisplayName,Enabled
Get-NetAdapterAdvancedProperty $Net -DisplayName "Speed & Duplex" | select DisplayValue
}) | Format-List

The output of first cmd is correct:

Name        : LAN_Clients
DisplayName : Internet Protocol Version 6 (TCP/IPV6)
Enabled     : False

Name        : LAN_Clients
DisplayName : File and Print Sharing
Enabled     : False

Name        : LAN_Clients
DisplayName : Client for Microsoft Networks
Enabled     : False

The second cmd seems ignored... If I run cmd manually the output is correct:

Get-NetAdapterAdvancedProperty "LAN_Clients" -DisplayName "Speed & Duplex" | select DisplayValue
    
DisplayValue
------------
Auto Negotiation

What am I doing wrong?

9
  • can you just run the selection over $Net, and ensure a name is actually being passed to it? Commented Feb 3, 2021 at 16:12
  • Oh wait, youre running it inside the foreach. Commented Feb 3, 2021 at 16:17
  • @Abraham Zinala, yes Abraham $Nets = get-NetAdapter Commented Feb 3, 2021 at 16:20
  • 1
    If you explicitly pipe to Format-List, all output should show. With default output (implied Format-Table), the first output object locks in all output columns based on its properties, so the DisplayValue property from the 2nd command never shows - see this answer. Commented Feb 3, 2021 at 16:26
  • 1
    @AbrahamZinala, I'm not sure I fully understand, but you can place any command or expression inside a foreach block, and whatever these constructs output becomes the foreach statement's overall output. The problem is merely a display problem, due to how (implicitly applied) Format-Table formatting works; again, see the previously linked answer. Commented Feb 3, 2021 at 16:51

3 Answers 3

1

You need to combine both outputs into a single object.

Try

$(foreach ( $Net in $Nets ) {
    $speed = (Get-NetAdapterAdvancedProperty $Net -DisplayName "Speed & Duplex").DisplayValue
    Get-NetAdapterBinding -ComponentID  ms_msclient,ms_server,ms_tcpip6 -ErrorAction SilentlyContinue |   
    Select-Object Name,DisplayName,Enabled,
                  @{Name = 'Speed & Duplex'; Expression = {$speed}}
}) | Format-List
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Theo, in this mode the Speed & Duplex is printed out but it's blank for each $net
0

You don't need to call Get-NetAdapter, as Get-NetAdapterBinding already returns all the bindings for all the adapters.

In the foreach loop, you're not using the -Name parameter with Get-NetAdapterBinding so it's returning all the bindings for all your adapters every iteration of the loop.

You can use an expression block in Select-Object to get the additional duplex property you're after, like this:

Get-NetAdapterBinding -ComponentID  ms_msclient,ms_server,ms_tcpip6 -ErrorAction SilentlyContinue |
    Select-Object Name,DisplayName,Enabled, @{Name = 'Speed & Duplex'; Expression={Get-NetAdapterAdvancedProperty -InterfaceAlias $_.InterfaceAlias -DisplayName 'Speed & Duplex' | select -ExpandProperty DisplayValue }} |
    Format-List

1 Comment

Thank you antonyoni! Now I've to learn how to output all the properties per each nic, without print the same list 4 times, one for each properties!
0

For troubleshooting purposes, try this:

$Nets = Get-NetAdapterBinding -ComponentID  ms_msclient,ms_server,ms_tcpip6 -ErrorAction SilentlyContinue
    foreach ($Net in $Nets.name ) {
Get-NetAdapterAdvancedProperty $Net -DisplayName "Speed & Duplex" | select DisplayValue
}

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.