Ok, I’m stuck. I am trying to check if a service is running on a remote machine, and my script is able to tell if the service is running on the remote machine or if it is stopped. But it cannot start the service. Below is my code:

#--- VARIABLES ---#
#region variables
$ServiceName = 'Service Name'
$name = "WORKSTATION1"
#end region variables

#--- SCRIPT ---#
#region script

$arrService = Get-Service -ComputerName $name -Name $ServiceName

while ($arrService.Status -ne 'Running')
{

    Start-Service $ServiceName
    write-host $arrService.status
    write-host 'Service starting'
    Start-Sleep -seconds 30
    $arrService.Refresh()
    if ($arrService.Status -eq 'Running')
    {
        scriptres:"Ok,Running"
    }

}

But PS just loops and loops, and the service on the remote machine does not even attempt to restart. I can restart the service on the remote machine thru Services.msc. And I have tried both the Service Name and Display Name. And the Description of the Service is “Handle .NET Remoting Objects.” I have a PS script that I converted to an executable so that users can restart this service on their own, but it has to be run manually. I would like to be able to start this service if it is stopped without any user interaction, but I do not want to restart the service if its Running. Any help is greatly appreciated!

7 Spice ups

Run it in verbose mode or do a transcript while running to see what happens. Your while loop will off course make the script look like it’s hanging. The start fails and the while detects the svc isn’t started so it keeps trying until pigs can fly or you stop it.
Try an if then and not a perpetuous loop to debug and then later if it works you can think of looping it again which I don’t think is a good idea.

Permission issues maybe? I tested it with the w32time service as a test and you do need to run it from an elevated powershell session (run as Administrator).

When your script runs do you get any output? You have a few write-host statements that should produce some output (consider using write-output which is usually more correct to use - https://www.madwithpowershell.com/2015/11/write-host-is-not-as-bad-in-powershell-5.html )

1 Spice up

Have you tried using something simpler like PS Tools/Sysinternals psservice tool to start/stop/query status etc?? I pair that up with powershell scripts and it makes my life a LOT easier

PSService

I would suggest getting all the PS Tools suite to help with everything else too.

@briangorton5132

may be dependencies? try running locally and see the error or add

Start-Service $ServiceName -force

Problem is here:

$arrService = Get-Service -ComputerName $name -Name $ServiceName

while ($arrService.Status -ne 'Running')
{

    Start-Service $ServiceName

So you got the remote service in $arrService (why are you calling it an array??)–also, we don’t use the Hungarian notation in PowerShell, but you can if you want to.

Then, when you “Start-Service” you aren’t remoting, you’re just starting the service on the local machine. Since that won’t affect $arrService you never break out of the loop. Interestingly, Start-Service doesn’t have a -ComputerName parameter! So remoting directly from it is impossible. But we can still do it thanks to the pipeline.

$arrService | Start-Service

Since the remote information is in the service object, Start-Service will consume that and start the remote service up. Give it a shot.

4 Spice ups

First, can you do this

Invoke-Command -Computer <server>  -Scriptblock {Start Service <service in question>} -Credential (get-Credential server\administrator)

The *-Service commands use DCOM/RPC whereas ICM uses WinRM. That could explain the issue.

Note that some services start, find there’s nothing to do, and then closes itself down.

Which specific service is not starting?