获取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 NextI 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 NextBut 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 NextTry 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更多推荐
发布评论