2017-11-03 417 views
2

當某個事件發生時,我需要啓動PowerShell腳本,並且我正在使用WMI類來獲取持久性。我只能部分工作,並且需要一些幫助才能使其充分發揮作用。所以,這裏是什麼工作,什麼不...WMI事件訂閱和PowerShell執行

下面的代碼工作,並將啓動calc.exe後在後臺啓動PowerShell(爲簡單起見,我選擇此事件僅用於測試目的)。

$fname = "testFilter" 
$cname="testConsumer" 
$exePath="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" 
$query="SELECT * FROM __InstanceCreationEvent WITHIN 5 WHERE TargetInstance ISA 'Win32_Process' AND TargetInstance.Name='calc.exe'" 
$WMIEventFilter=Set-WmiInstance -Class __EventFilter -Namespace "root\subscription" -Arguments @{Name=$fname;EventNameSpace="root\cimv2";QueryLanguage="WQL";Query=$query} 
$WMIEventConsumer=Set-WmiInstance -Class CommandLineEventConsumer -Namespace "root\subscription" -Arguments @{Name=$cname;ExecutablePath=$exePath} 
Set-WmiInstance -Class __FilterToConsumerBinding -Namespace "root\subscription" -Arguments @{Filter=$WMIEventFilter;Consumer=$WMIEventConsumer} | out-null 

但是,如果我修改$exePath變量參數傳遞給powershell.exe那麼它不工作了(沒有PowerShell進程被創建)。

我還嘗試用CommandLineEventConsumer替換爲ActiveScriptEventConsumer,並使用VBScript啓動powershell。下面是修改後的代碼(僅線3和5是不同的):

$fname = "testFilter" 
$cname="testConsumer" 
$scriptPath="D:\Work\LaunchPowerShell.vbs" 
$query="SELECT * FROM __InstanceCreationEvent WITHIN 5 WHERE TargetInstance ISA 'Win32_Process' AND TargetInstance.Name='calc.exe'" 
$WMIEventFilter=Set-WmiInstance -Class __EventFilter -Namespace "root\subscription" -Arguments @{Name=$fname;EventNameSpace="root\cimv2";QueryLanguage="WQL";Query=$query} 
$WMIEventConsumer=Set-WmiInstance -Class ActiveScriptEventConsumer -Namespace "root\subscription" -Arguments @{Name=$cname;ScriptFileName=$scriptPath;ScriptingEngine="VBScript"} 
Set-WmiInstance -Class __FilterToConsumerBinding -Namespace "root\subscription" -Arguments @{Filter=$WMIEventFilter;Consumer=$WMIEventConsumer} | out-null 

而LaunchPowerShell.vbs:

Dim objShell : Set objShell = WScript.CreateObject("WScript.shell") 
objShell.run("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe D:\Work\MyScript.ps1") 

VB腳本按預期工作從一個命令提示啓動時(CMD。 exe),但沒有運氣可以在事件被觸發時(即,啓動calc.exe時)使PowerShell運行。即使我從powershell參數中刪除腳本,它也不會運行,所以不確定問題是什麼。

如果有人可以幫助,將不勝感激。謝謝!!!

+0

我想你想看看[CommandLineEventConsumer](https://msdn.microsoft.com/en-us/library/aa389231(v = vs.85).aspx)類的CommandLineTemplate屬性。您可以指定命令行的內容。 –

回答

2

如果您指定CommandLineTemplate而不是ExecutablePath,則可以爲您的字符串添加參數。

$fname = "testFilter" 
$cname = "testConsumer" 
$CommandLineTemplate = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File D:\Work\MyScript.ps1" 
$ExecutablePath = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" 
$query = "SELECT * FROM __InstanceCreationEvent WITHIN 5 WHERE TargetInstance ISA 'Win32_Process' AND TargetInstance.Name='calc.exe'" 

$WMIEventFilter = Set-WmiInstance -Class __EventFilter -Namespace "root\subscription" -Arguments @{Name=$fname;EventNameSpace="root\cimv2";QueryLanguage="WQL";Query=$query} 
$WMIEventConsumer = Set-WmiInstance -Class CommandLineEventConsumer -Namespace "root\subscription" -Arguments @{Name=$cname;CommandLineTemplate=$CommandLineTemplate;ExecutablePath=$ExecutablePath } 

Set-WmiInstance -Class __FilterToConsumerBinding -Namespace "root\subscription" -Arguments @{Filter=$WMIEventFilter;Consumer=$WMIEventConsumer} | out-null 

Source

CommandLineTemplate

數據類型:字符串

訪問類型:只讀,指定要啓動的過程

標準字符串模板。該屬性可以爲NULL,並且ExecutablePath屬性用作命令行。

+0

謝謝肖恩。我一定忽略了這個物業。 –