Board logo

标题: [原创] VBScript 脚本调用 WMI 获取硬盘与分区个数及其对应关系 [打印本页]

作者: IUnknown    时间: 2012-8-29 22:02     标题: VBScript 脚本调用 WMI 获取硬盘与分区个数及其对应关系

  1. 'VBScript 脚本调用 WMI 获取硬盘与分区个数及其对应关系
  2. 'By IUnknown @ www.bathome.net 批处理之家
  3. 'Ref: http://msdn.microsoft.com/en-us/library/windows/desktop/aa393259(v=vs.85).aspx
  4. LineSeparator = String(79, "=")
  5. ItemSeparator = String(79, "-")
  6. RunInCmd
  7. Set objWbemServices = GetObject("winmgmts:\\.\root\cimv2")
  8. Set colDiskDrives = objWbemServices.ExecQuery("SELECT * FROM Win32_DiskDrive")
  9. WScript.Echo "硬盘个数: " & colDiskDrives.Count
  10. WScript.Echo
  11. For Each objDrive In colDiskDrives
  12.     WScript.Echo "硬盘名称: " & objDrive.Caption & " | " & objDrive.DeviceID
  13.     Set colPartitions = objWbemServices.ExecQuery( _
  14.         "ASSOCIATORS OF {" & objDrive.SystemProperties_("__RelPath") & "} " _
  15.         & " WHERE AssocClass = Win32_DiskDriveToDiskPartition")
  16.     WScript.Echo "物理区数: " & colPartitions.Count
  17.     WScript.Echo "逻辑区数: " & objDrive.Partitions
  18.     WScript.Echo LineSeparator
  19.     For Each objPartition In colPartitions
  20.         WScript.Echo "物理分区: " & objPartition.DeviceID _
  21.             & " | " & IIF(objPartition.PrimaryPartition, "主分区", "扩展分区")
  22.         WScript.Echo ItemSeparator
  23.         Set colLogicalDisks = objWbemServices.ExecQuery( _
  24.             "ASSOCIATORS OF {" & objPartition.SystemProperties_("__RelPath") & "} " _
  25.             & " WHERE AssocClass = Win32_LogicalDiskToPartition")
  26.         For Each objLogicalDisk In colLogicalDisks
  27.             WScript.Echo "逻辑分区: " & objLogicalDisk.DeviceID _
  28.                 & " | " & objLogicalDisk.FileSystem
  29.         Next
  30.         If colLogicalDisks.Count=0 Then
  31.             WScript.Echo "未分配盘符"
  32.         End If
  33.         WScript.Echo LineSeparator
  34.     Next
  35. Next
  36. Function IIF(expr, TruePart, FalsePart)
  37.   If (expr) Then
  38.     IIF = TruePart
  39.   Else
  40.     IIF = FalsePart
  41.   End If
  42. End Function
  43. Function strdup(s, n)
  44.     Dim a() : ReDim a(n)
  45.     strdup = Join(a, s)
  46. End Function
  47. Sub RunInCmd()
  48.   If InStr(1, WScript.FullName, "wscript.exe", 1) Then
  49.     CreateObject("WScript.Shell").Run "cmd.exe /d /c " _
  50.         & "title IUnknown @ www.bathome.net | cscript.exe -nologo """ _
  51.         & WScript.ScriptFullName & """ &pause"
  52.     WScript.Quit
  53.   End If
  54. End Sub
复制代码

作者: IUnknown    时间: 2012-8-29 22:54

JavaScript/JScript 脚本调用 WMI 获取硬盘与分区个数及其对应关系
  1. @set @n=0//& cscript.exe -nologo -e:jscript "%~f0" &pause&exit/b
  2. // JavaScript/JScript 脚本调用 WMI 获取硬盘与分区个数及其对应关系
  3. // By IUnknown @ www.bathome.net 批处理之家
  4. // Ref: http://msdn.microsoft.com/en-us/library/windows/desktop/aa393259(v=vs.85).aspx
  5. LineSeparator = strdup("=", 79);
  6. ItemSeparator = strdup("-", 40);
  7. objWbemServices = GetObject("winmgmts:\\\\.\\root\\cimv2");
  8. colDiskDrives = objWbemServices.ExecQuery("SELECT * FROM Win32_DiskDrive");
  9. WScript.Echo( "硬盘个数: " + colDiskDrives.Count );
  10. WScript.Echo( );
  11. enumItems = new Enumerator(colDiskDrives);
  12. for (; !enumItems.atEnd(); enumItems.moveNext())
  13. {
  14.     objDrive = enumItems.item();
  15.     WScript.Echo( "硬盘名称: " + objDrive.Caption + " | " + objDrive.DeviceID );
  16.     colPartitions = objWbemServices.ExecQuery(
  17.         "ASSOCIATORS OF {" + objDrive.SystemProperties_("__RelPath") + "} "
  18.         + " WHERE AssocClass = Win32_DiskDriveToDiskPartition");
  19.     WScript.Echo( "物理区数: " + colPartitions.Count );
  20.     WScript.Echo( "逻辑区数: " + objDrive.Partitions );
  21.     WScript.Echo( LineSeparator );
  22.     enumItems2 = new Enumerator(colPartitions);
  23.     for (; !enumItems2.atEnd(); enumItems2.moveNext())
  24.     {
  25.         objPartition = enumItems2.item();
  26.         WScript.Echo( "物理分区: " + objPartition.DeviceID
  27.             + " | " + (objPartition.PrimaryPartition? "主分区" : "扩展分区") );
  28.         WScript.Echo( ItemSeparator );
  29.         colLogicalDisks = objWbemServices.ExecQuery(
  30.             "ASSOCIATORS OF {" + objPartition.SystemProperties_("__RelPath") + "} "
  31.             + " WHERE AssocClass = Win32_LogicalDiskToPartition");
  32.         enumItems3 = new Enumerator(colLogicalDisks);
  33.         for (; !enumItems3.atEnd(); enumItems3.moveNext())
  34.         {
  35.             objLogicalDisk = enumItems3.item();
  36.             WScript.Echo( "逻辑分区: " + objLogicalDisk.DeviceID
  37.                 + " | " + objLogicalDisk.FileSystem );
  38.         }
  39.         if (0 == colLogicalDisks.Count) WScript.Echo( "未分配盘符" );
  40.         WScript.Echo( LineSeparator );
  41.     }
  42. }
  43. function strdup(s, n)
  44. {
  45.     return new Array(n+1).join(s);
  46. }
复制代码

作者: tiandyoin    时间: 2023-9-18 21:37

不正确,在 win11 上用vb js 得到结果都 不对,用 PS 得到结果是对的。想写个batch ,但不会




欢迎光临 批处理之家 (http://bathome.net./) Powered by Discuz! 7.2