|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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)? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
"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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
\Rems Thanks!
I don't know vbs but I can read the code and I think I can figure out how to point to a file to run from what you gave me here. Thanks for the this is exactly what I was looking for. On Nov 20, 11:01 am, \RemS <R...@discussions.microsoft.com> wrote: > "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 |
|
![]() |
| Outils de la discussion | |
|
|