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

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

[复制链接]
发表于 2024-1-25 23:06:36 | 显示全部楼层 |阅读模式
本帖最后由 newswan 于 2024-1-27 18:45 编辑

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

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

评分

参与人数 1PB +2 收起 理由
Batcher + 2 感谢给帖子标题标注[已解决]字样

查看全部评分

发表于 2024-1-26 00:12:28 | 显示全部楼层
得监控u盘插入拔出,需要一直运行
监控可以自己监控,也可以让系统监控
自己监控的话写一个每隔一段时间检测盘符之类的主程序,然后根据检测结果运行其他程序或代码就是了
让系统监控的话得用win api,比如注册并处理WM_DEVICECHANGE
发表于 2024-1-26 09:11: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命令自己修改一下

评分

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

查看全部评分

发表于 2024-1-26 12:32:11 | 显示全部楼层
本帖最后由 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. )
复制代码
发表于 2024-1-26 13:43:52 | 显示全部楼层
本帖最后由 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"
复制代码
 楼主| 发表于 2024-1-26 16:31:43 | 显示全部楼层
回复 3# czjt1234

谢谢!
 楼主| 发表于 2024-1-26 16:32:05 | 显示全部楼层
回复 5# WHY

谢谢!
发表于 2024-1-26 21:18:07 | 显示全部楼层
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. }
复制代码
 楼主| 发表于 2024-1-26 22:42:26 | 显示全部楼层
回复 8# WHY

长时间运行的话,应该第一种好些吧
发表于 2024-1-26 23:40:34 | 显示全部楼层
第1个用的是 -action 脚本块,订阅的事件不会放到队列中;
第2个将订阅的事件会放到队列中,用 Wait-event 处理事件。

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

个人见解,不一定正确,萝卜白菜,各有所爱吧。
 楼主| 发表于 2024-1-27 15:39:00 | 显示全部楼层
回复 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 ,还有什么办法吗?
 楼主| 发表于 2024-1-27 15:39: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 ,还有什么办法吗?
 楼主| 发表于 2024-1-27 16:43:11 | 显示全部楼层
回复 4# yakeyun

一开始也是你这种方法,用 net share 和 wmic volume
发表于 2024-1-27 16:50:43 | 显示全部楼层
回复 12# newswan


需要这么查询的目的是?
没理解什么环境需要这个需求
 楼主| 发表于 2024-1-27 16:56:13 | 显示全部楼层
本帖最后由 newswan 于 2024-1-27 16:57 编辑

回复 14# czjt1234

只需要订阅 __InstanceCreationEvent , __InstanceDeletionEvent
但 __InstanceOperationEvent 还包含了 __InstanceModificationEven ,这个事件很多,也被订阅了, 写的时候,也会引发操作。
从优化的目的,应该去掉这个
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-18 04:22 , Processed in 0.024342 second(s), 9 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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