Help with COM/WMI Computer Info

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
coder_chick
Posts: 235
Joined: 05 Nov 2015, 10:43

Help with COM/WMI Computer Info

19 May 2017, 23:24

I find the COM/WMI stuff to be very hard to understand. The code just doesn't make sense to me and a lot of the posts don't include comments to help understand what it is doing under the hood.

The following code I found is great. However, there are a few things I would like to do.
1) I don't need the pop up. I would instead just like to append everything to log file
2) In addition, how can I make the output for one of those items a variable? For example, if I wanted the CurrentTimeZone to pop up in a msgbox?
3) Is there a better way of doing this and can someone add comments to help me understand what is happening? I honestly don't even know where to start with these.

Code: Select all

strComputer := "."
objWMIService := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\" . strComputer . "\root\cimv2")

colSettings := objWMIService.ExecQuery("Select * from Win32_ComputerSystem")._NewEnum
Gui, Add, ListView, x0 y0 r45 w400 h500 vMyLV, Attribute|Value
GuiControl, -Redraw, MyLV 

While colSettings[strCSItem]
{
  LV_Add("","AdminPasswordStatus",strCSItem.AdminPasswordStatus )
  LV_Add("","AutomaticResetBootOption",strCSItem.AutomaticResetBootOption )
  LV_Add("","AutomaticResetCapability",strCSItem.AutomaticResetCapability )
  LV_Add("","BootROMSupported",strCSItem.BootROMSupported )
  LV_Add("","BootupState",strCSItem.BootupState )
  LV_Add("","Caption",strCSItem.Caption )
  LV_Add("","ChassisBootupState",strCSItem.ChassisBootupState )
  LV_Add("","CurrentTimeZone",strCSItem.CurrentTimeZone )
  LV_Add("","DaylightInEffect",strCSItem.DaylightInEffect )
  LV_Add("","Description",strCSItem.Description )
  LV_Add("","Domain",strCSItem.Domain )
  LV_Add("","DomainRole",strCSItem.DomainRole )
  LV_Add("","EnableDaylightSavingsTime",strCSItem.EnableDaylightSavingsTime )
  LV_Add("","FrontPanelResetStatus",strCSItem.FrontPanelResetStatus )
  LV_Add("","InfraredSupported",strCSItem.InfraredSupported )
  LV_Add("","KeyboardPasswordStatus",strCSItem.KeyboardPasswordStatus )
  LV_Add("","Manufacturer",strCSItem.Manufacturer )
  LV_Add("","Model",strCSItem.Model )
  LV_Add("","Name",strCSItem.Name )
  LV_Add("","NetworkServerModeEnabled",strCSItem.NetworkServerModeEnabled )
  LV_Add("","NumberOfLogicalProcessors",strCSItem.NumberOfLogicalProcessors )
  LV_Add("","NumberOfProcessors",strCSItem.NumberOfProcessors )
  LV_Add("","OEMStringArray",strCSItem.OEMStringArray )
  LV_Add("","PartOfDomain",strCSItem.PartOfDomain )
  LV_Add("","PauseAfterReset",strCSItem.PauseAfterReset )
  LV_Add("","PowerOnPasswordStatus",strCSItem.PowerOnPasswordStatus )
  LV_Add("","PowerState",strCSItem.PowerState )
  LV_Add("","PowerSupplyState",strCSItem.PowerSupplyState )
  LV_Add("","PrimaryOwnerContact",strCSItem.PrimaryOwnerContact )
  LV_Add("","PrimaryOwnerName",strCSItem.PrimaryOwnerName )
  LV_Add("","ResetCapability",strCSItem.ResetCapability )
  LV_Add("","ResetCount",strCSItem.ResetCount )
  LV_Add("","ResetLimit",strCSItem.ResetLimit )
  LV_Add("","Roles",strCSItem.Roles )
  LV_Add("","Status",strCSItem.Status )
  LV_Add("","SupportContactDescription",strCSItem.SupportContactDescription )
  LV_Add("","SystemStartupDelay",strCSItem.SystemStartupDelay )
  LV_Add("","SystemStartupOptions",strCSItem.SystemStartupOptions )
  LV_Add("","SystemStartupSetting",strCSItem.SystemStartupSetting )
  LV_Add("","SystemType",strCSItem.SystemType )
  LV_Add("","ThermalState",strCSItem.ThermalState )
  LV_Add("","TotalPhysicalMemory",Round(strCSItem.TotalPhysicalMemory/(1024*1024),0) . " MB")
  LV_Add("","UserName",strCSItem.UserName )
  LV_Add("","WakeUpType",strCSItem.WakeUpType )
  LV_Add("","Workgroup",strCSItem.Workgroup )
}  
GuiControl, +Redraw, MyLV 
LV_ModifyCol()
Gui, Show, w400 h500, Computer Details
Return

GuiClose:
ExitApp
https://autohotkey.com/board/topic/6096 ... ith-ahk-l/
♥ ❤ ❥ coder_chick ♥ ❤ ❥
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Help with COM/WMI Computer Info

20 May 2017, 06:31

I might do something like this:

Code: Select all

q:: ;WMI get info
strComputer := "."
objWMIService := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\" strComputer "\root\cimv2")
colSettings := objWMIService.ExecQuery("Select * from Win32_ComputerSystem")._NewEnum

vList := "AdminPasswordStatus,AutomaticResetBootOption,AutomaticResetCapability,BootROMSupported,BootupState,Caption,ChassisBootupState,CurrentTimeZone,DaylightInEffect,Description,Domain,DomainRole,EnableDaylightSavingsTime,FrontPanelResetStatus,InfraredSupported,KeyboardPasswordStatus,Manufacturer,Model,Name,NetworkServerModeEnabled,NumberOfLogicalProcessors,NumberOfProcessors,OEMStringArray,PartOfDomain,PauseAfterReset,PowerOnPasswordStatus,PowerState,PowerSupplyState,PrimaryOwnerContact,PrimaryOwnerName,ResetCapability,ResetCount,ResetLimit,Roles,Status,SupportContactDescription,SystemStartupDelay,SystemStartupOptions,SystemStartupSetting,SystemType,ThermalState,TotalPhysicalMemory,UserName,WakeUpType,Workgroup"

while colSettings[strCSItem]
{
	vOutput := ""
	Loop, Parse, vList, % ","
	{
		;if (A_LoopField = "TotalPhysicalMemory")
		;	vTemp := Round(strCSItem.TotalPhysicalMemory/(1024*1024),0) " MB"
		;else
			vTemp := strCSItem[A_LoopField]
		vOutput .= A_LoopField "`t" vTemp "`r`n"
	}
	MsgBox, % vOutput
	MsgBox, % strCSItem.CurrentTimeZone
	vMyVar := "CurrentTimeZone"
	MsgBox, % strCSItem[vMyVar]
	Clipboard := vOutput
}
objWMIService := colSettings := strCSItem := ""
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
coder_chick
Posts: 235
Joined: 05 Nov 2015, 10:43

Re: Help with COM/WMI Computer Info

20 May 2017, 11:00

That was helpful.

Code: Select all

;WMI get info
strComputer := "."
objWMIService := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\" strComputer "\root\cimv2")
colSettings := objWMIService.ExecQuery("Select * from Win32_ComputerSystem")._NewEnum

vList := "AdminPasswordStatus,AutomaticResetBootOption,AutomaticResetCapability,BootROMSupported,BootupState,Caption,ChassisBootupState,CurrentTimeZone,DaylightInEffect,Description,Domain,DomainRole,EnableDaylightSavingsTime,FrontPanelResetStatus,InfraredSupported,KeyboardPasswordStatus,Manufacturer,Model,Name,NetworkServerModeEnabled,NumberOfLogicalProcessors,NumberOfProcessors,OEMStringArray,PartOfDomain,PauseAfterReset,PowerOnPasswordStatus,PowerState,PowerSupplyState,PrimaryOwnerContact,PrimaryOwnerName,ResetCapability,ResetCount,ResetLimit,Roles,Status,SupportContactDescription,SystemStartupDelay,SystemStartupOptions,SystemStartupSetting,SystemType,ThermalState,TotalPhysicalMemory,UserName,WakeUpType,Workgroup"

while colSettings[strCSItem]
{
	vOutput := ""
	Loop, Parse, vList, % ","
	{
		;if (A_LoopField = "TotalPhysicalMemory")
		;	vTemp := Round(strCSItem.TotalPhysicalMemory/(1024*1024),0) " MB"
		;else
        vTemp := strCSItem[A_LoopField]
		vOutput .= A_LoopField "`t" vTemp "`r`n"
	}
	MsgBox, % vOutput  ;display everything in msgbox
    
    UserName := strCSItem.UserName  ; create variable with one of the items from the list
    msgbox, %UserName%
}
objWMIService := colSettings := strCSItem := ""
return
Can you help me understand what the following lines are doing?

Code: Select all

strComputer := "."
objWMIService := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\" strComputer "\root\cimv2")
colSettings := objWMIService.ExecQuery("Select * from Win32_ComputerSystem")._NewEnum

Code: Select all

objWMIService := colSettings := strCSItem := ""
♥ ❤ ❥ coder_chick ♥ ❤ ❥
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Help with COM/WMI Computer Info

20 May 2017, 11:42

WMI is a core object provider of the Windows operating system, and, with the line below:

Code: Select all

objWMIService := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\" . strComputer . "\root\cimv2")
we get a reference to a COM component provided by WMI, depending on the parameter (here: "winmgmts:{impersonationLevel=impersonate}!\\" . strComputer . "\root\cimv2"), so that we can take advantage of this object from ahk.
This object has methods - for instance, at is root, the object returned by the ComObjGetcall above has the method ExecQuery which param syntax observes SQL one's: "Select * from Win32_ComputerSystem"
_NewEnummethod is called intenally each time you use for-loop in ahk and returns an enumerator object and is apllied here on the returned value of the ExecQuery method so that we can loop throught colSettings. Each key (strCSItem) retrieved via the enumeration (see especially: https://msdn.microsoft.com/en-us/librar ... s.85).aspx) has its own properties - about the computer it seems (for example: AutomaticResetBootOption, Manufacturer and so on)

Concening:

Code: Select all

objWMIService := colSettings := strCSItem := ""
this simply frees all global objects (see: CombjGet)


Hope this helps


[EDIT]: It's the best I can help you, the most of mechanisms involved using WMI provider escape me.
my scripts
User avatar
coder_chick
Posts: 235
Joined: 05 Nov 2015, 10:43

Re: Help with COM/WMI Computer Info

20 May 2017, 12:33

Very helpful. Thanks for taking the time to explain.
♥ ❤ ❥ coder_chick ♥ ❤ ❥
User avatar
coder_chick
Posts: 235
Joined: 05 Nov 2015, 10:43

Re: Help with COM/WMI Computer Info

23 May 2017, 03:34

One additional question, do I always need to include the vlist or is there a way to just dump all available fields and information to file without having to create the list? While the list is nice as I can specify exactly which items I want, in some cases, I may want them all, and I dont want to have to list all those field names each time.
♥ ❤ ❥ coder_chick ♥ ❤ ❥
User avatar
coder_chick
Posts: 235
Joined: 05 Nov 2015, 10:43

Re: Help with COM/WMI Computer Info

23 May 2017, 13:17

^^^^Anyone know how I can dump all the fields (Manufacturer, model, name, etc.) without listing them out one by one.
♥ ❤ ❥ coder_chick ♥ ❤ ❥
RickC
Posts: 299
Joined: 27 Oct 2013, 08:32

Re: Help with COM/WMI Computer Info

23 May 2017, 18:59

coder_chick wrote:^^^^Anyone know how I can dump all the fields (Manufacturer, model, name, etc.) without listing them out one by one.
Example:

Code: Select all

;http://msdn.microsoft.com/en-us/library/aa394068(VS.85).aspx
PropertyList := "ProductName,Accesses,Attributes,Caption,Description,IdentifyingNumber,"
	. "InstallDate,InstallState,LastUse,Name,Status,Vendor,Version"
WMIClass := "Win32_SoftwareFeature"
objWMIService := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\" . A_ComputerName . "\root\cimv2")
WQLQuery := "Select * From " . WMIClass
colProperties := objWMIService.ExecQuery(WQLQuery)._NewEnum
While colProperties[objProperty]
	Loop, Parse, PropertyList, `,
		Result .= A_index = 1 ? objProperty[A_LoopField] . "`n" : "`t" . A_LoopField . ":`t" . objProperty[A_LoopField] . "`n"
logfile = %A_ScriptDir%\%WMIClass%.txt
FileDelete, %logfile%
FileAppend, %Result%, %logfile%
Run, Notepad "%logfile%"
Hope this helps...
User avatar
coder_chick
Posts: 235
Joined: 05 Nov 2015, 10:43

Re: Help with COM/WMI Computer Info

23 May 2017, 19:15

Thanks, Rick, but you still have the list of items you want to retrieve in the properlist.

I want to know if there is a way to obtain everything Win32_ComputerSystem without have to list out all the following. In other words, assume I want any available items.

AdminPasswordStatus,AutomaticResetBootOption,AutomaticResetCapability,BootROMSupported,BootupState,Caption,ChassisBootupState,CurrentTimeZone,DaylightInEffect,Description,Domain,DomainRole,EnableDaylightSavingsTime,FrontPanelResetStatus,InfraredSupported,KeyboardPasswordStatus,Manufacturer,Model,Name,NetworkServerModeEnabled,NumberOfLogicalProcessors,NumberOfProcessors,OEMStringArray,PartOfDomain,PauseAfterReset,PowerOnPasswordStatus,PowerState,PowerSupplyState,PrimaryOwnerContact,PrimaryOwnerName,ResetCapability,ResetCount,ResetLimit,Roles,Status,SupportContactDescription,SystemStartupDelay,SystemStartupOptions,SystemStartupSetting,SystemType,ThermalState,TotalPhysicalMemory,UserName,WakeUpType,Workgroup
♥ ❤ ❥ coder_chick ♥ ❤ ❥
User avatar
Alguimist
Posts: 428
Joined: 05 Oct 2015, 16:41
Contact:

Re: Help with COM/WMI Computer Info

23 May 2017, 23:36

coder_chick wrote:I want to know if there is a way to obtain everything Win32_ComputerSystem without have to list out all the following. In other words, assume I want any available items.

Code: Select all

Output := ""
For Item in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_ComputerSystem") {
    For Key in ComObjGet("winmgmts:\\.\root\CIMV2:Win32_ComputerSystem").Properties_ {
        Output .= Key.Name . "=" . Item[Key.Name] . "`r`n"
    }
}

MsgBox % Output
It can also be done in the command line:
wmic computersystem get * /format:list

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], mikeyww, Rohwedder, Spawnova and 135 guests