[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[原创] 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
复制代码
1

评分人数

    • broly: 支持原创PB + 5 技术 + 1
我不知道

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. }
复制代码
1

评分人数

我不知道

TOP

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

TOP

返回列表