通过 VBScript 检测磁盘类型

问题描述 投票:0回答:1

我有一个 VBScript 可以检测本地硬盘驱动器号并将它们存储在某个位置。现在我想从中删除 Windows 驱动器。我的意思是,首先它会查找所有本地硬盘驱动器,然后检测 Windows 驱动器和磁盘类型,例如 MBR 或 GPT。怎么办?

作为总结,我想在输出中添加磁盘类型,例如 GPT 或 MBR。

这是 VBScript:

  Dim goFS      : Set goFS      = CreateObject( "Scripting.FileSystemObject" )
  Dim dicDTypes : Set dicDTypes = buildDicMKV( _
    vbTextCompare, Split( "0 1 2 3 4 5" ), Split( "Unknown Removable Fixed Network CD-ROM RAM-Disk" ) _
  )
  Dim dicDrives : Set dicDrives = CreateObject( "Scripting.Dictionary" )
  Dim oWSH      : Set oWSH      = CreateObject( "WScript.Shell" )
  Dim sSysDir   : sSysDir       = oWSH.Environment( "PROCESS" )( "SYSTEMROOT" )
  WScript.Echo "sSysDir", sSysDir
  Dim sSysDrive : sSysDrive     = goFS.GetDriveName( sSysDir )
  WScript.Echo "sSysDrive", sSysDrive
  Dim sSDLetter : sSDLetter     = Left( sSysDrive, 1 )
  WScript.Echo "sSDLetter", sSDLetter
  Dim oDrive
  For Each oDrive In goFS.Drives
      WScript.Echo oDrive.DriveLetter, oDrive.DriveType, dicDTypes( CStr( oDrive.DriveType ) )
      If     "Fixed" = dicDTypes( CStr( oDrive.DriveType ) ) _
         And sSDLetter <> oDrive.DriveLetter Then
         Set dicDrives( oDrive.DriveLetter ) = oDrive
      End If   
  Next    
  WScript.Echo "------------------"
  Dim sDrive
  For Each sDrive In dicDrives.Keys
      Set oDrive = dicDrives( sDrive )
      WScript.Echo oDrive.DriveLetter, oDrive.DriveType, dicDTypes( CStr( oDrive.DriveType ) )
  Next    

Function buildDicMKV( vbCompMode, aKeys, aValues )
  Set buildDicMKV = CreateObject( "Scripting.Dictionary" )
'    compare
'      Optional. If provided, compare is a value representing the comparison mode. 
'      Acceptable values are 0 (Binary), 1 (Text), 2 (Database). Values greater than 
'      2 can be used to refer to comparisons using specific Locale IDs (LCID). 
  buildDicMKV.CompareMode = vbCompMode
  Dim nIdx
  For nIdx = 0 To UBound( aKeys )
      buildDicMKV.Add aKeys( nIdx ), aValues( nIdx )
  Next    
End Function  
vbscript
1个回答
0
投票

您可以使用WMI检索磁盘信息。请参阅下面代码中

oPart.Description
的输出。

Set oWMI = GetObject("winmgmts:\\.\root\cimv2")
Set oPartitions = oWMI.ExecQuery("Select * from Win32_DiskPartition")
For Each oPart in oPartitions
  Wscript.Echo "Block Size: " & oPart.BlockSize
  Wscript.Echo "Bootable: " & oPart.Bootable
  Wscript.Echo "Boot Partition: " & oPart.BootPartition
  Wscript.Echo "Description: " & oPart.Description
  Wscript.Echo "Device ID: " & oPart.DeviceID
  Wscript.Echo "Disk Index: " & oPart.DiskIndex
  Wscript.Echo "Index: " & oPart.Index
  Wscript.Echo "Name: " & oPart.Name
  Wscript.Echo "Number Of Blocks: " & oPart.NumberOfBlocks
  Wscript.Echo "Primary Partition: " & oPart.PrimaryPartition
  Wscript.Echo "Size: " & oPart.Size
  Wscript.Echo "Starting Offset: " & oPart.StartingOffset
  Wscript.Echo "Type: " & oPart.Type
Next

您还可以切换到 PowerShell 并使用

Get-Disk
cmdlet。

© www.soinside.com 2019 - 2024. All rights reserved.