找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
楼主: newswan

[系统相关] [已解决]实现 u盘插入拔出时,调用一个程序

[复制链接]
发表于 2024-1-27 17:44:42 | 显示全部楼层
回复 15# newswan


我测试了win7x64

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

在U盘上写入删除修改文件,均不会触发设定的程序
 楼主| 发表于 2024-1-27 17:58:01 | 显示全部楼层
回复 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
复制代码
 楼主| 发表于 2024-1-27 18:01:01 | 显示全部楼层
回复 16# czjt1234

你加上 case else 试试
发表于 2024-1-27 18:04:56 | 显示全部楼层
回复 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 判断一下,除非插入与拔出时执行的是一样的命令(比如都是设置共享,或者都是删除共享)。

评分

参与人数 1技术 +1 收起 理由
newswan + 1 乐于助人

查看全部评分

 楼主| 发表于 2024-1-27 18:09:03 | 显示全部楼层
回复 19# WHY

谢谢,但是,我是为了避免的不需要的事件
发表于 2024-1-27 18:10:54 | 显示全部楼层
回复 18# newswan


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

还有 ps 不是用了 Switch ($class) 么
你把这个去掉当然有问题了
 楼主| 发表于 2024-1-27 18:13:35 | 显示全部楼层
回复 21# czjt1234

这样通知订阅就 提前过滤掉 __InstanceModificationEvent ,不用到 case 的才过滤掉
 楼主| 发表于 2024-1-27 18:14:32 | 显示全部楼层
回复 19# WHY

让 ai 给合并,结果是个 250
发表于 2024-1-27 18:49:07 | 显示全部楼层
提前过滤掉 __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
复制代码
 楼主| 发表于 2024-1-30 10:40:11 | 显示全部楼层
回复 24# czjt1234

这算强迫症
 楼主| 发表于 2024-2-5 15:39:52 | 显示全部楼层
最终
  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.                        
  21.                         Switch ($action) {
  22.                                 "__InstanceCreationEvent" {
  23.                                         if ( Test-Path $sharePath ) {
  24.                                                 Write-Host "USB Drive $diskLetter $diskVolume Inserted."
  25.                                                 Write-Host $(net share $shareName=$sharePath /GRANT:everyone,CHANGE)
  26.                                         }
  27.                                 }
  28.                                 "__InstanceDeletionEvent" {
  29.                                         Write-Host "USB Drive $diskLetter $diskVolume Removed."
  30.                                         Write-Host $(net share $shareName /Delete)
  31.                                 }
  32.                         }
  33.                 }
  34.                 5 {
  35.                         $sharePath = $diskLetter
  36.                         $shareName = $($sharePath -replace ":","_") + $diskVolume
  37.                        
  38.                         Switch ($action) {
  39.                                 "__InstanceCreationEvent" {
  40.                                         Write-Host "Optical Drive $diskLetter $diskVolume Inserted."
  41.                                         Write-Host $(net share $shareName=$sharePath)
  42.                                 }
  43.                                 "__InstanceDeletionEvent" {
  44.                                         Write-Host "Optical Drive $diskLetter $diskVolume Removed."
  45.                                         Write-Host $(net share $shareName /Delete)
  46.                                 }
  47.                         }
  48.                 }
  49.         }
  50. }


  51. Unregister-Event mountEvent -ErrorAction SilentlyContinue
  52. Register-WmiEvent  -SourceIdentifier mountEvent  -Query $psQuery  -Action $psAction
复制代码

评分

参与人数 1技术 +1 收起 理由
czjt1234 + 1 感谢分享

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-18 05:44 , Processed in 0.021417 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表