2014-01-05 53 views
0

我正在尋找一種方式來執行Web窗體提交,如果應用程序正在運行。我不確定最好的方法,但我確實創建了一個PowerShell腳本來完成我想要的操作。Web服務調用,如果應用程序正在運行

while($true) { 
     (Invoke-WebRequest -Method post 'Http://website.com').Content; 
     Start-Sleep -Seconds 600; 
    } 

現在我想要做的是運行這個只有當應用程序正在運行,然後退出如果應用程序不再運行。我懷疑Windows服務可能會是答案嗎?如果是這樣,任何想法我怎麼能做到這一點? 我也曾考慮過將此作爲Google Chrome擴展程序運行,但之後我的googlefu已經耗盡。對於Chrome,我只需要腳本,不需要檢查.exe。

任何想法或幫助,將不勝感激。再一次,我在這裏深深地發現了自己的需求,但是我們發現需要創造一些東西,所以我們非常需要虛擬步驟。

+0

您可能會考慮使用進程審計在應用程序啓動時生成日誌事件,並在Task Scheduler看到日誌中的啓動事件時運行該腳本。 – mjolinor

回答

0

如果你知道,應用程序運行的進程的名稱,你可以做到以下幾點:

$processname = "thing" 

# Wait until the process is detected 
Do { 
    Sleep 60 
} Until (Get-Process $processName) 

# Once it is detected, run the script 
# < SCRIPT RUN CODE HERE > 

While (1) { 


    # Monitor the process to make sure it is still running 
    If (Get-Process $processName) {  
     Continue    
    } 

    Else { 
     # Stop the script, because the process isn't running. 
     # < SCRIPT STOP CODE HERE > 

     # Wait until the process is detected again 
     Do { 
      Sleep 60 
     } Until (Get-Process $processName) 
     # Once it is detected again, run the script 
     # < SCRIPT RUN CODE HERE > 
    } 

    # You can add in a delay here to slow down the loop 
    # Sleep 60 
} 
+0

我認爲這可能是接近的,但我正在尋找的是一個不斷的監視器。就像打開計算機,尋找.exe或進程。如果沒有,等待,如果進程,然後執行腳本。繼續監視腳本以確保進程正在運行。如果進程不再運行,請暫停或停止腳本,然後再次監視,直到進程再次運行,然後重新啓動或恢復腳本。 – user3163728

+0

我經歷了您的評論,並試圖將其分解爲邏輯步驟,我編輯了該帖子以嘗試創建我認爲您想要的代碼。 –

+0

所以這樣的事情? http://pastebin.com/pXVMQw7X是需要的腳本的第二個副本還是我可以直接調用原始版本? – user3163728

0

我想你要尋找的可能是WMI事件觸發。你可以註冊(並響應)內WMI發生的事件,諸如:

  • 當過程開始或停止
  • 當一個服務啓動或停止
  • 當一個進程超過一定量的存儲器使用
  • 當安裝一個新版本的設備驅動程序
  • 當計算機被分配給一個新的組織單位
  • 當接通或斷開一個用戶登錄
  • 當環境變量改變
  • 當筆記本電腦的電池下降到低於某個閾值
  • 千其他情況下

要WMI事件註冊,使用Register-WmiEvent小命令。您可以使用-Action參數來聲明在檢測到匹配事件時要執行的PowerShell語句。下面是一個簡單的例子:

# 1. Start notepad.exe 
notepad; 

# 2. Register for events when Notepad disappears 
# 2a. Declare the WMI event query 
$WmiEventQuery = "select * from __InstanceDeletionEvent within 5 where TargetInstance ISA 'Win32_Process' and TargetInstance.Name = 'notepad.exe'"; 
# 2b. Declare the PowerShell ScriptBlock that will execute when event is matched 
$Action = { Write-Host -ForegroundColor Green -Object ('Process stopped! {0}' -f $event.SourceEventArgs.NewEvent.TargetInstance.Name) }; 
# 2c. Register for WMI events 
Register-WmiEvent -Namespace root\cimv2 -Query $WmiEventQuery -Action $Action -SourceIdentifier NotepadStopped; 

# 3. Stop notepad.exe 
# Note: For some reason, if you terminate the process as part of the same thread, the event 
#  doesn't seem to fire correctly. So, wrap the Stop-Process command in Start-Job. 
Start-Job -ScriptBlock { Stop-Process -Name notepad; }; 

# 4. Wait for event consumer (action) to fire and clean up the event registration 
Start-Sleep -Seconds 6; 
Unregister-Event -SourceIdentifier NotepadStopped; 

FYI:我開發了一個名爲PowerEvents PowerShell的模塊,在CodePlex上託管。該模塊包含註冊永久性WMI事件訂閱的功能,並且包含30多頁PDF文檔,可幫助您瞭解WMI事件。你可以在http://powerevents.codeplex.com找到這個開源項目。

如果我想讓你的代碼適應一些對你來說更實用的代碼,它可能看起來像下面的例子。您可以使用Windows任務計劃程序定期調用代碼。

# 1. If process is not running, then exit immediately 
if (-not (Get-Process -Name notepad)) { throw 'Process is not running!'; return; } 

# 2. Register for events when Notepad disappears 
# 2a. Declare the WMI event query 
$WmiEventQuery = "select * from __InstanceDeletionEvent within 5 where TargetInstance ISA 'Win32_Process' and TargetInstance.Name = 'notepad.exe'"; 
# 2b. Declare the PowerShell ScriptBlock that will execute when event is matched 
#  In this case, it simply appends the value of the $event automatic variable to a 
#  new, global variable named NotepadEvent. 
$Action = { $global:NotepadEvent += $event; }; 
# 2c. Register for WMI events 
Register-WmiEvent -Namespace root\cimv2 -Query $WmiEventQuery -Action $Action -SourceIdentifier NotepadStopped; 

# 3. Wait indefinitely, or until $global:NotepadEvent variable is NOT $null 
while ($true -and -not $global:NotepadEvent) { 
    Start-Sleep -Seconds 600; 
    (Invoke-WebRequest -Method post 'Http://website.com').Content; 
} 
相關問題