最终- if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
- Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -NoExit -File `"$PSCommandPath`"" -Verb RunAs
- exit
- }
-
-
- $psQuery = @"
- select * from __InstanceOperationEvent within 1
- where TargetInstance isa 'Win32_LogicalDisk' and (__CLASS='__InstanceCreationEvent' or __CLASS='__InstanceDeletionEvent')
- "@
-
- $psAction = {
- $action = $eventArgs.NewEvent.__CLASS
- $diskDriveType = $event.SourceArgs.NewEvent.TargetInstance.DriveType
- $diskLetter = $event.SourceArgs.NewEvent.TargetInstance.Name
- $diskVolume = $event.SourceArgs.NewEvent.TargetInstance.VolumeName
-
- Write-Host "`n"
- Write-Host "- $( Get-Date -format "yyyy-MM-dd HH:mm:ss" ) -"
-
- switch ( $diskDriveType ) {
- 2 {
- $sharePath = $diskLetter + "movie"
- $shareName = $diskVolume -replace "\D+","movie"
-
- Switch ($action) {
- "__InstanceCreationEvent" {
- if ( Test-Path $sharePath ) {
- Write-Host "USB Drive $diskLetter $diskVolume Inserted."
- Write-Host $(net share $shareName=$sharePath /GRANT:everyone,CHANGE)
- }
- }
- "__InstanceDeletionEvent" {
- Write-Host "USB Drive $diskLetter $diskVolume Removed."
- Write-Host $(net share $shareName /Delete)
- }
- }
- }
- 5 {
- $sharePath = $diskLetter
- $shareName = $($sharePath -replace ":","_") + $diskVolume
-
- Switch ($action) {
- "__InstanceCreationEvent" {
- Write-Host "Optical Drive $diskLetter $diskVolume Inserted."
- Write-Host $(net share $shareName=$sharePath)
- }
- "__InstanceDeletionEvent" {
- Write-Host "Optical Drive $diskLetter $diskVolume Removed."
- Write-Host $(net share $shareName /Delete)
- }
- }
- }
- }
- }
-
-
- Unregister-Event mountEvent -ErrorAction SilentlyContinue
- Register-WmiEvent -SourceIdentifier mountEvent -Query $psQuery -Action $psAction
复制代码
|