如何以特定顺序启动多个服务,如果没有可用的服务,它将跳至下一个可用的服务?

时间:2018-11-21 02:11:33

标签: powershell

此脚本将找到变量集中列出的服务。如果服务未运行,它将启动服务。它还以特定顺序检查并启动服务。它还从文本文件读取服务器。

但是,这里的问题是如何制作它,以便如果该服务不可用,它将跳至下一个可用服务?当前,该脚本仅在没有可用服务时停止。

Function startOTCSServices {

$ComputerPath = "C:\PS Scripts\OTCS CSIRT\servernames.otcs.txt"
$OTCSServicePath = "KIC-ED-MC02"
$OTCSServicePath1 = "KIC-ED-MC"
$OTCSServicePath2 = "Ascent Capture Service"
$OTCSServicePath3 = "ACIS Remote Synchronization Agent"

try {
    $ComputerNames = Get-Content -Path $ComputerPath  

    if ($ComputerNames -ne $null) 
    {    

        foreach ($c in $ComputerNames) 
        {
            Write-Host "Server:" $c -ForegroundColor Yellow
            Write-Host "-------------------------------------------"
            Write-Host "Starting Services"
            Write-Host "-------------------------------------------"

            $p = Get-Service -Name $OTCSServicePath -ComputerName $c -ErrorAction SilentlyContinue
            $p1 = Get-Service -Name $OTCSServicePath1 -ComputerName $c -ErrorAction SilentlyContinue
            $p2 = Get-Service -Name $OTCSServicePath2 -ComputerName $c -ErrorAction SilentlyContinue
            $p3 = Get-Service -Name $OTCSServicePath3 -ComputerName $c -ErrorAction SilentlyContinue

                while ($p.Status -ne "Running") 
                {              
                    Start-Service $p -Verbose                          
                    Start-Sleep -s 5
                    $p.Refresh() 
                    if ($p.Status -eq "Running") 
                    {
                        Write-Host $p "is now started" -ForegroundColor Green 
                    }
                }
                while ($p1.Status -ne "Running") 
                {   
                    Start-Service $p1 -Verbose                          
                    Start-Sleep -s 5
                    $p1.Refresh() 
                    if ($p1.Status -eq "Running") 
                    {
                        Write-Host $p1 "is now started" -ForegroundColor Green
                    }
                }
                while ($p2.Status -ne "Running") 
                {   
                    Start-Service $p2 -Verbose                          
                    Start-Sleep -s 5
                    $p2.Refresh() 
                    if ($p2.Status -eq "Running") 
                    {
                        Write-Host $p2 "is now started" -ForegroundColor Green
                    }
                }
                while ($p3.Status -ne "Running") 
                {   
                    Start-Service $p3 -Verbose                          
                    Start-Sleep -s 5
                    $p3.Refresh() 
                    if ($p3.Status -eq "Running") 
                    {
                        Write-Host $p3 "is now started" -ForegroundColor Green
                    }
                }
         }             
    } else { Write-Host "No computers found in file" }
     } catch { Write-Host $_ }

}

0 个答案:

没有答案