Web服务调用,如果应用程序正在运行

时间:2014-01-05 23:32:27

标签: powershell service

我正在寻找一种在应用程序运行时执行Web表单提交的方法。我不确定最好的方法,但我确实创建了一个PowerShell脚本来完成我想要的。

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

现在我想要做的只是在应用程序运行时运行它,然后在应用程序不再运行时退出。我怀疑可能是Windows服务的答案?如果是这样,任何想法我怎么能做到这一点? 我还考虑将其作为Google Chrome扩展程序运行,但后来我的googlefu已经用尽了。对于Chrome,我只需要脚本而无需检查.exe。

任何想法或帮助将不胜感激。再次,我在这里已经超出了我的深度,但已经发现需要创建一些东西,所以非常需要虚拟步骤。

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:0)

我认为您正在寻找的可能是WMI事件。您可以注册(和响应)WMI中发生的事件,例如:

  • 当流程开始或停止时
  • 服务开始或停止时
  • 当进程超过一定的内存使用量时
  • 安装新版本设备驱动程序时
  • 将计算机分配给新的单位部门时
  • 当用户登录或注销时
  • 当环境变量发生变化时
  • 笔记本电池电量低于某个阈值时
  • 成千上万其他案例

要注册WMI事件,请使用Register-WmiEvent cmdlet。您可以使用-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;

仅供参考:我开发了一个名为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;
}