Principales respuestas

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.0I 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.

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

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

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.