Board logo

标题: [系统相关] [已解决]实现 u盘插入拔出时,调用一个程序 [打印本页]

作者: newswan    时间: 2024-1-25 23:06     标题: [已解决]实现 u盘插入拔出时,调用一个程序

本帖最后由 newswan 于 2024-1-27 18:45 编辑

有什么程序可以实现,当u盘 移动磁盘插入拔出时,调用一个程序, 批处理或者 powershell脚本

用于自动共享离线盘,
挂上的时候,自动共享 moive 目录
取下的时候,自动删除共享
作者: Five66    时间: 2024-1-26 00:12

得监控u盘插入拔出,需要一直运行
监控可以自己监控,也可以让系统监控
自己监控的话写一个每隔一段时间检测盘符之类的主程序,然后根据检测结果运行其他程序或代码就是了
让系统监控的话得用win api,比如注册并处理WM_DEVICECHANGE
作者: czjt1234    时间: 2024-1-26 09:11

vbs
  1. s = "SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE " & _
  2.     "TargetInstance ISA 'Win32_LogicalDisk' AND TargetInstance.DriveType = 2"
  3. Set oWshShell = CreateObject("WScript.Shell")
  4. Set oWMI = GetObject("Winmgmts:\\.\Root\Cimv2")
  5. Set oSWbemEventSource = oWMI.ExecNotificationQuery(s)
  6. Do
  7.     Set oSWbemObject = oSWbemEventSource.NextEvent()
  8.     Select Case oSWbemObject.Path_.Class
  9.         Case "__InstanceCreationEvent"
  10.             'MsgBox "U盘插入,盘符:" & oSWbemObject.TargetInstance.DeviceId
  11.             oWshShell.Run "net use ......", 0
  12.         Case "__InstanceDeletionEvent"
  13.             'MsgBox "U盘移除,盘符:" & oSWbemObject.TargetInstance.DeviceId
  14.             oWshShell.Run "net use ......", 0
  15.     End Select
  16. Loop
复制代码
net命令自己修改一下
作者: yakeyun    时间: 2024-1-26 12:32

本帖最后由 yakeyun 于 2024-1-26 12:33 编辑

回复 1# newswan
在U盘根目录创建一个文本文件,命名为“test.txt”:
  1. @echo off & setlocal enabledelayedexpansion
  2. :RUN
  3. set "file=test.txt"
  4. set "found=false"
  5. for /D %%d in (D: E: F: G: H: I: J: K: L: M: N: O: P: Q: R: S: T: U: V: W: X: Y: Z:) do (
  6.     if exist "%%d\%file%" (
  7.         set "found=true"
  8.         echo %%d\%file%
  9.         echo 找到标记文件,开始执行命令。
  10. rem  自动共享 moive 目录
  11.         goto RUN
  12.     )
  13. )
  14. if "%found%"=="false" (
  15.     echo 未找到标记文件,开始执行命令。
  16. rem 删除共享
  17.     goto RUN
  18. )
复制代码

作者: WHY    时间: 2024-1-26 13:43

本帖最后由 WHY 于 2024-1-26 21:15 编辑
  1. Unregister-Event USBCheck -ea SilentlyContinue;
  2. $query = "select * from __InstanceOperationEvent within 5 where TargetInstance isa 'Win32_LogicalDisk' and TargetInstance.DriveType=2";
  3. Register-WmiEvent -Query $query -SourceIdentifier USBCheck -Action {
  4.     $class = $eventArgs.NewEvent.__CLASS;
  5.     $name  = $eventArgs.NewEvent.TargetInstance.Name;
  6.     Switch ($class) {
  7.         __InstanceCreationEvent {
  8.             Write-Host "USB Drive $name--Inserted."
  9.             #### net share MyShareName="C:\Users"
  10.         }
  11.         __InstanceDeletionEvent {
  12.             Write-Host "USB Drive $name--Removed."
  13.             #### net share MyShareName /Delete
  14.         }
  15.     }
  16. }
复制代码
保存为 D:\Test.ps1,CMD 或 PowerShell 命令行窗口执行:
  1. PowerShell -NoExit -exec Bypass -file "D:\Test.ps1"
复制代码

作者: newswan    时间: 2024-1-26 16:31

回复 3# czjt1234

谢谢!
作者: newswan    时间: 2024-1-26 16:32

回复 5# WHY

谢谢!
作者: WHY    时间: 2024-1-26 21:18

Test.ps1,右键 "使用 PowerShell 运行"
  1. Unregister-Event USBCheck -ea SilentlyContinue;
  2. $query = "select * from __InstanceOperationEvent within 5 where TargetInstance isa 'Win32_LogicalDisk' and TargetInstance.DriveType=2";
  3. Register-WmiEvent -Query $query -SourceIdentifier USBCheck;
  4. while ($true) {
  5.     $e = Wait-Event -SourceIdentifier USBCheck;
  6.     $class = $e.SourceArgs.NewEvent.__CLASS;
  7.     $name  = $e.SourceArgs.NewEvent.TargetInstance.Name;
  8.     Switch ($class) {
  9.         __InstanceCreationEvent {
  10.             Write-Host "USB Drive $name --Inserted.";
  11.             #### net share MyShareName="C:\Users"
  12.         }
  13.         __InstanceDeletionEvent {
  14.             Write-Host "USB Drive $name --Removed.";
  15.             #### net share MyShareName /Delete
  16.         }
  17.     }
  18.     Remove-Event -SourceIdentifier USBCheck;
  19. }
复制代码

作者: newswan    时间: 2024-1-26 22:42

回复 8# WHY

长时间运行的话,应该第一种好些吧
作者: WHY    时间: 2024-1-26 23:40

第1个用的是 -action 脚本块,订阅的事件不会放到队列中;
第2个将订阅的事件会放到队列中,用 Wait-event 处理事件。

一定要分出个孰优孰劣,我认为第2个更方便,相比第1个在命令行输入 PowerShell 命令运行,用右键要方便一点点;
命令行窗口运行还会多启动一个CMD或者POWERSHELL进程。

个人见解,不一定正确,萝卜白菜,各有所爱吧。
作者: newswan    时间: 2024-1-27 15:39

回复 10# WHY

新问题,学习了下相关知识,
__InstanceOperationEvent 包含3种事件:
__InstanceCreationEvent
__InstanceDeletionEvent
__InstanceModificationEvent

能不能在一个查询中查询 Creation Deletion ,避免 Modification
  1. "select * from __InstanceCreationEvent within 5 where TargetInstance isa 'Win32_LogicalDisk' and TargetInstance.DriveType=2"
  2. "select * from __InstanceDeletionEvent within 5 where TargetInstance isa 'Win32_LogicalDisk' and TargetInstance.DriveType=2"
复制代码
看了wql ,没找到 union ,还有什么办法吗?
作者: newswan    时间: 2024-1-27 15:39

回复 3# czjt1234

新问题,学习了下相关知识,
__InstanceOperationEvent 包含3种事件:
__InstanceCreationEvent
__InstanceDeletionEvent
__InstanceModificationEvent

能不能在一个查询中查询 Creation Deletion ,避免 Modification
  1. "select * from __InstanceCreationEvent within 5 where TargetInstance isa 'Win32_LogicalDisk' and TargetInstance.DriveType=2"
  2. "select * from __InstanceDeletionEvent within 5 where TargetInstance isa 'Win32_LogicalDisk' and TargetInstance.DriveType=2"
复制代码
看了wql ,没找到 union ,还有什么办法吗?
作者: newswan    时间: 2024-1-27 16:43

回复 4# yakeyun

一开始也是你这种方法,用 net share 和 wmic volume
作者: czjt1234    时间: 2024-1-27 16:50

回复 12# newswan


需要这么查询的目的是?
没理解什么环境需要这个需求
作者: newswan    时间: 2024-1-27 16:56

本帖最后由 newswan 于 2024-1-27 16:57 编辑

回复 14# czjt1234

只需要订阅 __InstanceCreationEvent , __InstanceDeletionEvent
但 __InstanceOperationEvent 还包含了 __InstanceModificationEven ,这个事件很多,也被订阅了, 写的时候,也会引发操作。
从优化的目的,应该去掉这个
作者: czjt1234    时间: 2024-1-27 17:44

回复 15# newswan


我测试了win7x64

拔插U盘会触发设定的程序

在U盘上写入删除修改文件,均不会触发设定的程序
作者: newswan    时间: 2024-1-27 17:58

回复 16# czjt1234

会不停的有消息__InstanceModificationEvent
  1. $psAction = {
  2. $action = $eventArgs.NewEvent.__CLASS
  3. Write-Host ""
  4. Write-Host (Get-Date)
  5. Write-Host $action
  6. }
  7. $psQuery = " select * from __InstanceOperationEvent within 1 where TargetInstance isa 'Win32_LogicalDisk' "
  8. Register-WmiEvent  -SourceIdentifier mountEvent  -Query $psQuery  -Action $psAction
复制代码

作者: newswan    时间: 2024-1-27 18:01

回复 16# czjt1234

你加上 case else 试试
作者: WHY    时间: 2024-1-27 18:04

回复 11# newswan
  1. $query = "select * from __InstanceOperationEvent within 5 where (__CLASS='__InstanceCreationEvent' or __CLASS='__InstanceDeletionEvent') and  TargetInstance isa 'Win32_LogicalDisk' and TargetInstance.DriveType=2";
复制代码
这样写,除了查询语句变长、变复杂,没啥太大意义。
即使这样写,还是需要 Swith 或 If 判断一下,除非插入与拔出时执行的是一样的命令(比如都是设置共享,或者都是删除共享)。
作者: newswan    时间: 2024-1-27 18:09

回复 19# WHY

谢谢,但是,我是为了避免的不需要的事件
作者: czjt1234    时间: 2024-1-27 18:10

回复 18# newswan


为什么要加 case else
原代码已经满足了你的需求,不是不要加这个么
加了这个反而不好

还有 ps 不是用了 Switch ($class) 么
你把这个去掉当然有问题了
作者: newswan    时间: 2024-1-27 18:13

回复 21# czjt1234

这样通知订阅就 提前过滤掉 __InstanceModificationEvent ,不用到 case 的才过滤掉
作者: newswan    时间: 2024-1-27 18:14

回复 19# WHY

让 ai 给合并,结果是个 250
作者: czjt1234    时间: 2024-1-27 18:49

提前过滤掉 __InstanceModificationEvent
代码运行查看cpu占用基本为0,不用在意这个吧
再说19楼说了,即使过滤掉,还是要对结果进行 case 或 Switch

非要过滤掉,vbs可以变通一下,当然代码非但没简洁反而更复杂了
  1. s1 = "SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_LogicalDisk' AND TargetInstance.DriveType = 2"
  2. s2 = "SELECT * FROM __InstanceDeletionEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_LogicalDisk' AND TargetInstance.DriveType = 2"
  3. Set oWshShell = CreateObject("WScript.Shell")
  4. Set oWMI = GetObject("Winmgmts:\\.\Root\Cimv2")
  5. Set oSWbemSink = WScript.CreateObject("WbemScripting.SWbemSink", "Sink_")
  6. Set o1 = CreateObject("WbemScripting.SWbemNamedValueSet")
  7. o1.Add "类型", "插入"
  8. Set o2 = CreateObject("WbemScripting.SWbemNamedValueSet")
  9. o2.Add "类型", "拔出"
  10. oWMI.ExecNotificationQueryAsync oSWbemSink, s1,,,, o1
  11. oWMI.ExecNotificationQueryAsync oSWbemSink, s2,,,, o2
  12. Do
  13.     WScript.Sleep 100
  14. Loop
  15. Sub Sink_OnObjectReady(objWbemObject, objWbemAsyncContext)
  16.     If objWbemAsyncContext.Item("类型") = "插入" Then msgbox "插入"
  17.     If objWbemAsyncContext.Item("类型") = "拔出" Then msgbox "拔出"
  18. End Sub
复制代码

作者: newswan    时间: 2024-1-30 10:40

回复 24# czjt1234

这算强迫症
作者: newswan    时间: 2024-2-5 15:39

最终
  1. if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
  2.     Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass  -NoExit -File `"$PSCommandPath`"" -Verb RunAs
  3.     exit
  4. }
  5. $psQuery = @"
  6. select *  from __InstanceOperationEvent  within 1
  7. where TargetInstance  isa 'Win32_LogicalDisk' and (__CLASS='__InstanceCreationEvent' or __CLASS='__InstanceDeletionEvent')
  8. "@
  9. $psAction = {
  10. $action = $eventArgs.NewEvent.__CLASS
  11. $diskDriveType  = $event.SourceArgs.NewEvent.TargetInstance.DriveType
  12. $diskLetter  = $event.SourceArgs.NewEvent.TargetInstance.Name
  13. $diskVolume  = $event.SourceArgs.NewEvent.TargetInstance.VolumeName
  14. Write-Host "`n"
  15. Write-Host "- $( Get-Date -format "yyyy-MM-dd HH:mm:ss" ) -"
  16. switch ( $diskDriveType ) {
  17. 2 {
  18. $sharePath = $diskLetter + "movie"
  19. $shareName = $diskVolume -replace "\D+","movie"
  20. Switch ($action) {
  21. "__InstanceCreationEvent" {
  22. if ( Test-Path $sharePath ) {
  23. Write-Host "USB Drive $diskLetter $diskVolume Inserted."
  24. Write-Host $(net share $shareName=$sharePath /GRANT:everyone,CHANGE)
  25. }
  26. }
  27. "__InstanceDeletionEvent" {
  28. Write-Host "USB Drive $diskLetter $diskVolume Removed."
  29. Write-Host $(net share $shareName /Delete)
  30. }
  31. }
  32. }
  33. 5 {
  34. $sharePath = $diskLetter
  35. $shareName = $($sharePath -replace ":","_") + $diskVolume
  36. Switch ($action) {
  37. "__InstanceCreationEvent" {
  38. Write-Host "Optical Drive $diskLetter $diskVolume Inserted."
  39. Write-Host $(net share $shareName=$sharePath)
  40. }
  41. "__InstanceDeletionEvent" {
  42. Write-Host "Optical Drive $diskLetter $diskVolume Removed."
  43. Write-Host $(net share $shareName /Delete)
  44. }
  45. }
  46. }
  47. }
  48. }
  49. Unregister-Event mountEvent -ErrorAction SilentlyContinue
  50. Register-WmiEvent  -SourceIdentifier mountEvent  -Query $psQuery  -Action $psAction
复制代码





欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2