Home All Groups Group Topic Archive Search About


Author
15 Nov 2007 11:01 PM
pand0ra.usa
Ok, here is the problem. I am looking to have a script file execute
when a usb device is attached to a computer. My problem is how to do
this without doing a scheduled task. Is there a registry key I can use
where I can point to my .bat file (myscript.bat) or is there a way to
script this to monitor the usb (I'm guessing this is more difficult)?

Author
20 Nov 2007 6:01 PM
RemS
"pand0ra.usa" wrote:

> Ok, here is the problem. I am looking to have a script file execute
> when a usb device is attached to a computer. My problem is how to do
> this without doing a scheduled task. Is there a registry key I can use
> where I can point to my .bat file (myscript.bat) or is there a way to
> script this to monitor the usb (I'm guessing this is more difficult)?
>

Here is a simple script in vbs (save as a *.vbs file) that monitors for new
attachted usb-devices.
For testing this script has a 'timeout' and will close the monitor after 2
minutes.
You can later on disable the 'timeout' and run this script as a logonscript.
Now a popup is showed when a device is attached, you can make that a program
to run..
The filter is "USB\" so it reacts for every usb device, but you can change
that.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
intTimeOut = 3   ' in minutes

strComputer = "."
Set objSWbemServices = _
GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set objSWbemEventSource = objSWbemServices.ExecNotificationQuery _ 
   ("SELECT * FROM __InstanceCreationEvent WITHIN 3" & _ 
   "WHERE TargetInstance ISA 'Win32_USBControllerdevice'")

On Error Resume Next
intTimeOut = 6e4*(intTimeOut)
Do until err
wscript.sleep 1000
Set objSWbemObject = objSWbemEventSource.NextEvent(intTimeOut)
If Err.number = 0 Then
    strDeviceName = objSWbemObject.TargetInstance.Dependent
    strQuotes = Chr(34)
    strDeviceName = Replace(strDeviceName, strQuotes, "")
    arrDeviceNames = Split(strDeviceName, "=")
    strDeviceName = Trim(Replace(arrDeviceNames(1),"\\", "\"))
    If InStr(strDeviceName, "USB\") Then
     With CreateObject("Wscript.Shell")
         intReturn = .Popup(objSWbemObject.TargetInstance.Antecedent,3)
     End With
   End If
End If
Loop
WScript.Echo Err.Descriptio
Err.Clear

Wscript.quit ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

note:
Users are able to stopt this monitor, by stopping the 'wscript.exe' process
in the TaskManager.

\Rems

AddThis Social Bookmark Button