VBScript获取硬盘与分区个数

标签: , , , , ,

VBScript 脚本调用 WMI 获取硬盘与分区个数及其对应关系。

'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

原文链接:http://bbs.bathome.net/viewthread.php?tid=18774

随机文章:

  1. Python调用COM组件
  2. JavaScript 中小数和大整数的精度丢失
  3. This document was successfully checked as XHTML 1.0 Strict!
  4. Firefox插件JSView
  5. 用VBS修改Windows用户密码

留下回复