Author Posts

January 1, 2012 at 12:00 am

by nitramap at 2013-03-20 13:17:53

n00b question

I want the Script to stop if the service cannot start – I think it due to where I am putting the "-ErrorAction"

$services='service#0','service#1','service#2','service#3',
'service#4','service#5','service#6','service#7','service#8','service#9','service#10',
'service#11','service#12','service#13'
$service_waits=@{Service1=7;Service2=60;Service3=7;Service4=7}
foreach($service in $services){
start-service -name $service
if($service_waits.ContainsKey($service)){
start-sleep -Seconds $service_waits[$service]
and
Start-Service -ErrorAction stop
} else {
#default to 5 seconds
start-sleep -seconds 2
}

Any help would be greatly appreciated

by MasterOfTheHat at 2013-03-20 13:50:33

I'm confused...
So, you are trying to start a list of services that you've stored in $services. After each service starts, you are checking to see if you have one of those services listed in the $service_waits hash table, and if it does, sleep for a certain number of seconds and then tell the service to start again? Otherwise, wait 2 seconds and start the next service?

by nitramap at 2013-03-20 14:18:19

Yes trying to to 14 separate services in order .... Service 1, 2, 3 and 4 have to be started before the script continues so hence putting the ErrorAction in the IF statement .....

Thanks from the n00b

by poshoholic at 2013-03-21 05:02:59

Sounds like you want something more like this:
$services='service#0','service#1','service#2','service#3',
'service#4','service#5','service#6','service#7','service#8','service#9','service#10',
'service#11','service#12','service#13'
$service_waits=@{Service1=7;Service2=60;Service3=7;Service4=7}
foreach($service in $services){
start-service -name $service
if($service_waits.ContainsKey($service)){
start-sleep -Seconds $service_waits[$service]
} else {
#default to 5 seconds
start-sleep -seconds 2
}
#if the current service is not running after the wait, break with an error
$service = get-service -name $service
if ($service.Status -ne 'Running') {
Write-Error "Service $($service.Name) did not start in time!"
break
}
}

by nitramap at 2013-03-21 06:49:29

Thank You poshoholic —–
That Works whole lot better