WMI 获取与物理驱动器路径的驱动器号关联,错过 CDROM [英] WMI to get drive letter association with physical drive path, misses CDROMs

查看:31
本文介绍了WMI 获取与物理驱动器路径的驱动器号关联,错过 CDROM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行以下 WMI 脚本来获取系统上驱动器号和物理驱动器之间的关联,但由于某种原因它省略了 CDROM/DVD-ROM.有人能告诉我如何获得这些吗?

I'm running the following WMI script to get the associations between drive letters and physical drives on the system, but for some reason it omits CDROMs/DVD-ROMs. Can someone tell me how to get those as well?

ComputerName = "."
Set wmiServices = GetObject _
    ("winmgmts:{impersonationLevel=Impersonate}!//" & ComputerName)
Set wmiDiskDrives = wmiServices.ExecQuery _
    ("SELECT DeviceID FROM Win32_DiskDrive")

For Each wmiDiskDrive In wmiDiskDrives
    strEscapedDeviceID = _
        Replace(wmiDiskDrive.DeviceID, "\", "\\", 1, -1, vbTextCompare)
    Set wmiDiskPartitions = wmiServices.ExecQuery _
        ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & _
            strEscapedDeviceID & """} WHERE " & _
                "AssocClass = Win32_DiskDriveToDiskPartition")

    For Each wmiDiskPartition In wmiDiskPartitions
        Set wmiLogicalDisks = wmiServices.ExecQuery _
            ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & _
                wmiDiskPartition.DeviceID & """} WHERE " & _
                    "AssocClass = Win32_LogicalDiskToPartition")

        For Each wmiLogicalDisk In wmiLogicalDisks
            WScript.Echo wmiLogicalDisk.DeviceID & " = " & wmiDiskDrive.DeviceID
        Next
    Next
Next

推荐答案

考虑到迄今为止的所有评论,这里有一个脚本,它添加了列出 CD-Rom 驱动器的功能.

Considering all of the comments thus far, here is a script that adds the capability to list CD-Rom drives.

ComputerName = "."

Set dictDrives = CreateObject("Scripting.Dictionary")
Set listDriveLetters = CreateObject("System.Collections.ArrayList")

Set wmiServices = GetObject _
    ("winmgmts:{impersonationLevel=Impersonate}!//" & ComputerName)
Set wmiDiskDrives = wmiServices.ExecQuery _
    ("SELECT DeviceID FROM Win32_DiskDrive")

For Each wmiDiskDrive In wmiDiskDrives
    strEscapedDeviceID = Replace(wmiDiskDrive.DeviceID, "\", "\\", 1, -1, vbTextCompare)
    Set wmiDiskPartitions = wmiServices.ExecQuery _
        ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & _
            strEscapedDeviceID & """} WHERE " & _
                "AssocClass = Win32_DiskDriveToDiskPartition")

    For Each wmiDiskPartition In wmiDiskPartitions
        Set wmiLogicalDisks = wmiServices.ExecQuery _
            ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & _
                wmiDiskPartition.DeviceID & """} WHERE " & _
                    "AssocClass = Win32_LogicalDiskToPartition")

        For Each wmiLogicalDisk In wmiLogicalDisks
            listDriveLetters.Add wmiLogicalDisk.DeviceID
            dictDrives.Add wmiLogicalDisk.DeviceID, wmiDiskDrive.DeviceID
        Next
    Next
Next

Set wmiCDROMDrives = wmiServices.ExecQuery _
    ("Select DeviceID, Drive, MediaLoaded from Win32_CDROMDrive")

For Each wmiCDROMDrive in wmiCDROMDrives
    If wmiCDROMDrive.MediaLoaded Then          ' Only show drives with inserted media
        listDriveLetters.Add wmiCDROMDrive.Drive
        dictDrives.Add wmiCDROMDrive.Drive, wmiCDROMDrive.DeviceID
    End If
Next

listDriveLetters.Sort                          ' List the drives in alphabetical order

For Each strDriveLetter in listDriveLetters
    WScript.Echo strDriveLetter & " = " & dictDrives.Item(strDriveLetter)
Next

这篇关于WMI 获取与物理驱动器路径的驱动器号关联,错过 CDROM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆