标题: [原创] VBScript 脚本调用 WMI 获取硬盘与分区个数及其对应关系 [打印本页]
作者: IUnknown 时间: 2012-8-29 22:02 标题: 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
复制代码
作者: IUnknown 时间: 2012-8-29 22:54
JavaScript/JScript 脚本调用 WMI 获取硬盘与分区个数及其对应关系- @set @n=0//& cscript.exe -nologo -e:jscript "%~f0" &pause&exit/b
-
- // JavaScript/JScript 脚本调用 WMI 获取硬盘与分区个数及其对应关系
- // By IUnknown @ www.bathome.net 批处理之家
-
- // Ref: http://msdn.microsoft.com/en-us/library/windows/desktop/aa393259(v=vs.85).aspx
-
- LineSeparator = strdup("=", 79);
- ItemSeparator = strdup("-", 40);
-
- objWbemServices = GetObject("winmgmts:\\\\.\\root\\cimv2");
-
- colDiskDrives = objWbemServices.ExecQuery("SELECT * FROM Win32_DiskDrive");
- WScript.Echo( "硬盘个数: " + colDiskDrives.Count );
- WScript.Echo( );
- enumItems = new Enumerator(colDiskDrives);
- for (; !enumItems.atEnd(); enumItems.moveNext())
- {
- objDrive = enumItems.item();
- WScript.Echo( "硬盘名称: " + objDrive.Caption + " | " + objDrive.DeviceID );
- colPartitions = objWbemServices.ExecQuery(
- "ASSOCIATORS OF {" + objDrive.SystemProperties_("__RelPath") + "} "
- + " WHERE AssocClass = Win32_DiskDriveToDiskPartition");
- WScript.Echo( "物理区数: " + colPartitions.Count );
- WScript.Echo( "逻辑区数: " + objDrive.Partitions );
- WScript.Echo( LineSeparator );
-
- enumItems2 = new Enumerator(colPartitions);
- for (; !enumItems2.atEnd(); enumItems2.moveNext())
- {
- objPartition = enumItems2.item();
- WScript.Echo( "物理分区: " + objPartition.DeviceID
- + " | " + (objPartition.PrimaryPartition? "主分区" : "扩展分区") );
- WScript.Echo( ItemSeparator );
-
- colLogicalDisks = objWbemServices.ExecQuery(
- "ASSOCIATORS OF {" + objPartition.SystemProperties_("__RelPath") + "} "
- + " WHERE AssocClass = Win32_LogicalDiskToPartition");
- enumItems3 = new Enumerator(colLogicalDisks);
- for (; !enumItems3.atEnd(); enumItems3.moveNext())
- {
- objLogicalDisk = enumItems3.item();
- WScript.Echo( "逻辑分区: " + objLogicalDisk.DeviceID
- + " | " + objLogicalDisk.FileSystem );
- }
- if (0 == colLogicalDisks.Count) WScript.Echo( "未分配盘符" );
-
- WScript.Echo( LineSeparator );
- }
- }
-
- function strdup(s, n)
- {
- return new Array(n+1).join(s);
- }
复制代码
作者: tiandyoin 时间: 2023-9-18 21:37
不正确,在 win11 上用vb js 得到结果都 不对,用 PS 得到结果是对的。想写个batch ,但不会
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |