获取 USB 存储盘符 VBScript
Get USB Storage Drive Letter VBSCript
这是一个显示连接的 USB 存储设备列表的列表。
On Error Resume Next
strComputer = "."
Dim oFSO, oDrive
Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive WHERE InterfaceType = 'USB'")
For Each objItem in colItems
Wscript.Echo objItem.Caption
Next
我需要获取盘符,但是上面的代码是不可能的。 colItems 的驱动器号没有 属性。我可以使用另一个支持驱动器号的脚本,这里是:
Dim oFSO, oDrive
Const USBDRIVE=1
Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")
For Each oDrive In oFSO.Drives
If oDrive.DriveType = USBDRIVE And oDrive.DriveLetter <> "A" Then
WScript.Echo oDrive.DriveLetter
End If
Next
但是在这个脚本中我不能使用标题 属性 因为它不受支持:( 我需要结合这两个脚本来获取每个 USB 存储标题和字母。我该怎么做?可能吗?
试试这个代码:
'Show drive letters associated with each
ComputerName = "."
Set wmiServices = GetObject ( _
"winmgmts:{impersonationLevel=Impersonate}!//" _
& ComputerName)
' Get physical disk drive
Set wmiDiskDrives = wmiServices.ExecQuery ( "SELECT Caption, DeviceID FROM Win32_DiskDrive WHERE InterfaceType = 'USB'")
For Each wmiDiskDrive In wmiDiskDrives
' x = wmiDiskDrive.Caption & Vbtab & " " & wmiDiskDrive.DeviceID
'Use the disk drive device id to
' find associated partition
query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" & wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
Set wmiDiskPartitions = wmiServices.ExecQuery(query)
For Each wmiDiskPartition In wmiDiskPartitions
'Use partition device id to find logical disk
Set wmiLogicalDisks = wmiServices.ExecQuery ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _
& wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition")
x = ""
For Each wmiLogicalDisk In wmiLogicalDisks
x = x & wmiDiskDrive.Caption & " " & wmiDiskPartition.DeviceID & " = " & wmiLogicalDisk.DeviceID
Wscript.echo x
Next
Next
Next
从下一个脚本中导出您想要的输出(根据需要进行注释并带有不言自明的输出)。写入与 wscript
或 cscript
主机一起启动)
' VB Script Document 28556806.vbs
option explicit
On Error Goto 0
Dim ComputerName, strRslt, strQuery
Dim wmiServices _
, wmiDiskDrives, wmiDiskDrive _
, wmiDiskPartitions, wmiDiskPartition _
, wmiLogicalDisks, wmiLogicalDisk
strRslt = Wscript.ScriptName _
& vbTab & "Drive letters associated with disk drives" _
ComputerName = "."
Set wmiServices = GetObject ( _
"winmgmts:{impersonationLevel=Impersonate}!//" _
& ComputerName)
' Get physical disk drive
Set wmiDiskDrives = wmiServices.ExecQuery ( _
"SELECT Caption, DeviceID, InterfaceType FROM Win32_DiskDrive")
For Each wmiDiskDrive In wmiDiskDrives
strRslt = strRslt & vbNewLine
strRslt = strRslt & vbNewLine _
& "DiskDrive.Caption = " & wmiDiskDrive.Caption _
& vbNewLine & "DiskDrive.InterfaceType = " _
& wmiDiskDrive.InterfaceType
'Use the disk drive device id to find associated partition
strQuery = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" _
& wmiDiskDrive.DeviceID _
& "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
Set wmiDiskPartitions = wmiServices.ExecQuery(strQuery)
For Each wmiDiskPartition In wmiDiskPartitions
'Use partition device id to find logical disk
Set wmiLogicalDisks = wmiServices.ExecQuery _
("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _
& wmiDiskPartition.DeviceID _
& "'} WHERE AssocClass = Win32_LogicalDiskToPartition")
For Each wmiLogicalDisk In wmiLogicalDisks
strRslt = strRslt _
& vbNewLine & "DiskDrive.Caption = " _
& wmiDiskDrive.Caption _
& vbNewLine & "DiskDrive.DeviceID = " _
& wmiDiskDrive.DeviceID _
& vbNewLine & "DiskPartition.Partition = " _
& wmiDiskPartition.DeviceID _
& vbNewLine & "LogicalDisk.DeviceID = " _
& wmiLogicalDisk.DeviceID
Next
Next
Next
WScript.Echo strRslt
我已经用更广泛的方法构思了我的答案,以便更容易地决定连接更多的 USB 驱动器;可以在 Computer System Hardware Classes
找到合适的属性
这是一个显示连接的 USB 存储设备列表的列表。
On Error Resume Next
strComputer = "."
Dim oFSO, oDrive
Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive WHERE InterfaceType = 'USB'")
For Each objItem in colItems
Wscript.Echo objItem.Caption
Next
我需要获取盘符,但是上面的代码是不可能的。 colItems 的驱动器号没有 属性。我可以使用另一个支持驱动器号的脚本,这里是:
Dim oFSO, oDrive
Const USBDRIVE=1
Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")
For Each oDrive In oFSO.Drives
If oDrive.DriveType = USBDRIVE And oDrive.DriveLetter <> "A" Then
WScript.Echo oDrive.DriveLetter
End If
Next
但是在这个脚本中我不能使用标题 属性 因为它不受支持:( 我需要结合这两个脚本来获取每个 USB 存储标题和字母。我该怎么做?可能吗?
试试这个代码:
'Show drive letters associated with each
ComputerName = "."
Set wmiServices = GetObject ( _
"winmgmts:{impersonationLevel=Impersonate}!//" _
& ComputerName)
' Get physical disk drive
Set wmiDiskDrives = wmiServices.ExecQuery ( "SELECT Caption, DeviceID FROM Win32_DiskDrive WHERE InterfaceType = 'USB'")
For Each wmiDiskDrive In wmiDiskDrives
' x = wmiDiskDrive.Caption & Vbtab & " " & wmiDiskDrive.DeviceID
'Use the disk drive device id to
' find associated partition
query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" & wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
Set wmiDiskPartitions = wmiServices.ExecQuery(query)
For Each wmiDiskPartition In wmiDiskPartitions
'Use partition device id to find logical disk
Set wmiLogicalDisks = wmiServices.ExecQuery ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _
& wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition")
x = ""
For Each wmiLogicalDisk In wmiLogicalDisks
x = x & wmiDiskDrive.Caption & " " & wmiDiskPartition.DeviceID & " = " & wmiLogicalDisk.DeviceID
Wscript.echo x
Next
Next
Next
从下一个脚本中导出您想要的输出(根据需要进行注释并带有不言自明的输出)。写入与 wscript
或 cscript
主机一起启动)
' VB Script Document 28556806.vbs
option explicit
On Error Goto 0
Dim ComputerName, strRslt, strQuery
Dim wmiServices _
, wmiDiskDrives, wmiDiskDrive _
, wmiDiskPartitions, wmiDiskPartition _
, wmiLogicalDisks, wmiLogicalDisk
strRslt = Wscript.ScriptName _
& vbTab & "Drive letters associated with disk drives" _
ComputerName = "."
Set wmiServices = GetObject ( _
"winmgmts:{impersonationLevel=Impersonate}!//" _
& ComputerName)
' Get physical disk drive
Set wmiDiskDrives = wmiServices.ExecQuery ( _
"SELECT Caption, DeviceID, InterfaceType FROM Win32_DiskDrive")
For Each wmiDiskDrive In wmiDiskDrives
strRslt = strRslt & vbNewLine
strRslt = strRslt & vbNewLine _
& "DiskDrive.Caption = " & wmiDiskDrive.Caption _
& vbNewLine & "DiskDrive.InterfaceType = " _
& wmiDiskDrive.InterfaceType
'Use the disk drive device id to find associated partition
strQuery = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" _
& wmiDiskDrive.DeviceID _
& "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
Set wmiDiskPartitions = wmiServices.ExecQuery(strQuery)
For Each wmiDiskPartition In wmiDiskPartitions
'Use partition device id to find logical disk
Set wmiLogicalDisks = wmiServices.ExecQuery _
("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _
& wmiDiskPartition.DeviceID _
& "'} WHERE AssocClass = Win32_LogicalDiskToPartition")
For Each wmiLogicalDisk In wmiLogicalDisks
strRslt = strRslt _
& vbNewLine & "DiskDrive.Caption = " _
& wmiDiskDrive.Caption _
& vbNewLine & "DiskDrive.DeviceID = " _
& wmiDiskDrive.DeviceID _
& vbNewLine & "DiskPartition.Partition = " _
& wmiDiskPartition.DeviceID _
& vbNewLine & "LogicalDisk.DeviceID = " _
& wmiLogicalDisk.DeviceID
Next
Next
Next
WScript.Echo strRslt
我已经用更广泛的方法构思了我的答案,以便更容易地决定连接更多的 USB 驱动器;可以在 Computer System Hardware Classes
找到合适的属性