每分钟在后台运行一次powershell脚本

时间:2010-01-15 12:46:16

标签: windows powershell scheduling

我希望PowerShell脚本在后台每分钟运行一次。不会出现任何窗口。我该怎么做?

9 个答案:

答案 0 :(得分:23)

使用Windows任务计划程序并运行您的脚本:

powershell -File myScript.ps1 -WindowStyle Hidden

此外,创建在特定用户帐户下运行的脚本,并且不仅在该用户登录时。否则你会看到一个控制台窗口。

答案 1 :(得分:16)

也许这种情况会发生。我们不会每分钟启动PowerShell可执行文件(这很昂贵,BTW)。相反,我们通过调用一个额外的脚本来启动它一次,该脚本每分钟调用一次工作脚本(或者在工作脚本退出或失败后实际等待一分钟)。

创建起始Invoke-MyScript.ps1:

for(;;) {
 try {
  # invoke the worker script
  C:\ROM\_110106_022745\MyScript.ps1
 }
 catch {
  # do something with $_, log it, more likely
 }

 # wait for a minute
 Start-Sleep 60
}

从Cmd运行(例如从启动.bat文件):

start /min powershell -WindowStyle Hidden -Command C:\ROM\_110106_022745\Invoke-MyScript.ps1

PowerShell窗口暂时出现,但由于start /min而被最小化,并且只是在一瞬间它被永久隐藏。实际上只有一个任务栏图标会出现,而不是窗口本身。这不是太糟糕。

答案 2 :(得分:10)

安排您的任务作为系统运行。下面的命令将安排脚本在后台运行,而不显示powershell控制台窗口:

schtasks /create /tn myTask /tr "powershell -NoLogo -WindowStyle hidden -file myScript.ps1" /sc minute /mo 1 /ru System

/ru开关允许您更改将在其中执行计划任务的用户上下文。

答案 3 :(得分:3)

要创建后台Powershell任务以运行每分钟重复一次,根本没有可见窗口的脚本,请以管理员身份运行powershell,然后使用Register-ScheduledJob cmdlet运行脚本。这是如何实现此目标的示例:

Register-ScheduledJob -Name 'SomeJobName' -FilePath 'path\to\your\ps1' -Trigger (New-JobTrigger -Once -At "9/28/2018 0am" -RepetitionInterval (New-TimeSpan -Minutes 1) -RepetitionDuration ([TimeSpan]::MaxValue))

如果要手动强制运行此作业(可能出于故障排除目的),则可以使用Get-ScheduledJob cmdlet,如下所示:

(Get-ScheduledJob -Name 'SomeJobName').StartJob()

答案 4 :(得分:2)

在任务的任务计划程序属性的[常规]选项卡上,选择"运行用户是否已登录"单选按钮可防止窗口出现在我的Windows 7计算机上。

答案 5 :(得分:1)

我在我们 IT 组织的约束下工作,该组织禁止执行脚本。因此,我仅限于在线。这是我使用的:

while ($true) {<insert command(s) here>;start-sleep 2}

我是 PowerShell 的菜鸟,所以如果你有任何反馈,请保持温和;-)

答案 6 :(得分:0)

这些答案是绝对的! 如果要将powershell脚本作为后台作业运行,请尝试 start-job。\ script 从您容纳脚本的文件夹中的CLI。

答案 7 :(得分:0)

如果要在时间间隔内自动运行脚本。 (对于Windows操作系统)

1)  Open Schedule tasks from control panel.
2)  Select Task Scheduler Library from left side window.
3)  select Create Task from right side window.
4)  Enter Name in General tab (any name).
5)  Open Triggers tab -> New
6)  Select interval as needed.Use Advanced settings for repeat task. 
7)  Open Actions tab ->New
8)  Copy/Paste following line in Program/script *

* Here D:\B.ps1 in code is path to my B.ps1 file

C:\ Windows \ system32 \ WindowsPowerShell \ v1.0 \ powershell.exe D:\ B.ps1

9)  ok and apply changes
10) Done!  

答案 8 :(得分:-2)

#   Filecheck.ps1

#   Version 1.0

#   Use this to simply test for the existance of an input file... Servers.txt.

#   I want to then call another script if the input file exists where the

#   servers.txt is neded to the other script.

#


    $workpath=".\Server1\Restart_Test"

#   Created a functon that I could call as part of a loop.


    function Filecheck

    {
    if (test-path $workpath\servers.txt)

        {

        rename-item $workpath\servers.txt servers1.txt

        "Servers.txt exist... invoking an instance of your script agains the list of servers"


        Invoke-Expression .\your_Script.ps1

        }

    Else
        {

            "sleeping"

            Start-Sleep -Seconds 60
        }

     }

    Do
        {
        Filecheck

        $fred=0
        # Needed to set a variabe that I could check in the while loop.

        # Probably a better way but this was my way.

        }

        While( $fred -lt 1 )