Beginner - Check Service IF\ELSE

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

It’s not working because you need two separate comparisons, like the following, instead of one. The same thing goes for your other IF statement.

Use This:

if(($StopServiceOption -eq “yes”) -or ($StopServiceOption -eq “y”))

Instead Of:

if($StopServiceOption -eq “yes” -or “y”)

I had a few typos in my previous post, so I corrected them.

Hi Kevyn,

Thank you for your feedback. I changed the statement to rather use “-match” which seems to have resolved this issue.

Just trying to work out now how one could put a “test” in ie, when typing in the computer name, it will fist try ping it to see if it exists or is alive and then proceed, if fails, asks to re-enter the name in case it was a typo or to quite.

I know it may be along the lines of Test-Connectivity but not really sure how to incorporate it.

 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 `n"
                   $StopServiceOption = Read-Host "Would you like to STOP the service" 
          
                        if($StopServiceOption -match"(yes|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 `n " 
             $WouldyouLikeTOStart = Read-Host "Would you like to START the Service" 

                if($WouldyouLikeTOStart -match"(yes|y)") 
                   {
                   $result.Start()
                   Write-Warning "Service Bits Has been STARTED"
                   } else {
                   Write-Warning "you have chosen NOT to start the service...EXITING"
                   break
                   }

             }
        }
}

Think about using parameters rather than read-host

Hi Richard,

Not quite sure what you mean? I am new to all of this.

Thank you

Rob what Richard means is that in your code instead of doing:

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

do something like the following instead

 function ServiceBitsAutomate3 
 
{
 
   param
	(
		[Parameter(Mandatory = $true)]
		[string]$RunningServiceBits
	)
    

Once you modify the function like this - you can then call the function like below

ServiceBitsAutomate3 -RunningServiceBits "localhost"

Doing it this way allows you to use the function in applications outside of interactive shells - such as if you wanted to run it in a scheduled task, etc.