test

<pre>

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’

}

</pre>

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.