usb - Powershell WMI: Runs without any errors/exceptions, however doesn't execute script -
$filter = ([wmiclass]"\\.\root\subscription:__eventfilter").createinstance() $filter.querylanguage = "wql" $filter.query = "select * __instancecreationevent within 5 targetinstance isa 'win32_logicaldisk'" $filter.name = "usbfilter" $filter.eventnamespace = 'root\cimv2' $result = $filter.put() $filterpath = $result.path $consumer = ([wmiclass]"\\.\root\subscription:commandlineeventconsumer").createinstance() $consumer.name = 'usbconsumer' $consumer.commandlinetemplate = "c:\windows\system32\windowspowershell\v1.0\powershell.exe –executionpolicy bypass -file c:\test.ps1" $consumer.executablepath = "c:\windows\system32\windowspowershell\v1.0\powershell.exe" $consumer.workingdirectory = "c:\" $result = $consumer.put() $consumerpath = $result.path $bind = ([wmiclass]"\\.\root\subscription:__filtertoconsumerbinding").createinstance() $bind.filter = $filterpath $bind.consumer = $consumerpath $result = $bind.put() $bindpath = $result.path
the above code supposed run script when windows detects usb device inserted runs fine (no error/warnings/exceptions) on insertion of device, script, supposed display message box. doesn't. have tested triggered script on own , dialogue box displays fine.
i not familiar wmis , persistant 1 less, @ appreciated
since event consumer invoked wmi host process running under system
account, won't see in own desktop session.
if change c:\test.ps1
contents write event log instead:
$logsource = "usb detector" if(-not [system.diagnostics.eventlog]::sourceexists($logsource)) { new-eventlog -logname application -source $logsource } write-eventlog -logname application -source $logsource -eventid 100 -message "new disk attached!"
you'll see works fine:
Comments
Post a Comment