Getting IPConfig from Get-NetIPConfiguration

Viewing 4 reply threads
  • Author
    Posts
    • #1554415
      PowerMe!
      Participant
      Member Points: 1,217
      Rank: 3

      I am trying to output basic IP config data for different interfaces on a host by

      Get-NetIPConfiguration | select InterfaceAlias, IPv4Address, DNSServer,IPv4DefaultGateway
      
      or 
      
      Get-NetIPConfiguration | ft -property ("InterfaceAlias", "IPv4Address", "DNSServer","IPv4DefaultGateway")

      It outputs the IP info not in the form I want – just IP addresses. My understanding is that it is outputting the relevant Objects as defined in the Select. I am missing a way to extract the strings from the above objects. Would appreciate an explanation on how to deal with it.

       

      On closer look:

       

      ... | select DNSServer| Format-List
      
      DNSServer : {MSFT_DNSClientServerAddress (Name = "8", CreationClassName = "", SystemCreationClassName = "", SystemName = "23"), MSFT_DNSClientServerAddress (Name = "8", CreationClassName = "",
      SystemCreationClassName = "", SystemName = "2")}
      
      
      • This topic was modified 1 year, 9 months ago by PowerMe!.
      0
    • #1554419
      Leos Marek
      Moderator
      Member Points: 22,572
      Author of Year 2020
      Rank: 4

      You have to create your own property showing just the info you need, like this:

      Get-NetIPConfiguration | select InterfaceAlias, @{l="IPaddress";e={$_.Ipv4address.ipaddress}}, @{l="DNS";e={$_.DNSServer.Serveraddresses}}
      InterfaceAlias                IPaddress                     DNS
      --------------                ---------                     ---
      VMware Network Adapter VMnet8 192.168.91.1                  {fec0:0:0:ffff::1, fec0:0:0:ffff::2, fec0:0:0:ffff::3}
      VMware Network Adapter VMnet1 192.168.17.1                  {fec0:0:0:ffff::1, fec0:0:0:ffff::2, fec0:0:0:ffff::3}
      Wi-Fi                         192.168.1.70                  {192.168.1.1, 8.8.8.8}
      Ethernet 3                    169.254.149.171               {fec0:0:0:ffff::1, fec0:0:0:ffff::2, fec0:0:0:ffff::3}
      Bluetooth Network Connection  169.254.32.60                 {fec0:0:0:ffff::1, fec0:0:0:ffff::2, fec0:0:0:ffff::3}
      Ethernet 2                    169.254.21.239                {fec0:0:0:ffff::1, fec0:0:0:ffff::2, fec0:0:0:ffff::3}
      Ethernet                      {192.168.10.3, 169.254.1.246} {fec0:0:0:ffff::1, fec0:0:0:ffff::2, fec0:0:0:ffff::3}

       

      +2
      avataravatar
    • #1554420
      PowerMe!
      Participant
      Member Points: 1,217
      Rank: 3

      Thank you- that’s what I looking for- lost the reference to it! Would you mind explaining a bit more on that syntax? Is there a reference  can follow?

      @{l="IPaddress";e={$_.Ipv4address.ipaddress}}
      
      

      @{…} you define an indexed array

      • I presume “l” is label
      • what’s “e” – I see that it has the property

      I ended up doing a similar but somewhat elaborate way by defining a custom array as below.

      foreach($int in (get-netipconfiguration -All)){
      $arr = @()
      $arr = [PSCustomObject]@{
      IntID = $int.InterfaceIndex
      IntAlias = $int.InterfaceAlias
      IntIP4 = ($int.IPv4Address).ipAddress
      IntDNS = ($int.DNSServer).ServerAddresses
      IntDescr =$int.InterfaceDescription
      }
      
      #Store output in a variable that writes to a debug file along with some other info.
      $str2Write = $str2Write + $arr | Out-String
      }

      This generates a bit of compact output but serves my purpose. But I liked your option as the output it much nicer. I will update my script, thanks again.

      C:\Users\Ratan\desktop> $str2Write
      @{IntID=26; IntAlias=Ethernet 5; IntIP4=169.254.47.26; IntDNS=System.Object[]; IntDescr=N........}
      @{IntID=37; IntAlias=vEthernet (dmz); IntIP4=169.254.37.249; IntDNS=System.Object[]; IntDescr=Hyper-V Virtual .....}
      @{IntID=48; IntAlias=vEthernet (Default Switch) 2; IntIP4=172.17.55.145; IntDNS=System.Object[]; IntDescr=H..........}
      @{IntID=55; IntAlias=vEthernet (nat); IntIP4=172.27.192.1; IntDNS=System.Object[]; IntDescr=H..........}
      @{IntID=8; IntAlias=Wi-Fi; IntIP4=172.16.159.110; IntDNS=4.2.2.1; IntDescr=Intel(R) Dual Band Wireless-AC 8260}
      @{IntID=22; IntAlias=Bluetooth Network Connection 2; IntIP4=169.254.135.17; IntDNS=System.Object[]; IntDescr=Blue.......}
      @{IntID=32; IntAlias=Local Area Connection* 2; IntIP4=169.254.31.155; IntDNS=System.Object[]; IntDescr=Microsoft....}
      @{IntID=11; IntAlias=Local Area Connection* 17; IntIP4=169.254.11.193; IntDNS=System.Object[]; IntDescr=Microsoft .........}
      

      In any case, this is part of a script that I made to dump basic information from a computer to text file which is used for troubleshooting AD, networking and application errors.

      • This reply was modified 1 year, 9 months ago by PowerMe!.
      +1
    • #1554433
      Leos Marek
      Moderator
      Member Points: 22,572
      Author of Year 2020
      Rank: 4

      As you say, l=label, e=expression. Its not about array. The @ is used to create the custom property, not related to array at all. You can basically put to expression whatever scriptblock you want.

      Here you have great explanation – https://4sysops.com/archives/add-a-calculated-property-with-select-object-in-powershell/

      +1
      avatar
    • #1554434
      PowerMe!
      Participant
      Member Points: 1,217
      Rank: 3

      Ah, thank you so much, got it.

      0
Viewing 4 reply threads
  • You must be logged in to reply to this topic.
© 4sysops 2006 - 2021

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account