Quite often, I will find a computer where the service for one of our screen viewing software is having troubles. I have a batch file to fix it, but just trying to speed the whole process up as noticed Powershell accomplishes this faster.

I first tried stopping the service and then restarting it. But I know sometimes the service has just stopped, so all it needs is to be started. After a quick bit of research I found a script online that checks to see if a specific service is running or not, if it isn’t it’ll start it and if it is running already it displays a message on screen saying it was already running.

I need to be able to restart the service if it’s running, but just start it if it’s not running. I altered the script slightly and got this;

$svc = get-service -name icas -computername 70-01
if ($svc.Status -eq "Stopped") {$svc.start()} {Write-Host "Service was stopped but is now starting"}
elseif ($svc.status -eq "Running") {$svc.stop()} {$svc.start()} {Write-Host "Service restarted"}

But the problem is, I’m getting this error;

elseif : The term 'elseif' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:3 char:1
+ elseif ($svc.status -eq "Running") {$svc.stop()} {$svc.start()} {Writ ...
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (elseif:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

After some more research online, most of the time this error is apparently due to an incorrect/missing bracket, but everything looks fine to me.

3 Spice ups

No, it looks definitely weird.

write it out , no compact as you did, then you see they are weird, I assume they are supposed to be all in the same loop?

Also why did you use elseif and not just else if you just have 2 conditions?

$svc = get-service -name icas -computername '70-01'

if ($svc.Status -eq "Stopped") {
    $svc.start()
} 

{Write-Host "Service was stopped but is now starting"}

elseif ($svc.status -eq "Running") {
    $svc.stop()
} 

{$svc.start()} {Write-Host "Service restarted"}
1 Spice up

Change this:

$svc = get-service -name icas -computername 70-01 
if ($svc.Status -eq "Stopped") {$svc.start()} {Write-Host "Service was stopped but is now starting"} 
elseif ($svc.status -eq "Running") {$svc.stop()} {$svc.start()} {Write-Host "Service restarted"} 

to this:

$svc = get-service -name icas -computername 70-01 
if ($svc.Status -eq "Stopped") {$svc.start(); Write-Host "Service was stopped but is now starting"} 
elseif ($svc.status -eq "Running") {$svc.stop(); $svc.start(); Write-Host "Service restarted"}
2 Spice ups

It should be like this:

$svc = get-service -name icas -computername '70-01'

if ($svc.Status -eq "Stopped") {
    $svc.start()
    Write-Host "Service was stopped but is now starting"
} 
elseif ($svc.status -eq "Running") {
    $svc.stop()
    Start-sleep -seconds 5 # idk just giving it more time, not needed I guess
    $svc.start()
    Write-Host "Service restarted"
}
2 Spice ups

I see too many curly brackets

$svc = get-service -name icas -computername 70-01 |select Name,displayname,status
if($svc.Status -eq "Stopped") 
{
Start-Service $svc.name
 Write-Host "Service was stopped but is now starting" -ForegroundColor Green
}
ElseIf($svc.status -eq "Running") {
Restart-Service $svc.name -Force -Verbose
Write-Host "Service restarted"
}

4 Spice ups

Thanks, but I’m getting this error message;

Restart-Service : Service 'iTALC Client (icas)' cannot be stopped due to the following error: Cannot open icas service on computer '.'.
At C:\Users\whettonc\Desktop\Untitled1.ps1:8 char:1
+ Restart-Service $svc.name -Force -Verbose
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (System.ServiceProcess.ServiceController:ServiceController) [Restart-Service], ServiceCommandException
    + FullyQualifiedErrorId : CouldNotStopService,Microsoft.PowerShell.Commands.RestartServiceCommand

Results with the service being stopped but not being restarted.

Exception calling "Start" with "0" argument(s): "Cannot start service icas on computer '70-01'."
At line:10 char:5
+     $svc.start()
+     ~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException

Getting a similar error message as above and the service is also being stopped but not started again.

Exception calling "Start" with "0" argument(s): "Cannot start service icas on computer '70-01'."
At line:3 char:50
+ elseif ($svc.status -eq "Running") {$svc.stop(); $svc.start(); Write- ...
+                                                  ~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException

Are you running as administrator as administrator if service is not stopping may be it has other dependencies

try changing this line

Restart-Service $svc.name -Force -Verbose

to

Stop-Service $svc.name -Force -Verbose -PassThru
Start-Sleep -Seconds 3
Start-Service $svc.name 
1 Spice up

The script I edited was using elseif for just 2 conditions. Not knowing any different, I just left it with how the script originally was.

Stop-Service : Service 'iTALC Client (icas)' cannot be stopped due to the following error: Cannot open icas service on computer '.'.
At C:\Users\whettonc\Desktop\Untitled1.ps1:8 char:1
+ Stop-Service $svc.name -Force -Verbose -PassThru
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (System.ServiceProcess.ServiceController:ServiceController) [Stop-Service], ServiceCommandException
    + FullyQualifiedErrorId : CouldNotStopService,Microsoft.PowerShell.Commands.StopServiceCommand
 
Start-Service : Service 'iTALC Client (icas)' cannot be started due to the following error: Cannot open icas service on computer '.'.
At C:\Users\whettonc\Desktop\Untitled1.ps1:10 char:1
+ Start-Service $svc.name
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service], ServiceCommandException
    + FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand

Above is the error I’m getting when not running as administrator. When running as administrator I’m getting no error message, but nothing actually happens to the service.

That sounds like a permission issue, does your account have rights on the remote computer?

Maybe try invoke-command

Do have the permissions on the computer. Will try invoke-command

Tried the very basic way of doing this via PowerShell, which is how I was originally doing it, and this does work.

Get-Service -Name icas -ComputerName 70-01 | Set-Service -Status stopped
Get-Service -Name icas -ComputerName 70-01 | Set-Service -Status running

Try to add in write-host screws the whole thing and it doesn’t work.

Tried Neally’s script again without write-host and it worked the first couple of times that I tested it, and then it went back to just stopping the service (when running again it worked to start the service). When I came back to this a few minutes later, it worked again - perhaps I was trying to do it too often.

Anyone any clue why write-host screws everything up?

It should not, as it just draws pixels on your local console.

What if you give it more time between the stop and start, and instead of a restart you stop and start it.

stop-service $service -verbose
start-sleep -seconds 10
start-service $service -verbose

I’ve also seen services that are having issues stopping because they are stuck and you have to use the ‘-force’ switch or even kill the process rather. ¯_(ツ)_/¯

1 Spice up

Been really busy these last few days so have not had a chance to reply sooner.

This is the script I am currently using:

$host.ui.RawUI.WindowTitle = "Restarting the iTALC Service on $(Computer:TARGET)"
$svc = get-service -name icas -computername '$(Computer:TARGET)'

if ($svc.Status -eq "Stopped") {
    $svc.start() 
    Write-Host "Serviced started"
} 
elseif ($svc.status -eq "Running") {
    $svc.stop()
    Write-Host "Service Stopped"
    Start-sleep -seconds 3
    $svc.start()
    Write-Host "Service Restarted"
}

The $(Computer:TARGET) is a variable in PDQ Inventory (which is where I’m running the script from).

The script works most of the time, but when it doesn’t work, I get the error saying it can’t start the service after stopping it.I’ve tried running it as administrator, but it doesn’t seem to change the result.

I just ran the script on 4 PCs that were having trouble, and it failed to start the service on all of them after stopping. I’d never run the script before on these PCs.

If the service is stopped before running the script, the script will successfully start the service 100% of the time.

Really scratching my head over here.

Did you verify it stopped?

Some services take a while to stop, and if you try starting it in between, it will not be able to start it if it is still stopping

Maybe put a loop in that waits for the service to stop for sure?

$host.ui.RawUI.WindowTitle = "Restarting the iTALC Service on $(Computer:TARGET)"
$svc = get-service -name icas -computername '$(Computer:TARGET)'

if ($svc.Status -eq "Stopped") {
    Start-Service -Name icas -Verbose -Comp
    Write-Host "Serviced started"
} 
elseif ($svc.status -eq "Running") {
    get-service -name icas -computername '$(Computer:TARGET)' | stop-Service -Force
    Write-Host "Service Stopped"
    do {
        $svc = get-service -name icas -computername '$(Computer:TARGET)'
        Start-sleep -seconds 5
    }until($svc.status -eq 'stopped')
    get-service -name icas -computername '$(Computer:TARGET)' | Start-Service -Verbose
    Write-Host "Service Restarted"
}
1 Spice up

I must admit, I never did confirm that the service had actually stopped.

I will try this and hopefully, all will be good!