使用 PowerShell 每 x 秒运行一个批处理文件 [英] Run a batch file every x number of seconds using PowerShell

查看:84
本文介绍了使用 PowerShell 每 x 秒运行一个批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Windows PowerShell 脚本每隔 x 秒运行一个批处理文件.所以它是一遍又一遍地运行同一个批处理文件.

I'd like to use a windows PowerShell script to run a batch file every x number of seconds. So it's the same batch file being run over and over again.

我进行了一些查找,但找不到我要查找的内容.这是我想在 Windows XP 机器上运行的东西.我以前曾多次使用 Windows Scheduler 进行类似的操作,但出于某种原因,Windows Scheduler 只运行一次......然后就没有了.我也不想让批处理文件自己调用,因为它运行这么多次后会出错.

I've done some looking, but can't find what I'm looking for. It's for something that I want to run on a Windows XP machine. I've used Windows Scheduler a lot of times before for something similar, but for some reason Windows Scheduler only runs once... Then no more. I also don't want to have the batch file call itself, because it will error out after it runs so many times.

推荐答案

只需将批处理文件的调用放在 while 循环中,例如:

Just put the call to the batch file in a while loop e.g.:

$period = [timespan]::FromSeconds(45)
$lastRunTime = [DateTime]::MinValue 
while (1)
{
    # If the next period isn't here yet, sleep so we don't consume CPU
    while ((Get-Date) - $lastRunTime -lt $period) { 
        Start-Sleep -Milliseconds 500
    }
    $lastRunTime = Get-Date
    # Call your batch file here
}

这篇关于使用 PowerShell 每 x 秒运行一个批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆