AutoHotkey Community

It is currently May 27th, 2012, 12:39 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: February 3rd, 2010, 5:41 am 
Offline

Joined: July 12th, 2009, 5:33 pm
Posts: 9
I'm trying to use WMI_Query to get my current IP address, but I'm having issues with the values it returns.
Code:
WMI_Query("\\.\root\cimv2", "Win32_NetworkAdapterConfiguration WHERE ServiceName='athr'", "IPAddress")


returns numbers like 934456. If I reload the script, the numbers change, but are still meaningless.


Code:
WMI_Query("\\.\root\cimv2", "Win32_NetworkAdapterConfiguration WHERE ServiceName='athr'")

gives a meaningful result, but I only want the line with the IPAddress, not the rest of the information.

Any thoughts?


Report this post
Top
 Profile  
Reply with quote  
PostPosted: February 3rd, 2010, 5:44 am 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
I am not aware of this WMI_Query function, but...
dansherman wrote:
(...) gives a meaningful result, but I only want the line with the IPAddress, not the rest of the information.

You could get only the IP from that.
What is the format of the returned value?
You may try RegEx or something like that.

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 3rd, 2010, 6:15 am 
Offline

Joined: July 12th, 2009, 5:33 pm
Posts: 9
WMI_Query() comes from this post.

The result from the second example looks like this:
Code:
instance of Win32_NetworkAdapterConfiguration
{
DHCPEnabled      : True;
IPAddress        : {192.168.1.218};
DefaultIPGateway : {192.168.1.1};
DNSDomain        :;
ServiceName      : athr;
Description      : 11b/g Wireless LAN Mini PCI Express Adapter III;
Index            : 11;
};

If I return DHCPEnabled, or ServiceName, I get the expected result. But if the value is in curly brackets, I get gibberish.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 10th, 2011, 9:37 pm 
Offline

Joined: September 13th, 2009, 2:34 pm
Posts: 30
Hi dansherman, did you ever solve this?

I'm looking for a way to do this as well and tried WMI, which gives me the same garbled result as you get/ got and also tried RegRead.

Regread works on my machine and other Windows 7, DHCP enabled machines but not on XP or Vista or when the IP is manually set..

My WMI code:

Code:
COM_Init()
WMI_Query("\\.\root\cimv2", "Win32_NetworkAdapterConfiguration WHERE IPEnabled = True", "IPAddress")
ItemIP := WMI_Query("\\.\root\cimv2", "Win32_NetworkAdapterConfiguration WHERE IPEnabled = True", "IPAddress")
FileAppend, %ItemIP%, NetworkX.txt
COM_Term()
ExitApp

WMI_Query(Namespace, Class, Property = "")
{
   psvc :=   COM_GetObject("winmgmts:{impersonationLevel=impersonate}!" . Namespace)
   pset :=   COM_Invoke(psvc, "ExecQuery",  "SELECT * FROM " . Class)
   penm :=   COM_Invoke(pset, "_NewEnum")
   Loop, %   COM_Invoke(pset, "Count")
      If   COM_Enumerate(penm,pobj)=0
      {
         If  Not   Property
            MsgBox % COM_Invoke(pobj, "GetObjectText_")
         Else   sResult.=COM_Invoke(pobj, Property) . "`n"
         COM_Release(pobj)
      }
   COM_Release(penm)
   COM_Release(pset)
   COM_Release(psvc)
   Return   sResult
}


and another flavor of WMI:
Code:
COM_Init()
OpenEvent := "NetworkConfig.txt"
wLine := " +----------------- [Network Configuration] -----------------+ "
wLineBlank := " "
oFSO := COM_CreateObject("Scripting.FileSystemObject")
oNetworkConfig := COM_Invoke(oFSO,"CreateTextFile",OpenEvent, True)
COM_Invoke(oNetworkConfig,"WriteLine",wLine)
COM_Invoke(oNetworkConfig,"WriteLine",wLineBlank)
NameSpace     := "root\CIMV2"
Class         := "Win32_NetworkAdapterConfiguration WHERE IPEnabled = True"
Query         := "SELECT * FROM " . Class

objWMIService := COM_GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\" . Namespace)
colItems      := COM_Invoke(objWMIService, "ExecQuery", Query)
enum          := COM_Invoke(colItems, "_NewEnum")
while (COM_Enumerate(enum, objItem) = 0)
;For Each objItem in colItems
{
ItemIP  := COM_Invoke(objItem, "IPAddress")
MsgBox %ItemIP%
ItemSubnet    := COM_Invoke(objItem, "IPSubnet")
ItemGateway := COM_Invoke(objItem, "DefaultIPGateway")
ItemMAC := Com_Invoke(objItem, "MACAddress")
ItemDescription := COM_Invoke(objItem, "Description")
ItemDHCPEnabled := COM_Invoke(objItem, "DHCPEnabled")
}

COM_Invoke(oNetworkConfig,"WriteLine", ItemDescription) . "`n" . A_Tab
COM_Invoke(oNetworkConfig,"WriteLine"," IP Address :" ItemIP). "`n" . A_Tab
COM_Invoke(oNetworkConfig,"WriteLine"," Subnet Mask : " ItemSubnet). "`n" . A_Tab
COM_Invoke(oNetworkConfig,"WriteLine"," Default Gateway : " ItemGateway). "`n" . A_Tab
COM_Invoke(oNetworkConfig,"WriteLine"," MAC Address : " ItemMAC). "`n" . A_Tab
COM_Invoke(oNetworkConfig,"WriteLine", " DHCP Enabled : " ItemDHCPEnabled). A_Tab . "`n" . "`n"

   COM_Release(objWMIService), objWMIService := 0
   COM_Release(colItems), colItems :=
   COM_Release(oNetworkConfig), oNetworkConfig :=
   COM_Term()


Result of this one is, on my machine:
Quote:
+----------------- [Network Configuration] -----------------+

Intel(R) Wireless WiFi Link 4965AGN
IP Address :11047464
Subnet Mask : 11047512
Default Gateway : 11047608
MAC Address : 00:1D:E0:6F:B9:6D
DHCP Enabled : -1


I took these snippets from examples on this forum, I don't remember who posted it but thanks!

The problem is that the output of the WMI Query of the IP Address is
Quote:
{"192.168.1.101","fe80::e54f:44fc:874e:d93e"}
and I'm guessing that's what gives me this result just as you describe.. I did read on MSDN that the output of IPAddress is a String Array but I've dealt with those before and they never gave me this issues, those values werent numerical but names of drives (e.g. ST3500341AS)

Does anyone know how to best solve this?

Also, the Description of the 'IPEnabled' adapter is perfect and I can use that to search the registry:
Code:
Loop, HKEY_LOCAL_MACHINE, Software\Microsoft\Windows%A_Space%NT\CurrentVersion\NetworkCards, 1, 1
{
    if a_LoopRegType = key
        value =
    else
    {
        RegRead, value
        if ErrorLevel
            value = *error*
            If value = %ItemDescription%
      {
      Loop, %A_LoopRegKey%, %A_LoopRegSubKey%, 1, 1
                  if a_LoopRegType = key
         {
         servicename =
         }
         else
         {
        RegRead, servicename
        if ErrorLevel
            servicename = *error*
         }
 If servicename != %ItemDescription%
         {
            RegRead , ItemIP, HKLM, System\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\%servicename%, DHCPIPAddress
            RegRead , ItemSubnet, HKLM, System\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\%servicename%, DHCPSubnetMask
            RegRead , ItemGateway, HKLM, System\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\%servicename%, DHCPDefaultGateway
            RegRead , ItemDHCPEnabled, HKLM, System\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\%servicename%, EnableDHCP
            If ItemDHCPEnabled = 1
            ItemDHCPEnabled = True
         }
   }
}
}

Again, this works on my machine but not on others, I know there are other variables when the machine isn't configured via DHCP but even when I build in a check for DHCPEnabled and then search for different variables I still don't get the correct results, at least on XP..

I'm using this in a system info gathering tool we use to evaluate customer's systems and it would be nice if I could get it to work.. I don't know what'll happen in either of these examples when multiple network adapters are IPEnabled though so if someone even knows how to do that I'd be very grateful!

Thanks for taking a look!


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: chaosad, jrav, Yahoo [Bot] and 22 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group