Page 1 of 1

Common WMI tasks

Posted: 05 Feb 2014, 23:47
by tank
tank wrote:In light of my error in judgement and search skills. I am going to Reference
. jNizM's more a refined results and more useful as-is
Compiled from wmi-tasks-com-with-ahk-l
Posted here for ease. All credit to SBC and Shajul

WMI Tasks COM with AHK_L

Some examples from MSDN [url=http://msdn.microsof...5(v=VS.85).aspx]http://msdn.microsof...5(v=VS.85).aspx[/url].

New users can convert all the MSDN examples given to Native AHK code, I am just giving some examples


CD-ROM drives details:

Code: Select all

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

colItems := objWMIService.ExecQuery("Select * from Win32_CDROMDrive")._NewEnum 
While colItems[objItem] 
MsgBox % "Device ID: " . objItem.DeviceID 
. "`nDescription: " . objItem.Description 
. "`nName: " . objItem.Name


Ping without ping.exe :

Code: Select all

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

colPings := objWMIService.ExecQuery("Select * From Win32_PingStatus where Address = 'www.google.com'")._NewEnum ;or ip address like 192.168.1.1 

While colPings[objStatus] 
{ 
If (objStatus.StatusCode="" or objStatus.StatusCode<>0) 
MsgBox Computer did not respond. 
Else 
MsgBox Computer responded. 
}


Computer system details:

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


Operating system details and free physical memory:

Code: Select all

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

colSettings := objWMIService.ExecQuery("Select * from Win32_OperatingSystem")._NewEnum 

Gui, Add, ListView, x0 y0 r45 w400 h500 vMyLV, Attribute|Value 
GuiControl, -Redraw, MyLV 

While colSettings[objOSItem] 
{ 
LV_Add("","Build Number" ,objOSItem.BuildNumber ) 
LV_Add("","Build Type" ,objOSItem.BuildType ) 
LV_Add("","Caption" ,objOSItem.Caption ) 
LV_Add("","CountryCode" ,objOSItem.CountryCode ) 
LV_Add("","CreationClassName" ,objOSItem.CreationClassName ) 
LV_Add("","CSDVersion" ,objOSItem.CSDVersion ) 
LV_Add("","CSName" ,objOSItem.CSName ) 
LV_Add("","CurrentTimeZone" ,objOSItem.CurrentTimeZone ) 
LV_Add("","Distributed" ,objOSItem.Distributed ) 
LV_Add("","EncryptionLevel" ,objOSItem.EncryptionLevel ) 
LV_Add("","FreePhysicalMemory" ,objOSItem.FreePhysicalMemory ) 
LV_Add("","FreeSpaceInPagingFiles" ,objOSItem.FreeSpaceInPagingFiles ) 
LV_Add("","FreeVirtualMemory" ,objOSItem.FreeVirtualMemory ) 
LV_Add("","InstallDate" ,objOSItem.InstallDate ) 
LV_Add("","LargeSystemCache" ,objOSItem.LargeSystemCache ) 
LV_Add("","LastBootUpTime" ,objOSItem.LastBootUpTime ) 
LV_Add("","LocalDateTime" ,objOSItem.LocalDateTime ) 
LV_Add("","Locale" ,objOSItem.Locale ) 
LV_Add("","Manufacturer" ,objOSItem.Manufacturer ) 
LV_Add("","MaxNumberOfProcesses" ,objOSItem.MaxNumberOfProcesses ) 
LV_Add("","MaxProcessMemorySize" ,objOSItem.MaxProcessMemorySize ) 
LV_Add("","Name" ,objOSItem.Name ) 
LV_Add("","NumberOfLicensedUsers" ,objOSItem.NumberOfLicensedUsers ) 
LV_Add("","NumberOfProcesses" ,objOSItem.NumberOfProcesses ) 
LV_Add("","NumberOfUsers" ,objOSItem.NumberOfUsers ) 
LV_Add("","Organization" ,objOSItem.Organization ) 
LV_Add("","OSLanguage" ,objOSItem.OSLanguage ) 
LV_Add("","OSType" ,objOSItem.OSType ) 
LV_Add("","Primary" ,objOSItem.Primary ) 
LV_Add("","ProductType" ,objOSItem.ProductType ) 
LV_Add("","RegisteredUser" ,objOSItem.RegisteredUser ) 
LV_Add("","SerialNumber" ,objOSItem.SerialNumber ) 
LV_Add("","ServicePackMajorVersion" ,objOSItem.ServicePackMajorVersion ) 
LV_Add("","ServicePackMinorVersion" ,objOSItem.ServicePackMinorVersion ) 
LV_Add("","SizeStoredInPagingFiles" ,objOSItem.SizeStoredInPagingFiles ) 
LV_Add("","Status" ,objOSItem.Status ) 
LV_Add("","SuiteMask" ,objOSItem.SuiteMask ) 
LV_Add("","SystemDevice" ,objOSItem.SystemDevice ) 
LV_Add("","SystemDirectory" ,objOSItem.SystemDirectory ) 
LV_Add("","SystemDrive" ,objOSItem.SystemDrive ) 
LV_Add("","TotalSwapSpaceSize" ,objOSItem.TotalSwapSpaceSize ) 
LV_Add("","TotalVirtualMemorySize" ,objOSItem.TotalVirtualMemorySize ) 
LV_Add("","TotalVisibleMemorySize" ,objOSItem.TotalVisibleMemorySize ) 
LV_Add("","Version" ,objOSItem.Version ) 
LV_Add("","WindowsDirectory" ,objOSItem.WindowsDirectory ) 
} 

GuiControl, +Redraw, MyLV 
LV_ModifyCol() 
Gui, Show, w400 h500, Operating System Details 
Return 

GuiClose: 
ExitApp


Properties of the mouse used on computer:

Code: Select all

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

colItems := objWMIService.ExecQuery("Select * from Win32_PointingDevice")._NewEnum 
While colItems[objItem] 
MsgBox % "Description: " . objItem.Description 
. "`nDevice ID: " . objItem.DeviceID 
. "`nDevice Interface: " . objItem.DeviceInterface 
. "`nDouble Speed Threshold: " . objItem.DoubleSpeedThreshold 
. "`nHandedness: " . objItem.Handedness 
. "`nHardware Type: " . objItem.HardwareType 
. "`nINF File Name: " . objItem.InfFileName 
. "`nINF Section: " . objItem.InfSection 
. "`nManufacturer: " . objItem.Manufacturer 
. "`nName: " . objItem.Name 
. "`nNumber Of Buttons: " . objItem.NumberOfButtons 
. "`nPNP Device ID: " . objItem.PNPDeviceID 
. "`nPointing Type: " . objItem.PointingType 
. "`nQuad Speed Threshold: " . objItem.QuadSpeedThreshold 
. "`nResolution: " . objItem.Resolution 
. "`nSample Rate: " . objItem.SampleRate 
. "`nSynch: " . objItem.Synch


List Desktop settings:

Code: Select all

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

colItems := objWMIService.ExecQuery("Select * from Win32_Desktop")._NewEnum 
While colItems[objItem] 
MsgBox % "Border Width: " . objItem.BorderWidth 
. "`nCaption: " . objItem.Caption 
. "`nCool Switch: " . objItem.CoolSwitch 
. "`nCursor Blink Rate: " . objItem.CursorBlinkRate 
. "`nDescription: " . objItem.Description 
. "`nDrag Full Windows: " . objItem.DragFullWindows 
. "`nGrid Granularity: " . objItem.GridGranularity 
. "`nIcon Spacing: " . objItem.IconSpacing 
. "`nIcon Title Face Name: " . objItem.IconTitleFaceName 
. "`nIcon Title Size: " . objItem.IconTitleSize 
. "`nIcon Title Wrap: " . objItem.IconTitleWrap 
. "`nName: " . objItem.Name 
. "`nPattern: " . objItem.Pattern 
. "`nScreen Saver Active: " . objItem.ScreenSaverActive 
. "`nScreen Saver Executable: " . objItem.ScreenSaverExecutable 
. "`nScreen Saver Secure: " . objItem.ScreenSaverSecure 
. "`nScreen Saver Timeout: " . objItem.ScreenSaverTimeout 
. "`nSetting ID: " . objItem.SettingID 
. "`nWallpaper: " . objItem.Wallpaper 
. "`nWallpaper Stretched: " . objItem.WallpaperStretched 
. "`nWallpaper Tiled: " . objItem.WallpaperTiled


List BIOS details

Code: Select all

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

colSettings := objWMIService.ExecQuery("Select * from Win32_BIOS")._NewEnum 

While colSettings[objBiosItem] 
{ 
MsgBox % "BIOSVersion : " . objBiosItem.BIOSVersion 
. "`nBuildNumber : " . objBiosItem.BuildNumber 
. "`nCaption : " . objBiosItem.Caption 
. "`nCurrentLanguage : " . objBiosItem.CurrentLanguage 
. "`nDescription : " . objBiosItem.Description 
. "`nInstallableLanguages : " . objBiosItem.InstallableLanguages 
. "`nInstallDate : " . objBiosItem.InstallDate 
. "`nListOfLanguages : " . objBiosItem.ListOfLanguages 
. "`nManufacturer : " . objBiosItem.Manufacturer 
. "`nName : " . objBiosItem.Name 
. "`nPrimaryBIOS : " . objBiosItem.PrimaryBIOS 
. "`nReleaseDate : " . objBiosItem.ReleaseDate 
. "`nSerialNumber2 : " . objBiosItem.SerialNumber 
. "`nSMBIOSBIOSVersion : " . objBiosItem.SMBIOSBIOSVersion 
. "`nSMBIOSMajorVersion : " . objBiosItem.SMBIOSMajorVersion 
. "`nSMBIOSMinorVersion : " . objBiosItem.SMBIOSMinorVersion 
. "`nSMBIOSPresent : " . objBiosItem.SMBIOSPresent 
. "`nSoftwareElementID : " . objBiosItem.SoftwareElementID 
. "`nSoftwareElementState : " . objBiosItem.SoftwareElementState 
. "`nStatus : " . objBiosItem.Status 
. "`nTargetOperatingSystem : " . objBiosItem.TargetOperatingSystem 
. "`nVersion : " . objBiosItem.Version 
}

Memory:

Code: Select all

;http://msdn.microsoft.com/en-us/library/aa394347%28v=vs.85%29.aspx 
PropertyList := "BankLabel,Capacity,Caption,CreationClassName,DataWidth,Description,DeviceLocator,FormFactor," 
. "HotSwappable,InstallDate,InterleaveDataDepth,InterleavePosition,Manufacturer,MemoryType,Model,Name," 
. "OtherIdentifyingInfo,PartNumber,PositionInRow,PoweredOn,Removable,Replaceable,SerialNumber,SKU,Speed," 
. "Status,Tag,TotalWidth,TypeDetail,Version" 

objWMIService := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\" . A_ComputerName . "\root\cimv2") 
WQLQuery = Select * From Win32_PhysicalMemory 
colMemInfo := objWMIService.ExecQuery(WQLQuery)._NewEnum 
While colMemInfo[objMemInfo] 
Loop, Parse, PropertyList, `, 
MemInfo .= A_index = 1 ? objMemInfo[A_LoopField] . "`n" : "`t" . A_LoopField . ":`t" . objMemInfo[A_LoopField] . "`n" 
logfile = %A_ScriptDir%\MemoryInfoList.txt 
FileDelete, %logfile% 
FileAppend, %MemInfo%, %logfile% 
Run, Notepad "%logfile%"

Onboard Devices:

Code: Select all

;http://msdn.microsoft.com/en-us/library/aa394238%28VS.85%29.aspx 
PropertyList := "Caption,CreationClassName,Description,DeviceType,Enabled," 
. "HotSwappable,InstallDate,Manufacturer,Model,Name,OtherIdentifyingInfo," 
. "PartNumber,PoweredOn,Removable,Replaceable,SerialNumber,SKU,Status,Tag,Version" 

objWMIService := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\" . A_ComputerName . "\root\cimv2") 
WQLQuery = Select * From Win32_OnBoardDevice 
colOnboard := objWMIService.ExecQuery(WQLQuery)._NewEnum 
While colOnboard[objOnboard] 
Loop, Parse, PropertyList, `, 
OnboardDeviceList .= A_index = 1 ? objOnboard[A_LoopField] . "`n" : "`t" . A_LoopField . ":`t" . objOnboard[A_LoopField] . "`n" 

logfile = %A_ScriptDir%\OnboardDeviceList.txt 
FileDelete, %logfile% 
FileAppend, %OnboardDeviceList%, %logfile% 
Run, Notepad "%logfile%"

Hard Drives:

Code: Select all

;http://msdn.microsoft.com/en-us/library/aa394132%28v=vs.85%29.aspx 
PropertyList := "Caption,Availability,BytesPerSector,CompressionMethod,ConfigManagerErrorCode," 
. "ConfigManagerUserConfig,CreationClassName,DefaultBlockSize,Description,DeviceID,ErrorCleared," 
. "ErrorDescription,ErrorMethodology,FirmwareRevision,Index,InstallDate,InterfaceType,LastErrorCode," 
. "Manufacturer,MaxBlockSize,MaxMediaSize,MediaLoaded,MediaType,MinBlockSize,Model,Name,NeedsCleaning," 
. "NumberOfMediaSupported,Partitions,PNPDeviceID,PowerManagementSupported,SCSIBus,SCSILogicalUnit," 
. "SCSIPort,SCSITargetId,SectorsPerTrack,SerialNumber,Signature,Size,Status,StatusInfo," 
. "SystemCreationClassName,SystemName,TotalCylinders,TotalHeads,TotalSectors,TotalTracks,TracksPerCylinder" 

objWMIService := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\" . A_ComputerName . "\root\cimv2") 
WQLQuery = Select * From Win32_DiskDrive 
colDiskDrive := objWMIService.ExecQuery(WQLQuery)._NewEnum 
While colDiskDrive[objDiskDrive] 
Loop, Parse, PropertyList, `, 
DiskDriveInfo .= A_index = 1 ? objDiskDrive[A_LoopField] . "`n" : "`t" . A_LoopField . ":`t" . objDiskDrive[A_LoopField] . "`n" 

logfile = %A_ScriptDir%\DiskDriveInfo.txt 
FileDelete, %logfile% 
FileAppend, %DiskDriveInfo%, %logfile% 
Run, Notepad "%logfile%"

Motherboard:

Code: Select all

;http://msdn.microsoft.com/en-us/library/aa394072%28v=vs.85%29.aspx 
PropertyList := "Caption,CreationClassName,Depth,Description,Height,HostingBoard,HotSwappable,InstallDate," 
. "Manufacturer,Model,Name,OtherIdentifyingInfo,PartNumber,PoweredOn,Product,Removable,Replaceable," 
. "RequirementsDescription,RequiresDaughterBoard,SerialNumber,SKU,SlotLayout,SpecialRequirements," 
. "Status,Tag,Version,Weight,Width" 
objWMIService := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\" . A_ComputerName . "\root\cimv2") 
WQLQuery = Select * From Win32_BaseBoard 
colMBInfo := objWMIService.ExecQuery(WQLQuery)._NewEnum 
While colMBInfo[objMBInfo] 
Loop, Parse, PropertyList, `, 
MatherBoardInfo .= A_LoopField . ":`t" . objMBInfo[A_LoopField] . "`n" 
MsgBox % MatherBoardInfo

CPU:

Code: Select all

;http://msdn.microsoft.com/en-us/library/aa394373%28v=vs.85%29.aspx 
PropertyList := "AddressWidth,Architecture,Availability,Caption,ConfigManagerErrorCode," 
. "ConfigManagerUserConfig,CpuStatus,CreationClassName,CurrentClockSpeed,CurrentVoltage," 
. "DataWidth,Description,DeviceID,ErrorCleared,ErrorDescription,ExtClock,Family,InstallDate," 
. "L2CacheSize,L2CacheSpeed,L3CacheSize,L3CacheSpeed,LastErrorCode,Level,LoadPercentage," 
. "Manufacturer,MaxClockSpeed,Name,NumberOfCores,NumberOfLogicalProcessors,OtherFamilyDescription," 
. "PNPDeviceID,PowerManagementSupported,ProcessorId,ProcessorType,Revision,Role,SocketDesignation," 
. "Status,StatusInfo,Stepping,SystemCreationClassName,SystemName,UniqueId,UpgradeMethod,Version,VoltageCaps" 

objWMIService := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\" . A_ComputerName . "\root\cimv2") 
WQLQuery = Select * From Win32_Processor 
colCPU := objWMIService.ExecQuery(WQLQuery)._NewEnum 
While colCPU[objCPU] 
Loop, Parse, PropertyList, `, 
CPUInfo .= A_LoopField . ":`t" . objCPU[A_LoopField] . "`n" 

logfile = %A_ScriptDir%\CPUInfo.txt 
FileDelete, %logfile% 
FileAppend, %CPUInfo%, %logfile% 
Run, Notepad "%logfile%"

Product:

Code: Select all

;http://msdn.microsoft.com/en-us/library/aa394105%28v=vs.85%29.aspx 
PropertyList := "Caption,Description,IdentifyingNumber,Name,SKUNumber,UUID,Vendor,Version" 
objWMIService := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\" . A_ComputerName . "\root\cimv2") 
WQLQuery = Select * From Win32_ComputerSystemProduct 
colSysProduct := objWMIService.ExecQuery(WQLQuery)._NewEnum 
While colSysProduct[objSysProduct] 
Loop, Parse, PropertyList, `, 
ProductInfo .= A_LoopField . ":`t" . objSysProduct[A_LoopField] . "`n" 
MsgBox % ProductInfo

****



List of Installed Applications

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%"


List of Installed Audio and Video Codecs

Code: Select all

;http://msdn.microsoft.com/en-us/library/aa394091%28VS.85%29.aspx 
PropertyList := "Caption,AccessMask,Archive,Compressed,CompressionMethod," 
. "CreationClassName,CreationDate,CSCreationClassName,CSName,Description," 
. "Drive,EightDotThreeFileName,Encrypted,EncryptionMethod,Extension," 
. "FileName,FileSize,FileType,FSCreationClassName,FSName,Group,Hidden," 
. "InstallDate,InUseCount,LastAccessed,LastModified,Manufacturer,Name," 
. "Path,Readable,Status,System,Version,Writeable" 
WMIClass := "Win32_CodecFile" 
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%"


List of Fonts installed with Applications

Code: Select all

;http://msdn.microsoft.com/en-us/library/aa394150%28VS.85%29.aspx 
PropertyList := "ActionID,Caption,Description,Direction,File,FontTitle," 
. "Name,SoftwareElementID,SoftwareElementState,TargetOperatingSystem,Version" 
WMIClass := "Win32_FontInfoAction" 
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%"


List of User Accounts

Code: Select all

;http://msdn.microsoft.com/en-us/library/aa394507%28v=vs.85%29.aspx 
PropertyList := "Caption,AccountType,Description,Disabled,Domain,FullName,InstallDate,LocalAccount," 
. "Lockout,Name,PasswordChangeable,PasswordExpires,PasswordRequired,SID,SIDType,Status" 
WMIClass := "Win32_UserAccount" 
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%"


List of System Accounts

Code: Select all

;http://msdn.microsoft.com/en-us/library/aa394507%28v=vs.85%29.aspx 
PropertyList := "Name,Caption,Description,Domain,InstallDate,LocalAccount,SID,SIDType,Status" 
WMIClass := "Win32_SystemAccount" 
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%"


List of Shared Folders and Printes

Code: Select all

;http://msdn.microsoft.com/en-us/library/aa394507%28v=vs.85%29.aspx 
PropertyList := "Name,AccessMask,AllowMaximum,Caption,Description,InstallDate," 
. "MaximumAllowed,Path,Status,Type" 
WMIClass := "Win32_Share" 
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%"


List of running processes (narrowed down to those which path can be retrieved)

Code: Select all

strComputer := "." 
objWMIService := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\" . strComputer . "\root\cimv2") 
colItems := objWMIService.ExecQuery("SELECT Caption,ExecutablePath,ProcessID FROM Win32_Process where ExecutablePath is not null")._NewEnum 
While colItems[objItem] 
MsgBox % "Caption: " . objItem.Caption 
. "`nProcessID: " . objItem.ProcessID 
. "`nExecutablePath: " . objItem.ExecutablePath 

Re: Common WMI tasks

Posted: 06 Feb 2014, 00:35
by lexikos
There are color tags in many of the examples, which do not work.

Instead of using ._NewEnum and While, you can use a for-loop. For example, see below.

The string passed to ComObjGet can be shortened quite a bit in many cases. The local computer name \\.\ can be omitted. {impersonationLevel=impersonate}! and root\cimv2 can be omitted, provided that the default impersonation level and default namespace in the registry have their standard values.

For me, this makes it much easier to write WMI scripts from memory, without having to refer to documentation or other code.

Code: Select all

; Get a list of executables running on this computer.
for obj in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process where ExecutablePath is not null")
	s .= obj.ExecutablePath "`n"
Sort s, U  ; Remove duplicates.
MsgBox % s
I just learned that you can also get a specific WMI class or instance directly, though I'm not sure of the details. For example, this shows the file system of drive C:

Code: Select all

MsgBox % ComObjGet("winmgmts:root\cimv2:Win32_LogicalDisk=""C:""").FileSystem

Get Process properties via AHK using WMI

Posted: 06 Feb 2014, 01:31
by hoppfrosch
Get Process properties:

Re: Common WMI tasks

Posted: 06 Feb 2014, 01:50
by jNizM

Re: Common WMI tasks

Posted: 08 Feb 2014, 09:03
by Menixator
jNizM wrote:AHK - WMI - Snippets
... -.-
LOL :P

Re: Common WMI tasks

Posted: 09 Feb 2014, 14:43
by tank
In light of my error in judgement and search skills. I am going to Reference
jNizM wrote:AHK - WMI - Snippets
... -.-
at the top of this. I was in the process of trying to remove duplicates from this thread and merge the topic to his when i realized for the most part that the entire thread was more refined versions of these calls. Most of the ones I compiled to this list are raw examples where His is more a refined results and more useful as-is

Re: Common WMI tasks

Posted: 14 Aug 2014, 10:49
by arcticir
I ask, can detect whether there is a touch screen, and get its message?


Like this, but I did not find the direct detection of the touch screen

Code: Select all

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

colItems := objWMIService.ExecQuery("Select * from Win32_PointingDevice")._NewEnum
While colItems[objItem]
MsgBox % "Description: " . objItem.Description
. "`nDevice ID: " . objItem.DeviceID
. "`nDevice Interface: " . objItem.DeviceInterface
. "`nDouble Speed Threshold: " . objItem.DoubleSpeedThreshold
. "`nHandedness: " . objItem.Handedness
. "`nHardware Type: " . objItem.HardwareType
. "`nINF File Name: " . objItem.InfFileName
. "`nINF Section: " . objItem.InfSection
. "`nManufacturer: " . objItem.Manufacturer
. "`nName: " . objItem.Name
. "`nNumber Of Buttons: " . objItem.NumberOfButtons
. "`nPNP Device ID: " . objItem.PNPDeviceID
. "`nPointing Type: " . objItem.PointingType
. "`nQuad Speed Threshold: " . objItem.QuadSpeedThreshold
. "`nResolution: " . objItem.Resolution
. "`nSample Rate: " . objItem.SampleRate
. "`nSynch: " . objItem.Synch

Re: Common WMI tasks

Posted: 13 Oct 2016, 01:42
by Tomer
very useful! tnx

Re: Common WMI tasks

Posted: 15 Mar 2023, 02:41
by LatinSuD
R.I.P. WMI. Microsoft has killed it today.