windows 用VBScript查找U盘盘符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8277765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:32:12  来源:igfitidea点击:

Finding USB drive letter with VBScript

windowsvbscriptwmiusb-drive

提问by Dario Dias

I found this script on http://network-blog.lan-secure.com/2008/03/usb-detection-using-wmi-script.html

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

 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

This script is next to what I need. It detects the insertion of a usb drive. How to modify it to find the drive letter of the usb drive? If I get the drive letter, then on insertion instead of echoing "USB device found" I will be able to run command line scanner of Avast Antivirus to automatically scan the drive on Insertion. Please guide!

这个脚本是我需要的。它检测到 USB 驱动器的插入。怎么修改才能找到U盘的盘符?如果我得到驱动器号,然后在插入时而不是回显“找到 USB 设备”,我将能够运行 Avast Antivirus 的命令行扫描程序以在插入时自动扫描驱动器。请指导!

回答by Nilpo

This is extremely difficult to do. Most useful drive information is pulled from the Win32_LogicalDrive class. Unfortunately, removable drives often do not populate this class with much information about the drive. Useful properties such as DeviceID and PNPDeviceID are most often left empty. The next best thing to do is iterate the Win32_LogicalDisk class for instances that are removable disks. In keeping with your event-driven approach, that would look something like this.

这是极难做到的。最有用的驱动器信息是从 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

Of course, this will only work if the inserted drive is the only removable disk on the system. You could work around this limitation by grabbing all of the drive letters when your script starts and comparing them when a drive is inserted, however, that approach isn't bulletproof either. Changing the drive letter assignments of any other drives would cause your script to return invalid information.

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

回答by abbe

colDrivesis a collection of all connected drives, not last connected only. Should be:

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

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