Being an administrator, you can’t do much if you don’t know about IP Addresses, and how frustrating and stupid it is that it is so difficult to get the IP Address of a machine is a single variable when you are scripting on Windows!!!
sure you can get it with “IPCONFIG” or “ipconfig | find “IP Address”" (winXP) or ipconfig | find “IPv4″ but that gives you a full line and you have to go filter for the ip address only.
Other solutions, vbscript , method, wmi? or registry?
can do both except that the WMI is weird and doesn’t seem to work similarly on winXP as on win7.
strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration") For Each objItem In colItems wscript.echo objItem.IPAddress(0) Next
Gives you the address but an error which doesn’t quite seem to make sense..
the other option, get it from Registry. This is tricky because the network interface subkey is different for every machine. The trick is to recursively search the subkey for the ValueName you are looking for.. et voilĂ !!
const HKEY_LOCAL_MACHINE = &H80000002 strComputer = "." Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"&_ strComputer & "\root\default:StdRegProv") strKeyPath = "SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces" retval = objReg.EnumKey(HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys) 'WScript.Echo retval strValueName = "IPAddress" For Each subkey In arrSubKeys strKeyPath1 = strKeyPath & "\" & subkey 'wscript.echo strKeyPath Return = objReg.GetMultiStringValue(HKEY_LOCAL_MACHINE,strKeyPath1, _ strValueName,arrValues) If (Return = 0) And (Err.Number = 0) Then ' Treat the multistring value as a collection of strings separated by spaces and output For Each strValue In arrValues WScript.Echo strValue Next End If Next
