Как запустить несколько сервисов в определенном порядке, и если сервис недоступен, он перейдет к следующему доступному? - PullRequest
0 голосов
/ 21 ноября 2018

Этот скрипт найдет сервисы, перечисленные в наборе переменных.Если служба не запущена, она запустит службу.Он также проверяет и запускает службу в определенном порядке.Он также считывает серверы из текстового файла.

Однако здесь возникает вопрос: как сделать так, чтобы, если служба недоступна, она переходила к следующей доступной службе?В настоящее время сценарий просто останавливается, когда нет доступной службы.

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 $_ }

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...