WMI 获取驱动器号与物理驱动器路径的关联,错过 CDROM

发布于 2025-01-07 07:46:02 字数 1175 浏览 1 评论 0原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的黑色迷你裙 2025-01-14 07:46:02

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

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

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
帅气称霸 2025-01-14 07:46:02

我认为您需要使用 Win32_CDROMDrive WMI 类来访问 CD-ROM 信息。上面的代码正在 Win32_DiskDrive 类中查找物理驱动器,它不包括 CD_ROM

您可以使用其他行来获取类似的数据 - 但给定的 CD-ROM 不具有分区特征,但情况不同您当前的代码确实

ComputerName = "."
Set wmiServices = GetObject _
                  ("winmgmts:{impersonationLevel=Impersonate}!//" & ComputerName)
Set wmiDiskDrives = wmiServices.ExecQuery _
                    ("SELECT * FROM Win32_CDROMDrive")
For Each wmiDiskDrive In wmiDiskDrives
    MsgBox wmiDiskDrive.drive & "=" & wmiDiskDrive.DeviceID
Next

相反,我认为这个不同的 VBS 可能 可以做您想要的事情 - 可能部分,因为我认为分区信息与您无关。

版本

Dim objFSO
Dim colDrives
Dim strOut
Dim strArray

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = objFSO.Drives
strArray = Array("Unknown", "Removable", "Fixed", "Network", "CD-ROM", "RAM Disk")
On Error Resume Next
'File system errors for virtual drives
For Each objDrive In colDrives
    strOut = "Drive letter: " & objDrive.DriveLetter & vbNewLine
    strOut = strOut & ("Drive type: " & strArray(objDrive.DriveType) & vbNewLine)
    strOut = strOut & ("File system: " & objDrive.FileSystem & vbNewLine)
    strOut = strOut & ("Path: " & objDrive.Path)
    wscript.echo strOut
Next
On Error GoTo 0

版本

Sub Test()
Dim objFSO As Object
Dim colDrives As Object
Dim strOut As String
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = objFSO.Drives
On Error Resume Next
'File system errors for virtual drives
For Each objDrive In colDrives
    strOut = "Drive letter: " & objDrive.DriveLetter & vbNewLine
    strOut = strOut & ("Drive type: " & Choose(objDrive.DriveType + 1, "Unknown", "Removable", "Fixed", "Network", "CD-ROM", "RAM Disk") & vbNewLine)
    strOut = strOut & ("File system: " & objDrive.FileSystem & vbNewLine)
    strOut = strOut & ("Path: " & objDrive.Path)
    MsgBox strOut
Next
On Error GoTo 0
End Sub

I think you wouldn need to use the Win32_CDROMDrive WMI class to access CD-ROM info. The code you have above is looking for physical drives in the Win32_DiskDrive class, it excludes CD_ROM

You could additional lines to get similar data - but not the same given CD-ROMs don't have the Partition characteristics that your current code does

ComputerName = "."
Set wmiServices = GetObject _
                  ("winmgmts:{impersonationLevel=Impersonate}!//" & ComputerName)
Set wmiDiskDrives = wmiServices.ExecQuery _
                    ("SELECT * FROM Win32_CDROMDrive")
For Each wmiDiskDrive In wmiDiskDrives
    MsgBox wmiDiskDrive.drive & "=" & wmiDiskDrive.DeviceID
Next

Instead I think this different VBS may do what you want - the may part as I dont think the partition info is relevant to you.

version

Dim objFSO
Dim colDrives
Dim strOut
Dim strArray

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = objFSO.Drives
strArray = Array("Unknown", "Removable", "Fixed", "Network", "CD-ROM", "RAM Disk")
On Error Resume Next
'File system errors for virtual drives
For Each objDrive In colDrives
    strOut = "Drive letter: " & objDrive.DriveLetter & vbNewLine
    strOut = strOut & ("Drive type: " & strArray(objDrive.DriveType) & vbNewLine)
    strOut = strOut & ("File system: " & objDrive.FileSystem & vbNewLine)
    strOut = strOut & ("Path: " & objDrive.Path)
    wscript.echo strOut
Next
On Error GoTo 0

version

Sub Test()
Dim objFSO As Object
Dim colDrives As Object
Dim strOut As String
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = objFSO.Drives
On Error Resume Next
'File system errors for virtual drives
For Each objDrive In colDrives
    strOut = "Drive letter: " & objDrive.DriveLetter & vbNewLine
    strOut = strOut & ("Drive type: " & Choose(objDrive.DriveType + 1, "Unknown", "Removable", "Fixed", "Network", "CD-ROM", "RAM Disk") & vbNewLine)
    strOut = strOut & ("File system: " & objDrive.FileSystem & vbNewLine)
    strOut = strOut & ("Path: " & objDrive.Path)
    MsgBox strOut
Next
On Error GoTo 0
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文