while循环中两个逻辑相反的结束条件

时间:2014-01-17 20:30:22

标签: powershell

我在powershell中有一个函数,它将根据函数的输入参数停止服务或启动服务。它位于一个循环内,如果第一次失败则检查5个计数。现在它被设置为当服务的状态停止时它应该结束。如果服务的状态已经开始,我如何检查相同的循环...这是一个逻辑问题,你是否可以在while循环中具有截然相反的结束条件。但是我有一些缩写代码......

[System.ServiceProcess.ServiceController]$service = Get-Service -Name $ServiceName -ComputerName $Remoteserver
[int]$waitCount = 5
do
{
    $waitCount--

    switch($service.Status)
    {
            { @(
            [System.ServiceProcess.ServiceControllerStatus]::ContinuePending,
            [System.ServiceProcess.ServiceControllerStatus]::PausePending,
            [System.ServiceProcess.ServiceControllerStatus]::StartPending,
            [System.ServiceProcess.ServiceControllerStatus]::StopPending) -contains $_ }
            {
                # A status change is pending. Do nothing.
                break;
            }
            { @(
            [System.ServiceProcess.ServiceControllerStatus]::Paused,
            [System.ServiceProcess.ServiceControllerStatus]::Running) -contains $_ }
            {
                # The service is paused or running. We need to stop it.
                if($StopOrStart -eq "stop"){
                    $service.Stop()
                    write-host("Stopped.")
                    break;
                }
            }
            { @(
            [System.ServiceProcess.ServiceControllerStatus]::Stopped) -contains $_ }
            {
             #if Stop or Start is equal to start then start the service.
                if($StopOrStart -eq "start"){
                    $service.Start()
                    write-host("Started.")
                    break;
                }
            }            
    }
    # Sleep, then refresh the service object.
    Sleep -Seconds 1
    $service.Refresh()
} while (($service.Status -ne [System.ServiceProcess.ServiceControllerStatus]::Stopped) -and $waitCount -gt 0) 

如果$ stop.Status -ne启动,如果$ StopOrStart等于“start”,我试图想出一个结束while循环的方法吗?

我真的不想写两个执行相同操作的函数,一个用于启动服务,另一个用于停止,当我可以执行它时。

2 个答案:

答案 0 :(得分:1)

实例化一个布尔值,用于指示操作是否成功。

[bool] $ WasSuccessful = $ false

然后在条件步骤测试中确保成功设置状态。如果是,那就纾困。匹配你的工作方式:

[bool]$WasSuccessful = $false

[System.ServiceProcess.ServiceController]$service = Get-Service -Name $ServiceName -ComputerName $Remoteserver
[int]$waitCount = 5
do
{
    $waitCount--

    switch($service.Status)
    {
            { @(
            [System.ServiceProcess.ServiceControllerStatus]::ContinuePending,
            [System.ServiceProcess.ServiceControllerStatus]::PausePending,
            [System.ServiceProcess.ServiceControllerStatus]::StartPending,
            [System.ServiceProcess.ServiceControllerStatus]::StopPending) -contains $_ }
            {
                # A status change is pending. Do nothing.
                break;
            }
            { @(
            [System.ServiceProcess.ServiceControllerStatus]::Paused,
            [System.ServiceProcess.ServiceControllerStatus]::Running) -contains $_ }
            {
                # The service is paused or running. We need to stop it.
                if($StopOrStart -eq "stop"){
                    $service.Stop()
                    If ((Get-Service -ComputerName $Remoteserver -Name $ServiceName).Status -eq "Stopped") {
                        write-host("Stopped.")
                        $WasSuccessful = $true
                    }
                }
            }
            { @(
            [System.ServiceProcess.ServiceControllerStatus]::Stopped) -contains $_ }
            {
             #if Stop or Start is equal to start then start the service.
                if($StopOrStart -eq "start"){
                    If ((Get-Service -ComputerName $Remoteserver -Name $ServiceName).Status -eq "Running") {
                        write-host("Started")
                        $WasSuccessful = $true
                    }
                }
            }            
    }
    # Sleep, then refresh the service object.
    Sleep -Seconds 1
    $service.Refresh()
} while (($WasSuccessful -ne $true) -and $waitCount -gt 0)  

答案 1 :(得分:0)

未经测试:

$service = Get-Service -Name $ServiceName -ComputerName $Remoteserver
$waitCount = 5
do
{
    $waitCount--

    switch -Regex ($service.Status)
    {
       'Pending$'  
        {
          # A status change is pending. Do nothing.
          break;
        }

       'Paused'
        {
          # The service is paused.  We need to stop or start it.
            if($StopOrStart -eq "stop"){
                $service.Stop()
                write-host("Stopped.")}

            if($StopOrStart -eq "start"){
                $service.Start()
                write-host("Started.")}   
        }

       'Stopped'
        {
          if($StopOrStart -eq "Stop"){ Return }

          if($StopOrStart -eq "Start"){
            $service.Start()
            write-host("Started.")}

        }

     'Started'
       {
          if($StopOrStart -eq "Start"){ Return }

          if($StopOrStart -eq "Stop"){
            $service.Stop()
            write-host("Stopped.")}

       }      
    }

    # Sleep, then refresh the service object.
    Sleep -Seconds 1
    $service.Refresh()

 } While ($waitCount -gt 0)