Unregister-Event RemovableDiskDetection
$query = "SELECT * FROM __InstanceOperationEvent WITHIN 5 WHERE TargetInstance ISA 'Win32_LogicalDisk' AND TargetInstance.DriveType=2"
Register-WmiEvent -Query $query -SourceIdentifier RemovableDiskDetection -Action {
$class = $eventArgs.NewEvent.__CLASS
$device = $eventArgs.NewEvent.TargetInstance.DeviceID
$wshell = New-Object -ComObject Wscript.Shell
switch($class)
{
__InstanceCreationEvent {
$path = $device + "\flag"
Write-Host "*** Checking the existence of the file $path"
if(Test-Path -Path $path)
{
$wshell.Popup("Inserted, device id: $device WITH flag",0,"Done",0x1)
}
else{
$wshell.Popup("Inserted, device id: $device WITHOUT flag",0,"Done",0x1)
}
}
__InstanceDeletionEvent {
$wshell.Popup("Removed, device id: $device ",0,"Done",0x1)
}
}
}
如果我从Windows Powershell ISE运行脚本,它按预期工作。如果我打开一个CMD窗口并运行命令" powershell -noexit -file basicUSBControl.ps1" ,它也按预期工作。
我的问题是当我尝试为整个计算机设置此脚本的执行时,会影响可能登录到计算机的任何用户。 我尝试通过本地组策略编辑器。我去了 计算机配置 - > Windows设置 - >脚本 - >启动 然后我选择了Powershell脚本选项卡并设置与以前相同的脚本。我还在下拉框中选择了选项"首先运行Windows Powershell脚本"。
然后我继续重新启动计算机并登录它,每当我插入任何USB记忆棒时似乎都没有发生。
知道可能会发生什么事吗?有人能帮助我吗?我变得疯狂了,也许是不可能做我需要做的事情.... 我还尝试了一个计划任务,结果任务状态显示" Running"所有的时间但不是事件被捕获,而不是弹出窗口显示插拔时,usb棒...:/
非常感谢!
答案 0 :(得分:0)
解决方案是创建一个创建计划任务的GPO。 在那里,您可以指定将运行脚本的用户,在我的情况下, %LogonDomain%\%的LogonUser%