forked from rcmaehl/WhyNotWin11
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetDiskInfoFromWmi.au3
153 lines (126 loc) · 6.9 KB
/
GetDiskInfoFromWmi.au3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
; Includes
;----------
#include-once
#include <Array.au3>
#cs
===============================================================================================================================
Title ...............: _GetDiskInfoFromWmi (GitHub: https://github.com/htcfreek/AutoIt-Scripts)
Version .............: 1.4.1
License .............: GNU LGPLv3
AutoIt Version ......: 3.3.14.5+
Language ............: English
Description .........: Get disk and partition informations from WMI.
Author ..............: htcfreek (Heiko) - https://github.com/htcfreek [original]
Modified ............:
Required includes ...: Array.au3
Dll .................:
===============================================================================================================================
CHANGELOG:
2021-07-06 (v1.4.1)
Fixed: Code styling
2021-07-05 (v1.4)
Fixed: Typos in script and example.
2021-07-04 (v1.3)
Fixed: Typos
Added: DiskType filter
Added: Constants
Added: Required includes
Added: Error handling
Changed: Improved comments
Changed: Disk property header renamed: WindowsRunningOnDisk (SystemRoot) -> WindowsRunningOnDisk (SystemDisk)
Changed: Partition property header renamed: PartitionIsSystemRoot -> PartitionIsSystemDisk
2021-07-02 (v1.2)
Added: Disk properties Manufacturer, InterfaceType, MediaType, SerialNumber, Status
Added: Partition property: Filesystem
Changed: Disk property header renamed: SystemIsBootedFromDisk -> WindowsRunningOnDisk (SystemRoot)
Changed: Partition property header renamed: SystemIsBootedFromPartition -> PartitionIsSystemRoot
2021-07-02 (v1.0)
New: Initial release
#ce
; Global constants
; -----------------
Global Const $DiskInfoWmi_TableHeader_Yes = 1
Global Const $DiskInfoWmi_TableHeader_No = 0
Global Const $DiskInfoWmi_DiskType_All = "%"
Global Const $DiskInfoWmi_DiskType_External = "External%"
Global Const $DiskInfoWmi_DiskType_Removable = "Removable%"
Global Const $DiskInfoWmi_DiskType_Fixed = "Fixed%"
Global Const $DiskInfoWmi_DiskType_Unknown = "Unknown%"
; Function
; ---------
Func _GetDiskInfoFromWmi(ByRef $aDiskList, ByRef $aPartitionList, $bAddTableHeader = $DiskInfoWmi_TableHeader_Yes, $sFilterDiskType = $DiskInfoWmi_DiskType_All)
; Name ...............: _GetDiskInfoFromWmi
; Author .............: htcfreek (Heiko) - https://github.com/htcfreek
; Input parameter ....: ByRef $aDiskList = Array var for list of disks.
; ByRef $aPartitionList = Array var for list of partitions.
; [$bAddTableHeader = $DiskInfoWmi_TableHeader_Yes] = Should array tables have a header row. (Values: 0|1 or $DiskInfoWmi_TableHeader_Yes|$DiskInfoWmi_TableHeader_No)
; [$sFilterDiskType = $DiskInfoWmi_DiskType_All] = Which type of disk should be included in result. (Values: $DiskInfoWmi_DiskType_All|$DiskInfoWmi_DiskType_External|$DiskInfoWmi_DiskType_Removable|$DiskInfoWmi_DiskType_Fixed|$DiskInfoWmi_DiskType_Unknown)
; Output parameter ...: none
; On WMI-Error .......: @error = 1
; Initialize function wide vars
Local $aDisks[0][12]
Local $aPartitions[0][13]
Local $iDiskArrayCount = 0 ; Initialize counter to write some disk data later in correct array row.
Local $iPartArrayCount = 0 ; Initialize counter to write partition data later in correct array row.
; Add Array header
If ($bAddTableHeader = 1) Then
Local $sDiskHeader = "DiskNum" & "||" & "DiskDeviceID" & "||" & "DiskManufacturer" & "||" & "DiskModel" & "||" & "DiskInterfaceType" & "||" & "DiskMediaType" & "||" & "DiskSerialNumber" & "||" & "DiskState" & "||" & "DiskSize" & "||" & "DiskInitType" & "||" & "DiskPartitionCount" & "||" & "WindowsRunningOnDisk (SystemDrive)"
_ArrayAdd($aDisks, $sDiskHeader, 0, "||")
Local $sPartitionHeader = "DiskNum" & "||" & "PartitionNum" & "||" & "PartitionID" & "||" & "PartitionType" & "||" & "PartitionIsPrimary" & "||" & "PartitionIsBootPartition" & "||" & "PartitionLetter" & "||" & "PartitionLabel" & "||" & "PartitionFileSystem" & "||" & "PartitionSizeTotal" & "||" & "PartitionSizeUsed" & "||" & "PartitionSizeFree" & "||" & "PartitionIsSystemDrive"
_ArrayAdd($aPartitions, $sPartitionHeader, 0, "||")
$iDiskArrayCount += 1
$iPartArrayCount += 1
EndIf
; Get Information from WMI
Local $oWmiInstance = ObjGet('winmgmts:\\' & @ComputerName & '\root\cimv2')
If (IsObj($oWmiInstance)) And (Not @error) Then
; Get Disks
Local $oPhysicalDisks = $oWmiInstance.ExecQuery('Select * from Win32_DiskDrive WHERE MediaType like "' & $sFilterDiskType & '"')
For $oDisk In $oPhysicalDisks
; Add Disk data to Array
Local $iDisk = $oDisk.Index
Local $sNewDisk = $iDisk & "||" & $oDisk.DeviceID & "||" & $oDisk.Manufacturer & "||" & $oDisk.Model & "||" & $oDisk.InterfaceType & "||" & $oDisk.MediaType & "||" & $oDisk.SerialNumber & "||" & $oDisk.Status & "||" & $oDisk.Size & "||" & "<DiskInitStyle>" & "||" & $oDisk.Partitions & "||" & False
_ArrayAdd($aDisks, $sNewDisk, 0, "||")
; Get Partitions
Local $oPartitions = $oWmiInstance.ExecQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" & $oDisk.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition")
For $oPartition In $oPartitions
; Add Partition data to Array
Local $iPartition = $oPartition.Index
Local $sNewPart = $iDisk & "||" & $iPartition & "||" & $oPartition.DeviceID & "||" & $oPartition.Type & "||" & $oPartition.PrimaryPartition & "||" & $oPartition.BootPartition & "||" & "" & "||" & "" & "||" & "" & "||" & $oPartition.Size & "||" & "" & "||" & "" & "||" & False
_ArrayAdd($aPartitions, $sNewPart, 0, "||")
; Set DiskInitStyle
If StringRegExp($oPartition.Type, "^GPT.*") Then
$aDisks[$iDiskArrayCount][9] = "GPT"
Else
$aDisks[$iDiskArrayCount][9] = "MBR"
EndIf
; Get logical disks
Local $oLogicalDisks = $oWmiInstance.ExecQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" & $oPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition")
For $oLogicalDisk In $oLogicalDisks
; Add logical disk data to array
$aPartitions[$iPartArrayCount][6] = $oLogicalDisk.DeviceID
$aPartitions[$iPartArrayCount][7] = $oLogicalDisk.VolumeName
$aPartitions[$iPartArrayCount][8] = $oLogicalDisk.FileSystem
$aPartitions[$iPartArrayCount][9] = $oLogicalDisk.Size ; Value of LogicalDisk.Size is different to Size of DiskPartition.Size!!
$aPartitions[$iPartArrayCount][10] = ($oLogicalDisk.Size - $oLogicalDisk.FreeSpace)
$aPartitions[$iPartArrayCount][11] = $oLogicalDisk.FreeSpace
; Detect SystemBootDisk
If $oLogicalDisk.DeviceID = EnvGet("SystemDrive") Then
$aDisks[$iDiskArrayCount][11] = True
$aPartitions[$iPartArrayCount][12] = True
EndIf
Next
; Array counter (Partitions) + 1
$iPartArrayCount += 1
Next
; Array counter (Disks) + 1
$iDiskArrayCount += 1
Next
Else
; If WMI-Error then set @error
SetError(1)
EndIf
; Return Data
$aDiskList = $aDisks
$aPartitionList = $aPartitions
EndFunc ;==>_GetDiskInfoFromWmi