用VBScript查找USB驱动器号

我在http://network-blog.lan-secure.com/2008/03/usb-detection-using-wmi-script.html上find了这个脚本

strComputer = "." '(Any computer name or address) Set wmi = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set wmiEvent = wmi.ExecNotificationQuery("select * from __InstanceOperationEvent within 1 where TargetInstance ISA 'Win32_PnPEntity' and TargetInstance.Description='USB Mass Storage Device'") While True Set usb = wmiEvent.NextEvent() Select Case usb.Path_.Class Case "__InstanceCreationEvent" WScript.Echo("USB device found") Case "__InstanceDeletionEvent" WScript.Echo("USB device removed") Case "__InstanceModificationEvent" WScript.Echo("USB device modified") End Select Wend 

这个脚本是我所需要的。 它检测到插入一个USB驱动器。 如何修改它以查找USB驱动器的驱动器号? 如果我得到驱动器号,然后插入,而不是回显“findUSB设备”,我将能够运行Avast Antivirus的命令行扫描器来自动扫描插入驱动器。 请指导!

这是非常困难的事情。 从Win32_LogicalDrive类中获取最有用的驱动器信息。 不幸的是,可移动驱动器通常不会提供有关驱动器的大量信息。 有用的属性,如DeviceID和PNPDeviceID通常是空的。 下一个最好的事情是对可移动磁盘的实例迭代Win32_LogicalDisk类。 按照事件驱动的方式,这看起来像这样。

 strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set wmiEvent = objWMIService.ExecNotificationQuery( _ "Select * From __InstanceCreationEvent Within 1" & _ " Where TargetInstance ISA 'Win32_PnPEntity' and" & _ " TargetInstance.Description='USB Mass Storage Device'") While True Set objEvent = wmiEvent.NextEvent() Set objUSB = objEvent.TargetInstance strName = objUSB.Name strDeviceID = objUSB.DeviceID Set objUSB = Nothing Set colDrives = objWMIService.ExecQuery( _ "Select * From Win32_LogicalDisk Where DriveType = 2") For Each objDrive in colDrives strDriveLetter = objDrive.DeviceID Next Set colDrives = Nothing WScript.Echo strName & " was mounted as " & strDriveLetter Wend Set wmiEvent = Nothing Set objWMIService = Nothing 

当然,这只有在插入的驱动器是系统上唯一的可移动磁盘时才有效。 您可以通过在脚本启动时抓取所有驱动器号并在插入驱动器时进行比较来解决此限制,但是这种方法也不是防弹的。 更改任何其他驱动器的驱动器号分配将导致您的脚本返回无效信息。

colDrives所有连接驱动器的集合,不是最后一次连接。 应该:

 strDriveLetter = "" For Each objDrive in colDrives strDriveLetter = strDriveLetter & objDrive.DeviceID Next