|
| 1 | +;GetDiskInfoFromWmi.au3 |
| 2 | + |
| 3 | +#include <Array.au3> |
| 4 | + |
| 5 | + |
| 6 | +Func _GetDiskInfoFromWmi(ByRef $aDiskList, ByRef $aPrtitionList, $bAddTableHeader = 1) |
| 7 | + ; Name: _GetDiskInfoFromWmi |
| 8 | + ; Author: htcfreek (Heiko) - https://github.com/htcfreek/AutoIt-Scripts |
| 9 | + ; Version: 1.0 |
| 10 | + ; License: GNU LGPLv3 |
| 11 | + ; Input parameter: ByRef $aDiskList = Array var for list of disks.; ByRef $aPrtitionList = Array var for list of partitions.; [$bAddTableHeader = 1] = Should array tables have a header rowß |
| 12 | + ; Output parameter: none |
| 13 | + ; Required includes: <Array.au3> |
| 14 | + |
| 15 | + |
| 16 | + ; Initialize function wide vars |
| 17 | + Local $aDisks[0][7] |
| 18 | + Local $aPartitions[0][12] |
| 19 | + Local $iDiskArrayCount = 0 ; Initialize counter to write some disk data later in correct array row. |
| 20 | + Local $iPartArrayCount = 0 ; Initialize counter to write partition data later in correct array row. |
| 21 | + |
| 22 | + |
| 23 | + ; Add Array header |
| 24 | + if ($bAddTableHeader = 1) Then |
| 25 | + $sDiskHeader = "DiskNum" & "||" & "DiskDeviceID" & "||" & "DiskModel" & "||" & "DiskSize" & "||" & "DiskPartitionCount" & "||" & "DiskInitType" & "||" & "SystemIsBootedFromDisk" |
| 26 | + _ArrayAdd($aDisks, $sDiskHeader, 0, "||") |
| 27 | + $sPartitionHeader = "DiskNum" & "||" & "PartitionNum" & "||" & "PartitionID" & "||" & "PartitionType" & "||" & "PartitionIsPrimary" & "||" & "PartIsBootPartition" & "||" & "PartitionLetter" & "||" & "PartitionLabel" & "||" & "PartitionSizeTotal" & "||" & "PartitionSizeUsed" & "||" & "PartitionSizeFree" & "||" & "SystemIsBootedFromPartition" |
| 28 | + _ArrayAdd($aPartitions, $sPartitionHeader, 0, "||") |
| 29 | + $iDiskArrayCount += 1 |
| 30 | + $iPartArrayCount += 1 |
| 31 | + EndIf |
| 32 | + |
| 33 | + ; Get Information from WMI |
| 34 | + Local $oWmiInstance = ObjGet('winmgmts:\\' & @ComputerName & '\root\cimv2') |
| 35 | + If (IsObj($oWmiInstance)) And (Not @error) Then |
| 36 | + ; Get Disks |
| 37 | + Local $oPhysicalDisks = $oWmiInstance.ExecQuery('Select * from Win32_DiskDrive') |
| 38 | + For $oDisk In $oPhysicalDisks |
| 39 | + ; Add Disk-Data to Array |
| 40 | + Local $iDisk = $oDisk.Index |
| 41 | + Local $sNewDisk = $iDisk & "||" & $oDisk.DeviceID & "||" & $oDisk.Model & "||" & $oDisk.Size & "||" & $oDisk.Partitions & "||" & "<DiskInitStyle>" & "||" & False |
| 42 | + _ArrayAdd($aDisks, $sNewDisk, 0, "||") |
| 43 | + |
| 44 | + ;Get Partitions |
| 45 | + Local $oPartitions = $oWmiInstance.ExecQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" & $oDisk.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition") |
| 46 | + For $oPartition In $oPartitions |
| 47 | + ; Add Part data to Array |
| 48 | + Local $iPartition = $oPartition.Index |
| 49 | + Local $sNewPart = $iDisk & "||" & $iPartition & "||" & $oPartition.DeviceID & "||" & $oPartition.Type & "||" & $oPartition.PrimaryPartition & "||" & $oPartition.BootPartition & "||" & "" & "||" & "" & "||" & $oPartition.Size & "||" & "" & "||" & "" & "||" & False |
| 50 | + _ArrayAdd($aPartitions, $sNewPart, 0, "||") |
| 51 | + |
| 52 | + ; Set DiskIniStyle |
| 53 | + if StringRegExp ( $oPartition.Type, "^GPT.*") Then |
| 54 | + $aDisks[$iDiskArrayCount][5] = "GPT" |
| 55 | + Else |
| 56 | + $aDisks[$iDiskArrayCount][5] = "MBR" |
| 57 | + EndIf |
| 58 | + |
| 59 | + ; Get LogicalDiks |
| 60 | + Local $oLogicalDisks = $oWmiInstance.ExecQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" & $oPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition") |
| 61 | + For $oLogicalDisk In $oLogicalDisks |
| 62 | + ;Add logical disk data to array |
| 63 | + $aPartitions[$iPartArrayCount][6] = $oLogicalDisk.DeviceID |
| 64 | + $aPartitions[$iPartArrayCount][7] = $oLogicalDisk.VolumeName |
| 65 | + $aPartitions[$iPartArrayCount][8] = $oLogicalDisk.Size ; Value of LogicalDisk.Size is different to Size of DiskPartiton.Size!! |
| 66 | + $aPartitions[$iPartArrayCount][9] = ($oLogicalDisk.Size - $oLogicalDisk.FreeSpace) |
| 67 | + $aPartitions[$iPartArrayCount][10] = $oLogicalDisk.FreeSpace |
| 68 | + |
| 69 | + ;Detect SystemBootDisk |
| 70 | + if $oLogicalDisk.DeviceID = Envget("SystemDrive") Then |
| 71 | + $aDisks[$iDiskArrayCount][6] = True |
| 72 | + $aPartitions[$iPartArrayCount][11] = True |
| 73 | + EndIf |
| 74 | + Next |
| 75 | + |
| 76 | + ; ArrayCounter + 1 |
| 77 | + $iPartArrayCount += 1 |
| 78 | + Next |
| 79 | + |
| 80 | + ; ArrayCounter + 1 |
| 81 | + $iDiskArrayCount += 1 |
| 82 | + Next |
| 83 | + EndIf |
| 84 | + |
| 85 | + ;Return Data |
| 86 | + $aDiskList = $aDisks |
| 87 | + $aPrtitionList = $aPartitions |
| 88 | +EndFunc ;==>_GetDiskInfoFromWmi |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +; Example Usage |
| 93 | +;------------------ |
| 94 | +Local $aOutputDisks |
| 95 | +Local $aOutputPartitions |
| 96 | +_GetDiskInfoFromWmi($aOutputDisks, $aOutputPartitions, 1) |
| 97 | + |
| 98 | +_ArrayDisplay($aOutputDisks, "Disks") |
| 99 | +_ArrayDisplay($aOutputPartitions, "Partitons") |
0 commit comments