none
PowerShell - How to find out the IP address of a machine using WMI on any machine.

    Pregunta

  • Hi All,
    I am currently using gwmi win32_networkadapterconfiguration | where-object {$_.ipaddress -ne $null -and $_ipaddress -ne "0.0.0.0"} to get the ipaddress of a machine.  The reason for the where-object is because there are often many adapters and in a few cases I've seen it set to 0.0.0.0
    I would like to be able to do this using the -query statement so that I dont bring back any unnessary information.  Can you please give me some direction?

    On one remote machine I also got the MAC code included in the output too, which was not desired.  There has to be a decent way to get the current IP address of whatever adapter is active.

    Thank you,

    Will
    lunes, 07 de septiembre de 2009 17:08

Respuestas

Todas las respuestas

  • Something like this:

    gwmi -query "Select IPAddress From Win32_NetworkAdapterConfiguration Where IPEnabled = True"
    Uros Calakovic
    lunes, 07 de septiembre de 2009 18:15
  • Hi, thanks for your reply,

    See, now I get this sometimes on various remote PC's:

    __GENUS          : 2
    __CLASS          : Win32_NetworkAdapterConfiguration
    __SUPERCLASS     :
    __DYNASTY        :
    __RELPATH        :
    __PROPERTY_COUNT : 1
    __DERIVATION     : {}
    __SERVER         :
    __NAMESPACE      :
    __PATH           :
    IPAddress        : {0.0.0.0}

    __GENUS          : 2
    __CLASS          : Win32_NetworkAdapterConfiguration
    __SUPERCLASS     :
    __DYNASTY        :
    __RELPATH        :
    __PROPERTY_COUNT : 1
    __DERIVATION     : {}
    __SERVER         :
    __NAMESPACE      :
    __PATH           :
    IPAddress        : {192.168.59.143}

    __GENUS          : 2
    __CLASS          : Win32_NetworkAdapterConfiguration
    __SUPERCLASS     :
    __DYNASTY        :
    __RELPATH        :
    __PROPERTY_COUNT : 1
    __DERIVATION     : {}
    __SERVER         :
    __NAMESPACE      :
    __PATH           :
    IPAddress        : {0.0.0.0}

    Which obviously I don't want,  I tried to create a WQL statement to filter this out but it doesn't work, I get "Invalid Query":
    gwmi -query "select ipaddress from win32_networkadapterconfiguration where ipenabled = 'true' and ipaddress is not '0.0.0.0'" -computer $user -credential $cred

    Can you give me some suggestions?

    Thank you,

    Will

    martes, 08 de septiembre de 2009 8:33
  • You can't use Win32_NetworkAdapterConfiguration.IPAddress in a query because it's an array. Is there any other Win32_NetworkAdapterConfiguration property you can use to distinguish the two adapters?
    Uros Calakovic
    martes, 08 de septiembre de 2009 14:35
  • On one remote machine I also got the MAC code included in the output too, which was not desired.  There has to be a decent way to get the current IP address of whatever adapter is active.

    (gwmi -query "Select IPAddress From Win32_NetworkAdapterConfiguration Where IPEnabled = True").IPAddress
    jueves, 10 de septiembre de 2009 16:32
  • You may want to use http://techibee.com/powershell/powershell-get-ip-address-subnet-gateway-dns-serves-and-mac-address-details-of-remote-computer/1367 to get IP address details at granular level like IP, subnetmask, default gateway, DNS, MAC address, etc.

    Thanks,

    Sitaram Pamarthi

    Blog : http://techibee.com

    Follow on Twitter

    This posting is provided AS IS with no warranties or gurentees,and confers no rights

    viernes, 17 de febrero de 2012 18:06
  • I also have several adapters, however I realise only the one being actually connecteed to my gateway-switch and being aclled lan has the correct ip number. The following command works for me on XP.

    (gwmi win32_networkadapterconfiguration | where-object {$_.DefaultIPGateway -ne $null -and $_.DNSDomain -eq "lan" -and $_ipaddress -ne "0.0.0.0"}).IPAddress

    schiebi 

    domingo, 23 de septiembre de 2012 9:48
  • I also have several adapters, however I realise only the one being actually connecteed to my gateway-switch and being aclled lan has the correct ip number. The following command works for me on XP.

    (gwmi win32_networkadapterconfiguration | where-object {$_.DefaultIPGateway -ne $null -and $_.DNSDomain -eq "lan" -and $_ipaddress -ne "0.0.0.0"}).IPAddress

    schiebi 

    Why work so had when there is a common way to get that information.

    gwmi win32_networkadapterconfiguration  -filter "IPEnabled=True"

    Your approach is backwards and will not work in all cases.

    Since this thread is closed you will need to open a new Topic if you have further questions.

    To get a specific adapter by name we can use the WMI NetConnectionID property which is where WMI reports the adapter name:

    $adapter=gwmi win32_networkadapter -filter "NetConnectionID='LAN'"
    $adapter.GetRelated('win32_networkadapterconfiguration')

    Note that all filtereing is fone in WMI saving extracting large amounts of unneeded over the network.  We get one adapter and one configuration.  This is also one way that PowerSHell gives us to simplify an 'AssociatorsOf' query.  An AssociatorsOf query would be the fastest at returing the configuration if you know the name of the adapter.

    Play with this.  You will find that things get much easier once you understand WMI and how PowerShell is arranged to use WMI.

    In PowerShell v3 we have even more goodies for this for improved WMI access.


    ¯\_(ツ)_/¯

    domingo, 23 de septiembre de 2012 10:02
  • powershell gwmi -query 'Select IPAddress FROM Win32_NetworkAdapterConfiguration Where IPEnabled = True' ^| select IPAddress
    martes, 04 de diciembre de 2012 8:11
  • powershell gwmi -query 'Select IPAddress FROM Win32_NetworkAdapterConfiguration Where IPEnabled = True' ^| select IPAddress

    Not an answer to this question.  The question is how to use teh adapter name to get the address.  There can be more than one adapter with an IP in which case this will not work at all.


    ¯\_(ツ)_/¯

    martes, 04 de diciembre de 2012 18:37