Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在powershell中按时间限制while循环_Powershell_Loops_Time_While Loop_Limit - Fatal编程技术网

如何在powershell中按时间限制while循环

如何在powershell中按时间限制while循环,powershell,loops,time,while-loop,limit,Powershell,Loops,Time,While Loop,Limit,我有一个脚本,它只在特定服务运行后启动进程。 这是一个试图获取服务状态的循环。 我找不到如何按时间限制循环 我被卡住的部分: #add Start button $button_start = New-Object System.Windows.Forms.Button $button_start.Location = New-Object System.Drawing.Size(25,70) $button_start.Size = New-Object System.Drawing.Size

我有一个脚本,它只在特定服务运行后启动进程。 这是一个试图
获取服务
状态的循环。 我找不到如何按时间限制循环

我被卡住的部分:

#add Start button
$button_start = New-Object System.Windows.Forms.Button
$button_start.Location = New-Object System.Drawing.Size(25,70)
$button_start.Size = New-Object System.Drawing.Size(240,32)
$button_start.TextAlign = "MiddleCenter"
$button_start.font = New-Object System.Drawing.Font("Segoe UI",14,[System.Drawing.FontStyle]::Regular)
$button_start.BackColor = "seashell"
$button_start.Text = "Start"
$button_start.Add_Click({
    #add statement
    while ((Get-Service -ComputerName $textBox_IP.text -ServiceName wscsvc).Status -ne "Running") {
        # Pause before next check
        Start-Sleep -Seconds 1
    }

    #only then..
    Start-Process -FilePath "C:\Users\username\Desktop\software.exe" -verb RunAs -ArgumentList $textBox_IP.text
})

$Form_remoteControl.Controls.Add($button_start)

我尝试在网络上搜索信息,但没有成功。

定义一个时间限制,并检查当前时间是否超过该限制

$limit = (Get-Date).AddMinutes(5)
while (... -or (Get-Date) -le $limit) {
    Start-Sleep -Seconds 1
}
如果要在服务仍然未运行时跳过启动外部程序,请在返回的循环之后添加另一个检查:

if ((Get-Service ...).Status -ne "Running") {
    return
}

这是一个如何停止服务并等待其停止或应用超时的示例。 您可以修改以启动服务

Function StopService ($serv)
{
    Write-Host "Config service " $serv  " ..."         
    $service = Get-Service $serv -ErrorAction SilentlyContinue
    if ($service)
    {
        if($service.status -eq "running")
        {
            write-host "Stop service" $serv
            Stop-Service $serv  -Force

            # Wait until service is stopped (max. 1 minute)
            $acttime  = 0
            $waittime = 100
            $maxtime  = 60000

            $TestService = Get-Service $serv
            While($TestService | Where-Object {$_.Status -eq 'Running'})
            {                     
                Start-Sleep -m $waittime
                $acttime += $waittime
                if ($acttime -gt $maxtime)
                {
                    write-host "ERROR: Service" $serv " could not be stopped!" -ForegroundColor Red                       
                    return $False
                } 
            }   
        } 
        else
        {
            write-host "Service already stopped!" -ForegroundColor Green   
            return $True             
        }

    }
    else 
    {
        write-host "Service not installed" -ForegroundColor Green
        return $True
    }
}

我建议您不要在Windows窗体界面中使用任何轮询
While
循环(使用
Start Sleep
cmdlet)。它将使您的界面因按钮单击等重要表单事件而暂停。

相反,我会通过创建计时器事件,并在特定时间段后进行适当的检查和操作(例如,根据服务状态启动新的
流程)。

Hi,谢谢你的重播,但我不知道如何将它插入我的script@ATX-750K您发布的代码片段中只有一个
,而
循环,因此请根据我在回答中概述的内容进行调整。如果您自己确实无法完成集成,您可能需要先找到PowerShell教程。我不会给你提供一个交钥匙解决方案。好吧,你不知道,没关系。无论如何,谢谢,谢谢。while(…-或(获取日期)-gt$limit){应该是:while(…-或(获取日期)-le$limit){,但是谢谢你的主要想法