启动某个程序时运行脚本

时间:2014-09-22 19:47:20

标签: powershell windows-7 scripting

启动特定文件时是否可以运行脚本? (在这种情况下访问)如果是这样,是否可以使用Windows任务调度程序?另外,请记住我使用powershell来开发脚本。

1 个答案:

答案 0 :(得分:1)

也许您可以使用WMI事件观察程序。此代码段等待启动calc.exe,然后运行脚本将优先级调整为低。它会在等待生成下一个calc.exe实例时循环。

PowerShell script that runs in background changing specific process priority to low whenever it is ran

$prog = Get-Process -Name calc | ?{$_.PriorityClass = [System.Diagnostics.ProcessPriorityClass]::Idle}

    while($true)
    {
        $Query = "select * from __instanceCreationEvent within 1 where targetInstance isa 'win32_Process' AND TargetInstance.Name = 'calc.exe'"
            $Eventwatcher = New-Object management.managementEventWatcher $Query

            $Event = $Eventwatcher.waitForNextEvent()

            $prog = Get-Process -Name calc | ?{$_.PriorityClass = [System.Diagnostics.ProcessPriorityClass]::Idle}
    }

其他信息。

http://blogs.technet.com/b/heyscriptingguy/archive/2012/06/08/an-insider-s-guide-to-using-wmi-events-and-powershell.aspx

http://blogs.technet.com/b/heyscriptingguy/archive/2009/01/19/how-can-i-be-notified-when-a-process-begins.aspx

另一个例子。

https://superuser.com/questions/693596/how-to-know-which-service-is-responsible-for-any-exe-running/693601#693601