- 'VBScript 脚本调用 WMI 获取硬盘与分区个数及其对应关系
- 'By IUnknown @ www.bathome.net 批处理之家
-
- 'Ref: http://msdn.microsoft.com/en-us/library/windows/desktop/aa393259(v=vs.85).aspx
-
- LineSeparator = String(79, "=")
- ItemSeparator = String(79, "-")
-
- RunInCmd
-
- Set objWbemServices = GetObject("winmgmts:\\.\root\cimv2")
-
- Set colDiskDrives = objWbemServices.ExecQuery("SELECT * FROM Win32_DiskDrive")
- WScript.Echo "硬盘个数: " & colDiskDrives.Count
- WScript.Echo
- For Each objDrive In colDiskDrives
- WScript.Echo "硬盘名称: " & objDrive.Caption & " | " & objDrive.DeviceID
- Set colPartitions = objWbemServices.ExecQuery( _
- "ASSOCIATORS OF {" & objDrive.SystemProperties_("__RelPath") & "} " _
- & " WHERE AssocClass = Win32_DiskDriveToDiskPartition")
- WScript.Echo "物理区数: " & colPartitions.Count
- WScript.Echo "逻辑区数: " & objDrive.Partitions
- WScript.Echo LineSeparator
- For Each objPartition In colPartitions
- WScript.Echo "物理分区: " & objPartition.DeviceID _
- & " | " & IIF(objPartition.PrimaryPartition, "主分区", "扩展分区")
- WScript.Echo ItemSeparator
- Set colLogicalDisks = objWbemServices.ExecQuery( _
- "ASSOCIATORS OF {" & objPartition.SystemProperties_("__RelPath") & "} " _
- & " WHERE AssocClass = Win32_LogicalDiskToPartition")
- For Each objLogicalDisk In colLogicalDisks
- WScript.Echo "逻辑分区: " & objLogicalDisk.DeviceID _
- & " | " & objLogicalDisk.FileSystem
- Next
- If colLogicalDisks.Count=0 Then
- WScript.Echo "未分配盘符"
- End If
- WScript.Echo LineSeparator
- Next
- Next
-
- Function IIF(expr, TruePart, FalsePart)
- If (expr) Then
- IIF = TruePart
- Else
- IIF = FalsePart
- End If
- End Function
-
- Function strdup(s, n)
- Dim a() : ReDim a(n)
- strdup = Join(a, s)
- End Function
-
- Sub RunInCmd()
- If InStr(1, WScript.FullName, "wscript.exe", 1) Then
- CreateObject("WScript.Shell").Run "cmd.exe /d /c " _
- & "title IUnknown @ www.bathome.net | cscript.exe -nologo """ _
- & WScript.ScriptFullName & """ &pause"
- WScript.Quit
- End If
- End Sub
复制代码
|