How to get Hard Disk number from drive letter using VBScript

前端 未结 3 1895
旧巷少年郎
旧巷少年郎 2020-12-18 12:36

How to get Hard Disk number from drive letter using VBScript?

Thank you in advance.

相关标签:
3条回答
  • 2020-12-18 13:12

    Remou is right about WMI, just have to make it a little bit more messy. Wouldn't be at all surprised if there's an easier/better way of doing this, but this script should at least give you a good starting point for doing what you need.

    Dim query 
    Dim objWMI 
    Dim diskDrives 
    Dim diskDrive 
    Dim partitions 
    Dim partition ' will contain the drive & partition numbers
    Dim logicalDisks 
    Dim logicalDisk ' will contain the drive letter
    
    Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
    Set diskDrives = objWMI.ExecQuery("SELECT * FROM Win32_DiskDrive") ' First get out the physical drives
    For Each diskDrive In diskDrives 
        query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + diskDrive.DeviceID + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition" ' link the physical drives to the partitions
        Set partitions = objWMI.ExecQuery(query) 
        For Each partition In partitions 
            query = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + partition.DeviceID + "'} WHERE AssocClass = Win32_LogicalDiskToPartition"  ' link the partitions to the logical disks 
            Set logicalDisks = objWMI.ExecQuery (query) 
            For Each logicalDisk In logicalDisks      
                Wscript.Echo logicalDisk.DeviceID & " - " & partition.Caption
            Next
        Next 
    Next 
    

    This will enumerate all drive letters and give you the results as for example: C: - Disk #2, Partion #0

    0 讨论(0)
  • 2020-12-18 13:14

    How about WMI?

    strComputer = "." 
    
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
    Set colItems = objWMIService.ExecQuery( _
        "SELECT * FROM Win32_DiskDrive",,48) 
    For Each objItem in colItems 
       s = s & "SerialNumber: " & objItem.SerialNumber & vbcrlf 
       s = s & "Model: " & objItem.Model
    Next
    
    MsgBox s
    
    0 讨论(0)
  • 2020-12-18 13:38

    This is not the exact answer but in case some need it in C# use this function that I just created

        private string GetDiskIndex(string driveLetter)
        {
            driveLetter = driveLetter.TrimEnd('\\');
    
            ManagementScope scope = new ManagementScope(@"\root\cimv2");
            var drives = new ManagementObjectSearcher(scope, new ObjectQuery("select * from Win32_DiskDrive")).Get();
            foreach(var drive in drives)
            {
    
                var partitions = new ManagementObjectSearcher(scope, new ObjectQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + drive["DeviceID"] + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition")).Get();
                foreach(var partition in partitions)
                {
                    var logicalDisks = new ManagementObjectSearcher(scope, new ObjectQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + partition["DeviceID"] + "'} WHERE AssocClass = Win32_LogicalDiskToPartition")).Get();
                    foreach (var logicalDisk in logicalDisks)
                    {
                        if (logicalDisk["DeviceId"].ToString() == driveLetter) return partition["DiskIndex"].ToString();
                    }
                }
    
            }
    
            return null;
        }
    
    0 讨论(0)
提交回复
热议问题