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

发布于 2024-08-18 16:35:32 字数 52 浏览 5 评论 0原文

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

I want a powershell script to be run once per minute in the background. No window may appear. How do I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

左秋 2024-08-25 16:35:32

使用 Windows 任务计划程序并按如下方式运行脚本:

powershell -File myScript.ps1 -WindowStyle Hidden

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

Use the Windows Task Scheduler and run your script like this:

powershell -File myScript.ps1 -WindowStyle Hidden

Furthermore create the script that it runs under a specific user account and not only when that user is logged on. Otherwise you'll see a console window.

私藏温柔 2024-08-25 16:35:32

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

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

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

Schedule your task to be run as System. The command below will schedule a script to be run in the background without showing powershell console window:

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

/ru switch lets you change the user context in which the scheduled task will be executed.

自在安然 2024-08-25 16:35:32

也许这个场景就可以了。我们不会每分钟启动 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 而被最小化,并且仅在那一刻它就被永远隐藏了。因此,实际上只有任务栏图标出现一会儿,而不是窗口本身。还不错。

Perhaps this scenario will do. We do not start PowerShell executable every minute (this is expensive, BTW). Instead, we start it once by calling an extra script that calls the worker script once a minute (or actually waits a minute after the worker script exits or fails).

Create the starting 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
}

Run this from Cmd (e.g. from a startup .bat file):

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

The PowerShell window appears for a moment but it is minimized due to start /min and just in a moment it gets hidden forever. So that actually only the task bar icon appears for a moment, not the window itself. It's not too bad.

ぃ双果 2024-08-25 16:35:32

要创建后台 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()

To create a background powershell task to run a script that repeats every minute with no visible window at all, run powershell as administrator and then use the Register-ScheduledJob cmdlet to run your script. Here's an example of how to make that happen:

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))

If you want to manually force this job to run (perhaps for troubleshooting purposes) you can use the Get-ScheduledJob cmdlet like this:

(Get-ScheduledJob -Name 'SomeJobName').StartJob()
半边脸i 2024-08-25 16:35:32

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

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

我是 PowerShell 新手,所以如果您有反馈意见,请保持温和;-)。

I labor under the constraints of our IT organization, who has disabled execution of scripts. I therefore am limited to on-liners. Here's what I used:

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

I'm a noob to PowerShell, so if you have feedback, please be gentle ;-).

漫雪独思 2024-08-25 16:35:32

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

On the [General] tab in the Task Scheduler properties for the task, selecting the "Run whether user is logged on or not" radio button prevents the window from appearing on my Windows 7 machine.

傲性难收 2024-08-25 16:35:32

如果你想按时间间隔自动运行脚本。 (对于 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!  

If you want to run script automatically in time interval. (for windows os)

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!  
时光磨忆 2024-08-25 16:35:32

这些答案都是历史性的!
如果您想将 powershell 脚本作为后台作业运行,请尝试
开始作业.\脚本
从您存放脚本的文件夹中的 CLI 中。

These answers are histerical!
If you want to run a powershell script as a background job try
start-job .\script
from the CLI within the folder you house scripts in.

森林很绿却致人迷途 2024-08-25 16:35:32
#   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 )
#   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 )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文