Page 1 of 2
AHK - WMI - Snippets
Posted: 01 Oct 2013, 04:51
by jNizM
AHK - WMI - Snippets
>
Memory (Total, Free & Used)
>
MAC-Address
>
HDD Temperature (°C)
>
HDD Health (O.K. / Critical)
>
Processor Architecture (x86 / x64)
>
Drives
>
Network Card (Network Card, IPAddress, IPSubnet, DefaultIPGateway, DNS-Server, MAC-Address and DHCP)
>
AntiVirus Status (Requirement: Windows Vista+)
>
AntiSpyware Status (Requirement: Windows Vista+)
>
Firewall Status (Requirement: Windows Vista+)
> ...tba
Useful Links
>
Microsoft WMI Code Creator
>
WMI Übersicht und Beispiele
Re: AHK - WMI - Snippets
Posted: 01 Oct 2013, 04:52
by jNizM
[Memory]
Code: Select all
for objItem in ComObjGet("winmgmts:\\.\root\CIMV2").ExecQuery("SELECT * FROM Win32_OperatingSystem")
MsgBox, % "Total Memory:`t" Round((objItem.TotalVisibleMemorySize / 1024), 2) " MB`n"
. "Free Memory:`t" Round((objItem.FreePhysicalMemory / 1024), 2) " MB`n"
. "Used Memory:`t" Round(((objItem.TotalVisibleMemorySize - objItem.FreePhysicalMemory) / 1024), 2) " MB"
[Function] WMI_MEMORY()
Code: Select all
MEM := WMI_MEMORY()
MsgBox, % "Total Memory:`t" Round(MEM[1], 2) " MB`n"
. "Free Memory:`t" Round(MEM[2], 2) " MB`n"
. "Used Memory:`t" Round(MEM[3], 2) " MB`n"
WMI_MEMORY()
{
for objItem in ComObjGet("winmgmts:\\.\root\CIMV2").ExecQuery("SELECT * FROM Win32_OperatingSystem")
return, % { 1 : (objItem.TotalVisibleMemorySize / 1024), 2 : (objItem.FreePhysicalMemory / 1024)
, 3 : ((objItem.TotalVisibleMemorySize - objItem.FreePhysicalMemory) / 1024) }
}
Re: AHK - WMI - Snippets
Posted: 01 Oct 2013, 04:54
by jNizM
[MAC-Address]
Code: Select all
for objItem in ComObjGet("winmgmts:\\.\root\CIMV2").ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = TRUE")
MsgBox, % "MAC-Address:`t" objItem.MACAddress
Re: AHK - WMI - Snippets
Posted: 01 Oct 2013, 04:56
by jNizM
[HDD Temperature]
Code: Select all
for objItem in ComObjGet("winmgmts:\\.\root\WMI").ExecQuery("SELECT * FROM MSStorageDriver_ATAPISmartData")
{
loop, 361
{
i := A_Index - 1
if (objItem.VendorSpecific[i] = "194")
{
j := i + 5
MsgBox, % "HDD Temperature:`t" objItem.VendorSpecific[j] " °C"
}
}
}
[Function] WMI_HDD_TEMP()
Code: Select all
MsgBox, % "HDD Temperature:`t" WMI_HDD_TEMP() " °C"
WMI_HDD_TEMP()
{
for objItem in ComObjGet("winmgmts:\\.\root\WMI").ExecQuery("SELECT * FROM MSStorageDriver_ATAPISmartData")
{
loop, 361
{
i := A_Index - 1
if (objItem.VendorSpecific[i] = "194")
{
j := i + 5
return, % objItem.VendorSpecific[j]
}
}
}
}
Re: AHK - WMI - Snippets
Posted: 01 Oct 2013, 04:57
by jNizM
[HDD Health]
Code: Select all
for objItem in ComObjGet("winmgmts:\\.\root\WMI").ExecQuery("SELECT * FROM MSStorageDriver_FailurePredictStatus")
MsgBox, % (objItem.PredictFailure = "0") ? "HDD-Health is:`tO.K."
: (objItem.PredictFailure = "1") ? "HDD-Health is:`tCritical"
: "FAIL"
[Function] WMI_HDD_HEALTH()
Code: Select all
MsgBox, % "HDD-Health is:`t" WMI_HDD_HEALTH()
WMI_HDD_HEALTH()
{
for objItem in ComObjGet("winmgmts:\\.\root\WMI").ExecQuery("SELECT * FROM MSStorageDriver_FailurePredictStatus")
return, % (objItem.PredictFailure = "0") ? "O.K." : (objItem.PredictFailure = "1") ? "Critical" : "FAIL"
}
Re: AHK - WMI - Snippets
Posted: 01 Oct 2013, 05:01
by jNizM
[Processor Architecture]
Code: Select all
for objItem in ComObjGet("winmgmts:\\.\root\CIMV2").ExecQuery("SELECT * FROM Win32_Processor")
MsgBox, % (objItem.Architecture = "0") ? "x86"
: (objItem.Architecture = "9") ? "x64"
: "other Architecture"
[Function] Architecture()
Code: Select all
MsgBox, % "Architecture is: " . Architecture()
Architecture()
{
for objItem in ComObjGet("winmgmts:\\.\root\CIMV2").ExecQuery("SELECT * FROM Win32_Processor")
return, % (objItem.Architecture = "0") ? "x86" : (objItem.Architecture = "9") ? "x64" : "other Architecture"
}
Re: AHK - WMI - Snippets
Posted: 01 Oct 2013, 05:03
by jNizM
[Drives]
Code: Select all
for objItem in ComObjGet("winmgmts:\\.\root\CIMV2").ExecQuery("SELECT * FROM Win32_LogicalDisk")
MsgBox, % "Drives:`t`t" . objItem.Caption . "`n"
. "File System:`t" . objItem.FileSystem . "`n"
. "Drive Size:`t`t" . Round((objItem.Size / (1024 ** 3)), 2) . " GB`n"
. "Free Space:`t" . Round((objItem.FreeSpace / (1024 ** 3)), 2) . " GB`n"
. "Free Space:`t" . Round((100 * (objItem.FreeSpace / objItem.Size)), 2) . " %"
Re: AHK - WMI - Snippets
Posted: 01 Oct 2013, 05:05
by jNizM
[Network Card]
Gives info about: Network Card, IPAddress, IPSubnet, DefaultIPGateway, DNS-Server, MAC-Address and DHCP
Code: Select all
for objItem in ComObjGet("winmgmts:\\.\root\CIMV2").ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
{
if (objItem.IPAddress[0] = A_IPAddress1)
{
MsgBox, % "Description:`t" objItem.Description[0] "`n"
. "IPAddress:`t`t" objItem.IPAddress[0] "`n"
. "IPSubnet:`t`t" objItem.IPSubnet[0] "`n"
. "DefaultIPGateway:`t" objItem.DefaultIPGateway[0] "`n"
. "DNS-Server:`t" (objItem.DNSServerSearchOrder[1] = "") ? objItem.DNSServerSearchOrder[0] : objItem.DNSServerSearchOrder[0] ", " objItem.DNSServerSearchOrder[1] "`n"
. "MACAddress:`t" objItem.MACAddress "`n"
. "DHCPEnabled:`t" (objItem.DHCPEnabled[0] ? "No" : "Yes") "`n"
}
}
[Function] IPAddress(IPA)
Code: Select all
IPA := IPAddress()
MsgBox, % "Description:`t" IPA[1] "`n"
. "IPAddress:`t`t" IPA[2] "`n"
. "IPSubnet:`t`t" IPA[3] "`n"
. "DefaultIPGateway:`t" IPA[4] "`n"
. "DNS-Server:`t" IPA[5] "`n"
. "MACAddress:`t" IPA[6] "`n"
. "DHCPEnabled:`t" IPA[7] "`n"
IPAddress()
{
for objItem in ComObjGet("winmgmts:\\.\root\CIMV2").ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
{
if (objItem.IPAddress[0] = A_IPAddress1)
{
return, % { 1 : objItem.Description[0], 2 : objItem.IPAddress[0], 3 : objItem.IPSubnet[0], 4 : objItem.DefaultIPGateway[0]
, 5 : ((objItem.DNSServerSearchOrder[1] = "") ? objItem.DNSServerSearchOrder[0] : objItem.DNSServerSearchOrder[0] ", " objItem.DNSServerSearchOrder[1])
, 6 : objItem.MACAddress, 7 : (objItem.DHCPEnabled[0] ? "No" : "Yes") }
}
}
}
Re: AHK - WMI - Snippets
Posted: 01 Oct 2013, 05:09
by jNizM
[AntiVirus Status]
Requirement: Windows Vista+
Code: Select all
for objItem in ComObjGet("winmgmts:\\.\root\SecurityCenter2").ExecQuery("SELECT * FROM AntiVirusProduct")
MsgBox, % "AntiVirus:`t`t" . objItem.displayName . "`n"
. "Guid:`t`t" . objItem.instanceGuid . "`n"
. "ProductExe:`t" . objItem.pathToSignedProductExe . "`n"
. "ReportingExe:`t" . objItem.pathToSignedReportingExe . "`n"
. "RealTimeProtection:`t" . ((SubStr(DecTo(objItem.productState, 16), 3, 2) = 10) ? "Enabled" : "Disabled") "`n"
. "VirusDefUpdate:`t" . ((SubStr(DecTo(objItem.productState, 16), 5, 2) = 00) ? "Up to Date" : "Out of Date")
DecTo(n, to)
{
hex := "0123456789ABCDEF"
while (n > 0)
{
rem := mod(n, to)
n /= to
y := SubStr(hex, rem + 1, 1)
result = %y%%result%
}
return, "0" . result
}
Re: AHK - WMI - Snippets
Posted: 01 Oct 2013, 05:10
by jNizM
[AntiSpyware Status]
Requirement: Windows Vista+
Code: Select all
for objItem in ComObjGet("winmgmts:\\.\root\SecurityCenter2").ExecQuery("SELECT * FROM AntiSpywareProduct")
MsgBox, % "AntiSpyware:`t" . objItem.displayName . "`n"
. "Guid:`t`t" . objItem.instanceGuid . "`n"
. "ProductExe:`t" . objItem.pathToSignedProductExe . "`n"
. "ReportingExe:`t" . objItem.pathToSignedReportingExe . "`n"
. "RealTimeProtection:`t" . ((SubStr(DecTo(objItem.productState, 16), 3, 2) = 10) ? "Enabled" : "Disabled") "`n"
. "VirusDefUpdate:`t" . ((SubStr(DecTo(objItem.productState, 16), 5, 2) = 00) ? "Up to Date" : "Out of Date")
DecTo(n, to)
{
hex := "0123456789ABCDEF"
while (n > 0)
{
rem := mod(n, to)
n /= to
y := SubStr(hex, rem + 1, 1)
result = %y%%result%
}
return, "0" . result
}
Re: AHK - WMI - Snippets
Posted: 01 Oct 2013, 05:11
by jNizM
[Firewall Status]
Requirement: Windows Vista+
Code: Select all
for objItem in ComObjGet("winmgmts:\\.\root\SecurityCenter2").ExecQuery("SELECT * FROM FirewallProduct")
MsgBox, % "Firewall:`t`t" . objItem.displayName . "`n"
. "Guid:`t`t" . objItem.instanceGuid . "`n"
. "ProductExe:`t" . objItem.pathToSignedProductExe . "`n"
. "ReportingExe:`t" . objItem.pathToSignedReportingExe . "`n"
. "RealTimeProtection:`t" . ((SubStr(DecTo(objItem.productState, 16), 3, 2) = 10) ? "Enabled" : "Disabled") "`n"
. "VirusDefUpdate:`t" . ((SubStr(DecTo(objItem.productState, 16), 5, 2) = 00) ? "Up to Date" : "Out of Date")
DecTo(n, to)
{
hex := "0123456789ABCDEF"
while (n > 0)
{
rem := mod(n, to)
n /= to
y := SubStr(hex, rem + 1, 1)
result = %y%%result%
}
return, "0" . result
}
Re: AHK - WMI - Snippets
Posted: 21 Sep 2016, 01:28
by daorc
These are brilliant. Thank you!!
Re: AHK - WMI - Snippets
Posted: 26 Sep 2016, 07:30
by arcticir
Hi, there is a problem, how to get the amount of memory used by AHK itself?
Re: AHK - WMI - Snippets
Posted: 26 Sep 2016, 08:07
by jNizM
hey...
something like this (winapi - not wmi)
Code: Select all
OwnPID := DllCall("GetCurrentProcessId")
MsgBox % GetProcessMemoryInfo(OwnPID)
GetProcessMemoryInfo(ProcessID)
{
static PMC_EX, size := NumPut(VarSetCapacity(PMC_EX, 8 + A_PtrSize * 9, 0), PMC_EX, "uint")
if !(hProcess := DllCall("OpenProcess", "uint", 0x1000, "int", 0, "uint", ProcessID))
throw Exception("OpenProcess failed: " NT_STATUS, -1)
if !(DllCall("GetProcessMemoryInfo", "ptr", hProcess, "ptr", &PMC_EX, "uint", size))
if !(DllCall("psapi\GetProcessMemoryInfo", "ptr", hProcess, "ptr", &PMC_EX, "uint", size))
throw Exception("GetProcessMemoryInfo failed: " NT_STATUS, -1)
DllCall("CloseHandle", "ptr", hProcess)
return Round(NumGet(PMC_EX, 8 + A_PtrSize * 8, "uptr") / 1024)
}
Re: AHK - WMI - Snippets
Posted: 26 Sep 2016, 08:40
by arcticir
Thank you. This is the V2
Code: Select all
f:= GetProcessMemory(,"ob") ;Returns all the data as an object, Use "FormatBytes"
MsgBox % ObjTree(f)
MsgBox % GetProcessMemory(,"b") ;Returns the current memory usage, Use "FormatBytes"
MsgBox % GetProcessMemory() ;Returns the current memory usage
GetProcessMemory(Id:="",type:=""){
NumPut(VarSetCapacity(pmc, size := 8 + A_PtrSize * 9, 0), pmc, "uint")
,Id?"":Id:=DllCall("GetCurrentProcessId"),instr(type,"o")?f:=[]:"",instr(type,"b")?b:=1:""
if (hProcess := DllCall("OpenProcess", "uint", 0x1000, "int", 0, "uint", Id))
and DllCall("psapi\GetProcessMemoryInfo", "ptr", hProcess, "ptr", &pmc, "uint", size)
if f
loop,parse,PeakWorkingSetSize\WorkingSetSize\QuotaPeakPagedPoolUsage\QuotaPagedPoolUsage\QuotaPeakNonPagedPoolUsage\QuotaNonPagedPoolUsage\PagefileUsage\PeakPagefileUsage,\
s:=NumGet(pmc, 8 + A_PtrSize * A_Index, "uptr"),f[A_LoopField]:=b?FormatBytes(s):s
else
s:=NumGet(pmc, 8 + A_PtrSize * 8, "uptr"),f:=b?FormatBytes(s):Round(s / 1024)
DllCall("CloseHandle", "ptr", hProcess)
return f
}
FormatBytes(bytes){
Loop
If bytes > 999
bytes /= 1024.0
else return RTrim(SubStr(bytes,1,4),".") " " (A_Index=1 ? "bytes" : SubStr(" KMGTPEZY",A_Index,1) "B")
}
Re: AHK - WMI - Snippets
Posted: 26 Sep 2016, 14:41
by Tomer
Thanks!
Re: AHK - WMI - Snippets
Posted: 13 Oct 2016, 19:17
by Drugoy
jNizM wrote:[Network Card]
Gives info about: Network Card, IPAddress, IPSubnet, DefaultIPGateway, DNS-Server, MAC-Address and DHCP
Code: Select all
for objItem in ComObjGet("winmgmts:\\.\root\CIMV2").ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
{
if (objItem.IPAddress[0] = A_IPAddress1)
{
MsgBox, % "Description:`t" objItem.Description[0] "`n"
. "IPAddress:`t`t" objItem.IPAddress[0] "`n"
. "IPSubnet:`t`t" objItem.IPSubnet[0] "`n"
. "DefaultIPGateway:`t" objItem.DefaultIPGateway[0] "`n"
. "DNS-Server:`t" (objItem.DNSServerSearchOrder[1] = "") ? objItem.DNSServerSearchOrder[0] : objItem.DNSServerSearchOrder[0] ", " objItem.DNSServerSearchOrder[1] "`n"
. "MACAddress:`t" objItem.MACAddress "`n"
. "DHCPEnabled:`t" (objItem.DHCPEnabled[0] ? "No" : "Yes") "`n"
}
}
Poor usage of the first ternary operator results into bad output.
Re: AHK - WMI - Snippets
Posted: 14 Oct 2016, 00:42
by jNizM
Drugoy wrote:Poor usage of the first ternary operator results into bad output.
Haha I know
(was one of my first wmi - never changed them)
For this I use the WinAPI directly to get the data...
Re: AHK - WMI - Snippets
Posted: 16 Oct 2016, 15:14
by Drugoy
Could you show how you do that using WinAPI?
Re: AHK - WMI - Snippets
Posted: 17 Oct 2016, 01:24
by jNizM
Here are some examples:
https://github.com/jNizM/AHK_Scripts/tr ... er/src/net
Will add more as soon as I cleanuped my code