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?

ilRobby
  • 69
  • 2
  • 10
  • can you just run the selection over `$Net`, and ensure a name is actually being passed to it? – Abraham Zinala Feb 03 '21 at 16:12
  • Oh wait, youre running it inside the `foreach`. – Abraham Zinala Feb 03 '21 at 16:17
  • @Abraham Zinala, yes Abraham `$Nets = get-NetAdapter` – ilRobby Feb 03 '21 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](https://stackoverflow.com/a/45705068/45375). – mklement0 Feb 03 '21 at 16:26
  • @mklement0, im confused in regards to this. One, I wasn't aware you could reference a command inside of the `foreach`; guess that's cool, unless you have some more insight on regards to how that works. Two, if that does work, its not piping the correct value to `Get-NetAdapterAdvancedProperty`, or a specific one in this case. – Abraham Zinala Feb 03 '21 at 16:30
  • @mklement0 Correct! Now I've removed format-list but second cmd still ignored – ilRobby Feb 03 '21 at 16:46
  • It isn't _ignored_, its output is just not _displayed_ - see the previously linked answer for a detailed explanation and workarounds. – mklement0 Feb 03 '21 at 16:49
  • 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. – mklement0 Feb 03 '21 at 16:51
  • @Theo, sorry Theo for the delay, now I've done! Bye! – ilRobby Feb 09 '21 at 20:37

3 Answers3

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
Theo
  • 57,719
  • 8
  • 24
  • 41
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
}
Abraham Zinala
  • 4,267
  • 3
  • 9
  • 24
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
antonyoni
  • 849
  • 5
  • 11
  • 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! – ilRobby Feb 09 '21 at 20:35