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

我想要一个PowerShell脚本在后台每分钟运行一次。 没有窗口可能会出现。 我该怎么做?

使用Windows任务计划程序并像这样运行脚本:

powershell -File myScript.ps1 -WindowStyle Hidden 

此外创建它运行在一个特定的用户帐户,而不是只有当该用户登录的脚本。 否则,你会看到一个控制台窗口。

也许这个场景会做。 我们不会每分钟启动一次PowerShell可执行文件(这很贵,顺便说一下)。 相反,我们通过调用一个额外的脚本来启动一次,即每分钟调用一次脚本脚本(或者在脚本脚本退出或失败后实际等待一分钟)。

创建启动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 ,它会被最小化,并在一瞬间永久隐藏。 所以实际上只有任务栏图标显示一会儿,而不是窗口本身。 这不是太糟糕。

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

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

/ru开关可以让您更改计划任务将执行的用户上下文。

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

这些答案是很奇怪的! 如果要将powershell脚本作为后台作业,请尝试在脚本所在的文件夹中从CLI启动。\ script。

如果你想在时间间隔内自动运行脚本。 (用于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! 
 # 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 )