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