【发布时间】:2011-01-05 12:03:57
【问题描述】:
我希望在后台每分钟运行一次 powershell 脚本。不会出现任何窗口。我该怎么做?
【问题讨论】:
-
你知道如何创建定时任务吗?
标签: windows powershell scheduling
我希望在后台每分钟运行一次 powershell 脚本。不会出现任何窗口。我该怎么做?
【问题讨论】:
标签: windows powershell scheduling
使用 Windows 任务计划程序并像这样运行您的脚本:
powershell -File myScript.ps1 -WindowStyle Hidden
此外,创建它在特定用户帐户下运行的脚本,不仅在该用户登录时。否则你会看到一个控制台窗口。
【讨论】:
安排您的任务作为系统运行。下面的命令将安排一个脚本在后台运行,而不显示 powershell 控制台窗口:
schtasks /create /tn myTask /tr "powershell -NoLogo -WindowStyle hidden -file myScript.ps1" /sc minute /mo 1 /ru System
/ru 开关可让您更改执行计划任务的用户上下文。
【讨论】:
system 并单击检查名称,它应该填充对象NT AUTHORITY\SYSTEM。这是阻止 PS 窗口出现在我的屏幕上的唯一解决方案。
也许这种情况可以。我们不会每分钟都启动 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 任务以运行每分钟重复一次且根本没有可见窗口的脚本,请以管理员身份运行 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()
【讨论】:
在任务的任务计划程序属性中的 [常规] 选项卡上,选择“无论用户是否登录都运行”单选按钮可防止该窗口出现在我的 Windows 7 机器上。
【讨论】:
我在我们的 IT 组织的限制下工作,该组织禁止执行脚本。因此,我仅限于在线。这是我使用的:
while ($true) {<insert command(s) here>;start-sleep 2}
我是 PowerShell 的菜鸟,所以如果您有反馈,请保持温和 ;-)。
【讨论】:
这些答案是历史性的! 如果您想将 powershell 脚本作为后台作业运行,请尝试 开始工作 .\script 从您存放脚本的文件夹中的 CLI。
【讨论】:
如果你想在时间间隔内自动运行脚本。 (适用于 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 )
【讨论】: