AHK - WMI - Snippets

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

AHK - WMI - Snippets

01 Oct 2013, 04:51

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
Last edited by jNizM on 06 Feb 2014, 02:11, edited 4 times in total.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: AHK - WMI - Snippets

01 Oct 2013, 04:52

[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) }
}
Last edited by jNizM on 06 Feb 2014, 02:17, edited 7 times in total.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: AHK - WMI - Snippets

01 Oct 2013, 04:54

[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.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: AHK - WMI - Snippets

01 Oct 2013, 04:56

[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]
            }
        }
    }
}
Last edited by jNizM on 06 Feb 2014, 02:21, edited 3 times in total.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: AHK - WMI - Snippets

01 Oct 2013, 04:57

[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"
}
Last edited by jNizM on 06 Feb 2014, 02:43, edited 2 times in total.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: AHK - WMI - Snippets

01 Oct 2013, 05:01

[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"
}
Last edited by jNizM on 06 Feb 2014, 03:00, edited 2 times in total.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: AHK - WMI - Snippets

01 Oct 2013, 05:03

[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.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: AHK - WMI - Snippets

01 Oct 2013, 05:05

[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") }
        }
    }
}
Last edited by jNizM on 06 Feb 2014, 03:24, edited 5 times in total.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: AHK - WMI - Snippets

01 Oct 2013, 05:09

[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
}
Last edited by jNizM on 06 Feb 2014, 03:28, edited 1 time in total.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: AHK - WMI - Snippets

01 Oct 2013, 05:10

[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
}
Last edited by jNizM on 06 Feb 2014, 03:28, edited 1 time in total.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: AHK - WMI - Snippets

01 Oct 2013, 05:11

[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
}
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
daorc
Posts: 8
Joined: 27 Mar 2015, 10:51

Re: AHK - WMI - Snippets

21 Sep 2016, 01:28

These are brilliant. Thank you!!
arcticir
Posts: 693
Joined: 17 Nov 2013, 11:32

Re: AHK - WMI - Snippets

26 Sep 2016, 07:30

Hi, there is a problem, how to get the amount of memory used by AHK itself? :)
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: AHK - WMI - Snippets

26 Sep 2016, 08:07

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)
}
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
arcticir
Posts: 693
Joined: 17 Nov 2013, 11:32

Re: AHK - WMI - Snippets

26 Sep 2016, 08:40

Thank you. This is the V2 :D

Image

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")
}
User avatar
Tomer
Posts: 366
Joined: 21 Aug 2016, 05:11

Re: AHK - WMI - Snippets

26 Sep 2016, 14:41

Thanks!
Drugoy
Posts: 48
Joined: 11 Jun 2016, 07:37
Contact:

Re: AHK - WMI - Snippets

13 Oct 2016, 19:17

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.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: AHK - WMI - Snippets

14 Oct 2016, 00:42

Drugoy wrote:Poor usage of the first ternary operator results into bad output.
Haha I know :D (was one of my first wmi - never changed them)

For this I use the WinAPI directly to get the data...
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
Drugoy
Posts: 48
Joined: 11 Jun 2016, 07:37
Contact:

Re: AHK - WMI - Snippets

16 Oct 2016, 15:14

Could you show how you do that using WinAPI?
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: AHK - WMI - Snippets

17 Oct 2016, 01:24

Here are some examples: https://github.com/jNizM/AHK_Scripts/tr ... er/src/net
Will add more as soon as I cleanuped my code
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: charlie89, gwarble, Spikea and 130 guests