need to obtain an ip address from an array

Welcome Forums General PowerShell Q&A need to obtain an ip address from an array

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 7 years, 7 months ago.

  • Author
  • #5734

    Topics: 1562
    Replies: 0
    Points: 0
    Rank: Member

    by willbs at 2013-03-28 09:05:43

    i need to get the ip addresses of a uut, i do this command

    $IPconfig = invoke-command -scriptblock {ipconfig /all} -computername $global:uutName -credential $global:Credential
    $IPconfig[18] is
    IPv4 Address. . . . . . . . . . . :
    $IPconfig[38] is
    IPv4 Address. . . . . . . . . . . :

    how do i get the ip addresses that are between ": " and "(Preferred)"
    i was going to hard code it but the ip length can change

    by MasterOfTheHat at 2013-03-28 11:14:39

    First off, start using .NET objects with PowerShell instead of string manipulation! 🙂 You can get the IP addresses using the Win32_NetworkAdapterConfiguration WMI class, (lots of other information, too):
    PS > Get-WMIObject -Class Win32_NetworkAdapterConfiguration -ComputerName . -Filter {IPEnabled = "True"} | Select-Object Description, IPAddress

    Description IPAddress
    ----------- ---------
    Broadcom NetXtreme Gigabit Ethernet {10.x.x.29}
    VMware Virtual Ethernet Adapter for VMnet1 {, fe80::b40a:99a6:a50d:c1c}
    VMware Virtual Ethernet Adapter for VMnet8 {, fe80]
    Or, if you're using v3, use the Get-NetworkConnectionInfo cmdlet:
    PS > Get-NetworkConnectionInfo

    Name : Broadcom NetXtreme Gigabit Ethernet
    DnsServers : {10.x.x.254, 10.x.x.157, 10.x.x.127}
    IPAddresses : {10.x.x.29}
    AdapterGuid : c4dd10fe-0bf2-48cb-a7b3-792cd2dbcc6e
    MacAddress : E8:39:35:33:EC:30
    Identity : c4dd10fe-0bf2-48cb-a7b3-792cd2dbcc6e
    IsValid : True
    ObjectState : Unchanged

    Name : VMware Virtual Ethernet Adapter for VMnet1
    DnsServers : {}
    IPAddresses : {, fe80::b40a:99a6:a50d:c1c}
    AdapterGuid : ee1d0dbd-7450-4a75-8ab1-9841064303d7
    MacAddress : 00:50:56:C0:00:01
    Identity : ee1d0dbd-7450-4a75-8ab1-9841064303d7
    IsValid : True
    ObjectState : Unchanged

    Name : VMware Virtual Ethernet Adapter for VMnet8
    DnsServers : {}
    IPAddresses : {, fe80::5c71:f473:4ff1:83c5}
    AdapterGuid : 1a77e468-d612-4e49-9ccf-489e82a1a54c
    MacAddress : 00:50:56:C0:00:08
    Identity : 1a77e468-d612-4e49-9ccf-489e82a1a54c
    IsValid : True
    ObjectState : Unchanged

    But if you just really want to use ipconfig, you'll have to do some fancy string manipulation and get to know regex...

    by willbs at 2013-03-28 11:35:57

    thanks that makes sense but i still need to extract the ip address because i'm getting the info from a remote server
    when i run your command remotely

    $IPconfig = invoke-command -scriptblock {Get-WMIObject -Class Win32_NetworkAdapterConfiguration -Filter {IPEnabled = "True"} | Select-Object IPAddress } `
    -computername $global:uutName -credential $global:Credential

    i get this result

    IPAddress PSComputerName RunspaceId
    ——— ————– ———-
    {, fe80::5c64:4246:xxx:xxx} stg-xx 4dxxx2-f9xx-xxx-98df-1c4c33xxxx
    {, fe80::b88c:87a:xxx:xxx} stg-xx 4dfxxx2-f9xx-xxx-98df-1c4cxxx

    by MasterOfTheHat at 2013-03-28 11:56:14

    $IPconfig.IPAddress will give you the entire array of IP addresses from each computer, ({, fe80::5c64:4246:xxx:xxx}).

    You can address each address using the array index: $IPconfig.IPAddress[0] would get you "" and $IPconfig.IPAddress[1] would get you "fe80::5c64:4246:xxx:xxx"

    by willbs at 2013-03-28 12:08:37

    yes it does, cool
    sorry about my noobness
    thanks guru dude!

    by MasterOfTheHat at 2013-03-28 12:44:35

    No worries! Glad it works. And we were all n00bs once...

The topic ‘need to obtain an ip address from an array’ is closed to new replies.