Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
734 views
in Technique[技术] by (71.8m points)

windows - Finding USB drive letter with VBScript

I found this script on 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 & "
ootcimv2")
 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 & "
ootcimv2")
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...