test

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
3 months, 1 week ago.

  • Author
    Posts
  • #111223
    Tim

    Participant
    Points: 0
    Rank: Member
    
    function fnStartService {
    
    param($serverName, $serviceName)
    
    $svcStart=0
    
    $continue = 'TRUE'
    
    do {
    
    $webService = Get-Service -ComputerName $serverName | Where-Object {$_.name -eq $serviceName}
    
    if ($svcStart -ge 48){
    
    throw " Cannot establish service control on $serverName for $serviceName."
    
    } elseif ($webService.Status -ieq 'Stopped') {
    
    $continue = 'FALSE'
    
    $SVC = new-Object System.ServiceProcess.ServiceController($serviceName,$serverName)
    
    $SVC.Start()
    
    Write-Host "$serviceName service stopped, start command sent"
    
    } elseif ($webService.Status -ieq 'Running'){
    
    $continue = 'FALSE'
    
    Write-Host "$serviceName running."
    
    } else {
    
    Write-Host "$serviceName cannot accept start command, waiting 5 seconds to try again."
    
    Start-Sleep 5
    
    $svcStart++
    
    }
    
    }while ($continue -ieq 'TRUE')
    
    }
    
     
    
    function fnValidateServiceStarted {
    
    param($serverName, $serviceName)
    
    $svcStart=0
    
    $continue = 'TRUE'
    
    do {
    
    $SVC = new-Object System.ServiceProcess.ServiceController($serviceName,$serverName)
    
    if (($SVC.get_Status()) -ieq "Running" ) {
    
    $continue = 'FALSE'
    
    Write-Host "Validated $serviceName service started."
    
    } elseif ($svcStart -ge 48) {
    
    $continue = 'FALSE'
    
    throw "Waited for 240 seconds for service to start."
    
    } else {
    
    $svcStart++
    
    Write-Host $serviceName $SVC.get_Status()
    
    Start-Sleep 5
    
    }
    
    } while ($continue -ieq 'TRUE')
    
    }
    
     
    
    Foreach ($server in Get-Content server.txt) {
    
    fnStartService -serverName $server -serviceName 'SVC'
    
    fnValidateServiceStarted -serverName $server -serviceName 'SVC'
    
    }
    
     
    
    sleep 120
    
    Foreach ($server in Get-Content server.txt) {
    
    fnStartService -serverName $server -serviceName 'W3SVC'
    
    fnValidateServiceStarted -serverName $server -serviceName 'W3SVC'
    
     
    
    Start-Sleep 5
    
    fnStartService -serverName $server -serviceName 'SVCNAME'
    
    }
    
    
  • #111235

    Participant
    Points: 894
    Helping Hand
    Rank: Major Contributor

    Helow, What are you trying to achieve using above code, What error are you facing ?

    You just need to use Start-Service command, The cmdlet is intelligent enough to do what you are doing.

The topic ‘test’ is closed to new replies.