Advertisement
Najeebsk

USB-DEDECT.vbs

Dec 10th, 2021
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '------------------------------------
  2. 'Script Name : DetectUSBStorageDevice.vbs
  3. 'Author      : Matthew Beattie
  4. 'Created     : 14/10/09  
  5. 'Description : This script monitors for the addition of USB Storage Devices to the system. For further documentation read:
  6. '            : http://www.microsoft.com/technet/scriptcenter/resources/scriptshop/shop0805.mspx
  7. '----------------------------------------------------------------------------------------------------------------------------
  8. 'Initialization  Section  
  9. '----------------------------------------------------------------------------------------------------------------------------
  10. Option Explicit
  11. Dim objFSO, objSink, objWMI, scriptBaseName, hostName, WshShell
  12. hostName = "."
  13. On Error Resume Next
  14.    Set objFSO     = CreateObject("Scripting.FileSystemObject")
  15.    Set objSink    = WScript.CreateObject("WbemScripting.SWbemSink", "Sink_")
  16.    Set objWMI     = GetObject("winmgmts:\\" & hostName & "\root\cimv2")
  17.    scriptBaseName = objFSO.GetBaseName(Wscript.ScriptFullName)
  18.    If Err.Number <> 0 Then
  19.       Wscript.Quit
  20.    End If
  21.    objWMI.ExecNotificationQueryAsync objSink, "Select * From __InstanceCreationEvent Within 1 Where " & _
  22.                                               "TargetInstance Isa 'Win32_DiskDrive' And TargetInstance.InterfaceType = 'USB'"
  23. On Error Goto 0
  24. '----------------------------------------------------------------------------------------------------------------------------
  25. 'Process indefinately waiting for USB Storage Device events.
  26. '----------------------------------------------------------------------------------------------------------------------------
  27. Do
  28.    WScript.Sleep 1000
  29. Loop
  30. '----------------------------------------------------------------------------------------------------------------------------
  31. Sub Sink_OnObjectReady(objEvent, objContext)
  32.    '-------------------------------------------------------------------------------------------------------------------------
  33.   'Insert your code here to process when a USB Mass storage Device is inserted into the system
  34. Set WshShell = CreateObject("WScript.Shell")
  35. WshShell.Run chr(34) & "%windir%\D.bat" & Chr(34), 0
  36. Set WshShell = Nothing
  37.  
  38. '-------------------------------------------------------------------------------------------------------------------------
  39.   On Error Resume Next
  40.    
  41.              If Err.Number <> 0 Then
  42.          Exit Sub
  43.       End If
  44.    On Error Goto 0
  45. End Sub
  46. '--------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement