获取USB存储驱动器字母VBSCript(Get USB Storage Drive Letter VBSCript)

编程入门 行业动态 更新时间:2024-10-27 11:27:34
获取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没有关于Drive Letter的属性。 我可以使用另一个支持Drive Letter的脚本,这里是:

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

但是在这个脚本中我不能使用Caption属性,因为它不受支持:(我需要结合这两个脚本来获取每个USB存储标题和信件。我怎么能这样做?可能吗?

Here is a which shows list of connected USB storage devices.

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

I need to get the drive letter, but it is impossible with the code above. There is no property about Drive Letter for colItems. I can use another script which supports Drive Letter, here it is:

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

But in this script I can not use Caption Property because it is not supported :( I need to combine these two scripts to get every USB Storage Caption and Letter. How Can I do that? Possible?

最满意答案

试试这段代码:

'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

Try This code :

'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

更多推荐

本文发布于:2023-04-29 08:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335986.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:驱动器   字母   USB   VBSCript   Letter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!