Hi all,
Wondering if someone may be able to point out where i am going wrong. I think the script below is ignoring the one else statement.
I know there may be better ways to write this however, just trying to figure out how to get it working my own way first.
function ServiceBitsAutomate3
{
$RunningServiceBits = Read-Host "enter pc name to check BITS service Status"
foreach($serverName in $RunningServiceBits) {
$result = Get-Service -Name BITS -computername $serverName
if($result.Status -eq "running") {
Write-warning "BITS IS RUNNING"
$StopServiceOption = Read-Host "Would you like to STOP the service"
if($StopServiceOption -eq "yes" -or "y") {
$result.Stop()
Write-Warning "Service BITS has been stopped"
} else {
Write-Warning "you have chosen NOT to start the service...exiting"
break
}
} else {
Write-Warning "BITS IS NOT RUNNING"
$WouldyouLikeTOStart = Read-Host "Would you like to START the Service"
if($WouldyouLikeTOStart -eq "Yes" -or "y")
{
$result.Start()
Write-Warning "Service Bits Has been STARTED"
} else {
Write-Warning "you have chosen NOT to start the service...EXITING"
break
}
}
}
}
If the service is running and asks if you would like to stop it, if you say “no” it still seems to stop the service and output “service has been stopped”
PS E:> ServiceBitsAutomate3
enter pc name to check BITS service Status: pc name here
WARNING: BITS IS RUNNING
Would you like to STOP the service: n
WARNING: Service BITS has been stopped