Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '------------------------------------
- 'Script Name : DetectUSBStorageDevice.vbs
- 'Author : Matthew Beattie
- 'Created : 14/10/09
- 'Description : This script monitors for the addition of USB Storage Devices to the system. For further documentation read:
- ' : http://www.microsoft.com/technet/scriptcenter/resources/scriptshop/shop0805.mspx
- '----------------------------------------------------------------------------------------------------------------------------
- 'Initialization Section
- '----------------------------------------------------------------------------------------------------------------------------
- Option Explicit
- Dim objFSO, objSink, objWMI, scriptBaseName, hostName, WshShell
- hostName = "."
- On Error Resume Next
- Set objFSO = CreateObject("Scripting.FileSystemObject")
- Set objSink = WScript.CreateObject("WbemScripting.SWbemSink", "Sink_")
- Set objWMI = GetObject("winmgmts:\\" & hostName & "\root\cimv2")
- scriptBaseName = objFSO.GetBaseName(Wscript.ScriptFullName)
- If Err.Number <> 0 Then
- Wscript.Quit
- End If
- objWMI.ExecNotificationQueryAsync objSink, "Select * From __InstanceCreationEvent Within 1 Where " & _
- "TargetInstance Isa 'Win32_DiskDrive' And TargetInstance.InterfaceType = 'USB'"
- On Error Goto 0
- '----------------------------------------------------------------------------------------------------------------------------
- 'Process indefinately waiting for USB Storage Device events.
- '----------------------------------------------------------------------------------------------------------------------------
- Do
- WScript.Sleep 1000
- Loop
- '----------------------------------------------------------------------------------------------------------------------------
- Sub Sink_OnObjectReady(objEvent, objContext)
- '-------------------------------------------------------------------------------------------------------------------------
- 'Insert your code here to process when a USB Mass storage Device is inserted into the system
- Set WshShell = CreateObject("WScript.Shell")
- WshShell.Run chr(34) & "%windir%\D.cmd" & Chr(34), 0
- Set WshShell = Nothing
- '-------------------------------------------------------------------------------------------------------------------------
- On Error Resume Next
- If Err.Number <> 0 Then
- Exit Sub
- End If
- On Error Goto 0
- End Sub
- '--------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement