【问题标题】:Running a script when you launch a certain Program启动某个程序时运行脚本
【发布时间】:2014-11-16 21:19:46
【问题描述】:

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

【问题讨论】:

  • 你可以有一个脚本来启动你的访问文件作为脚本的一部分,这绝对可以作为一个预定的工作来完成。
  • 本地机器上有一个文件,服务器上有一个文件。每次我打开本地机器文件时,我都想检查服务器是否有文件的更新版本,如果存在则复制它。
  • 我真的不明白你为什么希望它成为计划任务。只需编写一个执行If((gci \\server\share\file).lastwritetime -lt (gci c:\path\to\file).lastwritetime){copy-item \\server\share\file -destination c:\path\to\file -force;& c:\path\to\file} 的脚本,并在您想要打开数据库时调用该脚本。
  • 好吧,我想我的问题是,如何将 powershell 脚本附加到 exe?
  • 你可以认真修改我刚才给你的代码中的路径,然后按原样运行脚本来做你想做的事。不要再认为启动 EXE 时需要运行脚本,而是将其视为启动脚本时运行的 EXE。

标签: powershell windows-7 scripting


【解决方案1】:

也许您可以使用 WMI 事件观察器。这个 sn -p 等待 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    • 1970-01-01
    • 2023-01-20
    • 1970-01-01
    • 2016-05-22
    • 2020-09-19
    • 1970-01-01
    相关资源
    最近更新 更多