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

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

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

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

用于自动共享离线盘,
挂上的时候,自动共享 moive 目录
取下的时候,自动删除共享
1

评分人数

    • Batcher: 感谢给帖子标题标注[已解决]字样PB + 2

回复 3# czjt1234

谢谢!

TOP

回复 5# WHY

谢谢!

TOP

回复 8# WHY

长时间运行的话,应该第一种好些吧

TOP

回复 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 ,还有什么办法吗?

TOP

回复 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 ,还有什么办法吗?

TOP

回复 4# yakeyun

一开始也是你这种方法,用 net share 和 wmic volume

TOP

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

回复 14# czjt1234

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

TOP

回复 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
复制代码

TOP

回复 16# czjt1234

你加上 case else 试试

TOP

回复 19# WHY

谢谢,但是,我是为了避免的不需要的事件

TOP

回复 21# czjt1234

这样通知订阅就 提前过滤掉 __InstanceModificationEvent ,不用到 case 的才过滤掉

TOP

回复 19# WHY

让 ai 给合并,结果是个 250

TOP

回复 24# czjt1234

这算强迫症

TOP

最终
  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
复制代码
1

评分人数

TOP

返回列表