none
Get-netadapter in a for each loop RRS feed

  • Question

  • Hi,

    I'm trying to create an array using get-netadapter.  My variable is:

    $adapter = get-netadapter -physical

    When I run the for each loop I'm getting the messy output of: MSFT_Netadapter (CreationClassName = ...), over and over for each one.

    What I'm wanting is it to print the output as if you just ran the command without it being in a variable

    My code looks like the following:

    function Network {

    $adapter = get-netadapter -physical

    foreach ($a in $adapter) {
    Write-Host "$a"
    }
    }
    Network

    I know something is off as its an array just can't remember at the moment.  Any thoughts?

    Thanks


    JCtech1123, Cheers

    Wednesday, September 11, 2019 11:17 PM

Answers

All replies

  • Get-Netadapter returns an array of your network adapters. You just need to reference the properties of each one that you are interested in. 

     cls
     $adapters = get-netadapter -physical
     'InterfaceDescription for all adapters'
     ForEach ($adapter in $adapters){
        "Desc={0}" -f $adapter.InterfaceDescription  
     }
     ''
     'All properties and methods of the last $adapter object'
     $adapter | Get-Member

    Wednesday, September 11, 2019 11:35 PM
  • get-netadapter -physical | select name, Status, IfDesc

    Get-NetAdapter -physical | 
         ForEach-Object{ $_.IfDesc }


    \_(ツ)_/

    Wednesday, September 11, 2019 11:35 PM
  • I read the above comments and I put below what I got so far.

    $adapter = Get-NetAdapter -Physical | Select-Object name, status, IFDesc, MacAddress, LinkSpeed | ?{$_.status -eq "up"} | Format-Table

    What I would like to do from here is put the above command into a while loop or something. 

    Essentially what I'm trying to do is through PowerShell, see at what point one connection goes down and when another takes over as active.  Like if Ethernet is disconnected and WIFI is on you see where WIFI becomes the active connection.

    Thoughts?  I fell like I have done this before but at a stuck.

    Thanks


    JCtech1123, Cheers

    Tuesday, September 17, 2019 5:36 PM
  • So what you would do is write "pseudo code" that provides an exact list of steps of what you want the script to do. Once you have a good idea of the steps and how you want the loop to work, then you would write your code. (You'll need to start somewhere. It's a bit much to ask others to write the code for you, as that's not really the purpose of this forum.)

    -- Bill Stewart [Bill_Stewart]

    Tuesday, September 17, 2019 5:40 PM
    Moderator
  • Not really sure what your referring to with "It's a bit much to ask others to write the code for you", but essentially I have searched everywhere for articles and tips on trying to do this and all I have found is what I have above.  I was able to put that variable into a while loop but the problem is when the interface changes it doesn't catch it.

    Thanks.


    JCtech1123, Cheers

    Wednesday, September 18, 2019 5:31 PM
  • Start by learning PowerShell basics. You are just begging people to write code for you because you can't read or don't want to read or take a tutorial. You cannot learn anything by asking questions in a forum if you do not know the basic technology.

    Here is how to make a loop.

    while($true){
         # add you code her
    }


    \_(ツ)_/

    Wednesday, September 18, 2019 5:43 PM
  • I did the while loop, done those many times, problem is that if Ethernet is the "up" link and "WIFI" is Disconnected as in (not active link) and I make the $variable, then Wifi becomes active link it doesn't dynamically see the new "up" link.

    Thanks


    JCtech1123, Cheers

    Wednesday, September 18, 2019 8:55 PM
  • Are you running the command once for each loop iteration?

    -- Bill Stewart [Bill_Stewart]

    Wednesday, September 18, 2019 9:15 PM
    Moderator
  • Yeah, I think the issue is, I'm looking at this wrong.  I'm trying to target the "up" "disconnected" "up" state of the adapters on the device.  If I run my command without having it as a variable, I can run over and over, disconnect Ethernet, reconnect, and I can see Ethernet or WIFI become active.

    Problem is the variable itself would change and I don't know if variables will update like this dynamically.  Still searching.

    Thanks


    JCtech1123, Cheers

    Wednesday, September 18, 2019 9:31 PM
  • Solved issue.  Problem was my if clause in my variable.

    JCtech1123, Cheers

    • Marked as answer by JCtech1123 10 hours 46 minutes ago
    10 hours 46 minutes ago