AHK - WMI - Snippets
AHK - WMI - Snippets
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
> 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
Last edited by jNizM on 06 Feb 2014, 02:11, edited 4 times in total.
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
Re: AHK - WMI - Snippets
[Memory]
[Function] WMI_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"
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) }
}
Last edited by jNizM on 06 Feb 2014, 02:17, edited 7 times in total.
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
Re: AHK - WMI - Snippets
[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
Last edited by jNizM on 06 Feb 2014, 02:17, edited 4 times in total.
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
Re: AHK - WMI - Snippets
[HDD Temperature]
[Function] WMI_HDD_TEMP()
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"
}
}
}
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]
}
}
}
}
Last edited by jNizM on 06 Feb 2014, 02:21, edited 3 times in total.
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
Re: AHK - WMI - Snippets
[HDD Health]
[Function] WMI_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"
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"
}
Last edited by jNizM on 06 Feb 2014, 02:43, edited 2 times in total.
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
Re: AHK - WMI - Snippets
[Processor Architecture]
[Function] 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"
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"
}
Last edited by jNizM on 06 Feb 2014, 03:00, edited 2 times in total.
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
Re: AHK - WMI - Snippets
[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) . " %"
Last edited by jNizM on 06 Feb 2014, 03:01, edited 2 times in total.
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
Re: AHK - WMI - Snippets
[Network Card]
Gives info about: Network Card, IPAddress, IPSubnet, DefaultIPGateway, DNS-Server, MAC-Address and DHCP
[Function] IPAddress(IPA)
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"
}
}
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") }
}
}
}
Last edited by jNizM on 06 Feb 2014, 03:24, edited 5 times in total.
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
Re: AHK - WMI - Snippets
[AntiVirus Status]
Requirement: Windows Vista+
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
}
Last edited by jNizM on 06 Feb 2014, 03:28, edited 1 time in total.
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
Re: AHK - WMI - Snippets
[AntiSpyware Status]
Requirement: Windows Vista+
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
}
Last edited by jNizM on 06 Feb 2014, 03:28, edited 1 time in total.
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
Re: AHK - WMI - Snippets
[Firewall Status]
Requirement: Windows Vista+
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
}
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
Re: AHK - WMI - Snippets
These are brilliant. Thank you!!
Re: AHK - WMI - Snippets
Hi, there is a problem, how to get the amount of memory used by AHK itself?
Re: AHK - WMI - Snippets
hey...
something like this (winapi - not wmi)
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)
}
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
Re: AHK - WMI - Snippets
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
Poor usage of the first ternary operator results into bad output.jNizM wrote:[Network Card]
Gives info about: Network Card, IPAddress, IPSubnet, DefaultIPGateway, DNS-Server, MAC-Address and DHCPCode: 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" } }
Re: AHK - WMI - Snippets
Haha I know (was one of my first wmi - never changed them)Drugoy wrote:Poor usage of the first ternary operator results into bad output.
For this I use the WinAPI directly to get the data...
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
Re: AHK - WMI - Snippets
Could you show how you do that using WinAPI?
Re: AHK - WMI - Snippets
Here are some examples: https://github.com/jNizM/AHK_Scripts/tr ... er/src/net
Will add more as soon as I cleanuped my code
Will add more as soon as I cleanuped my code
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile