Newbie – StopStart Service with IfElse

by Aug 10, 2017

Hi All,

 

Wondering if someone could maybe help me out with the below. I am new to powershell and trying to play around with it a little bit.

 

Basically, the below script checks for the status of the BITS service on a PC, once it has the info, it will then ask if you want to start or stop depending on the original state of the service. 

I think i may be muddling up my statements somewhere but I think it jumps to the wrong ifelse.

( I know there is most probably a better way to write this however, just trying to get it to work first) 🙂

 

Thanks All
ps: forgive the formatting 

 

function ServiceBitsAutomate3
{
 $RunningSericeBits = Read-Host "enter pc name to check BITS service Status"

  foreach($serverName in $RunningSericeBits) {
   $result = Get-Service -Name BITS
      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 Servie"

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