1

このスクリプトは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 ドライブの挿入を検出します。USBドライブのドライブ文字を見つけるためにそれを変更する方法は? ドライブ文字を取得すると、挿入時に「USB デバイスが見つかりました」と表示される代わりに、アバスト アンチウイルスのコマンド ライン スキャナーを実行して、挿入時にドライブを自動的にスキャンできます。ガイドしてください!

4

2 に答える 2

3

これを行うのは非常に困難です。最も有用なドライブ情報は、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

もちろん、これは、挿入されたドライブがシステム上の唯一のリムーバブル ディスクである場合にのみ機能します。スクリプトの開始時にすべてのドライブ文字を取得し、ドライブの挿入時にそれらを比較することで、この制限を回避できますが、その方法も防弾ではありません。他のドライブのドライブ文字の割り当てを変更すると、スクリプトが無効な情報を返す原因となります。

于 2011-11-26T18:47:20.237 に答える