用VBScript查找U盘盘符 [英] Finding USB drive letter with VBScript

查看:17
本文介绍了用VBScript查找U盘盘符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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

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

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!

推荐答案

这很难做到.最有用的驱动器信息是从 Win32_LogicalDrive 类中提取的.不幸的是,可移动驱动器通常不会在此类中填充有关该驱动器的大量信息.DeviceID 和 PNPDeviceID 等有用的属性通常为空.下一个最好的做法是为可移动磁盘的实例迭代 Win32_LogicalDisk 类.与您的事件驱动方法保持一致,这看起来像这样.

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.

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.

这篇关于用VBScript查找U盘盘符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆