Logic fault

Hi Folks, I think there is a logic fault here, but not sure how to correct it. I want to write this script that checks a list of servers for the status of a specific service, and if it is stopped to then start it.
I started with this

$servers = import-csv c:\folder\servers.csv
$service=Get-Service -Name 'PCNSSVC'
Foreach ($server in $servers)
{
		while ($Service.Status -ne 'Running')
	{
		Start-Service $Service
		write-host $service.status
		write-host 'Service starting'
		Start-Sleep -seconds 10
		$Service.Refresh()
		if ($Service.Status -eq 'Running')
		{
        Write-Host 'Service is now Running'
		}

	}
}

I think however that the first service variable ($service=Get-Service -Name ‘PCNSSVC’) only returns the service of the server that i am executing the script on, and not checking for the specific service on each server in the servers list.

I then move that line to inside the foreach loop but before the while loop

$servers = import-csv c:\milo\servers.csv
#$service=get-service 'PCNSSVC'-ComputerName $servers
Foreach ($server in $servers)
{
		$service = Get-Service -Name PCNSSVC -ComputerName $servers
        while ($Service.Status -ne 'Running')
	{
		Start-Service $Service
		write-host $service.status
		write-host 'Service starting'
		Start-Sleep -seconds 10
		$Service.Refresh()
		if ($Service.Status -eq 'Running')
		{
        Write-Host 'Service is now Running'
		}

	}
}

but that returns that it cannot find the servicename.

SO my question is

Help me with the logic, how do I run the get-service against each computer in the list, and then start the service if it is stopped.
your help will be greatly appreciated.

Use server not servers with your ‘ComputerName’ parameter.

$servers = import-csv c:\milo\servers.csv
Foreach ($server in $servers)
{
	$service = Get-Service -Name PCNSSVC -ComputerName $server
    If ($service.Status -ne 'Running') {
        Start-Service $service ; "Starting $($service.Name)"} Else {
        "$($service.Name) is running"}    
}

Good morning,
One minor thing that i noticed is for computername you have $servers, that should be $server as you want the server you are currently on in the array.

Another item is you are attempting to start the service, however start-service much like you found earlier with get-service is executing on your local machine. There is no -computername param in start service.

You could do another get-service with computername and pipe that to start-service as i did below.
Or you could also loop through the array and use use invoke-commmand to check the status of the service and just start it that way.

Hope that helps.

$servers = import-csv c:\milo\servers.csv
#$service=get-service 'PCNSSVC'-ComputerName $servers
Foreach ($server in $servers)
{
		$service = Get-Service -Name PCNSSVC -ComputerName $server
        while ($Service.Status -ne 'Running')
	{
		Get-Service -Name PCNSSVC -ComputerName $server | Start-Service $Service
		write-host $service.status
		write-host 'Service starting'
		Start-Sleep -seconds 10
		$Service.Refresh()
		if ($Service.Status -eq 'Running')
		{
        Write-Host 'Service is now Running'
		}

	}
}

As far as I know does the cmdlet Start-Service NOT work remotely. You will have to use Invoke-Command with the -ComputerName parameter to start or restart a service remotely.

Along the lines of Jason’s suggestion of using invoke-command, try something like this.

$servers = import-csv c:\milo\servers.csv
#$service=get-service 'PCNSSVC'-ComputerName $servers

Foreach ($server in $servers)
{
    $service = Get-Service -Name PCNSSVC -ComputerName $server

        if ($Service.Status -ne 'Running')
        {
            $servername = $service.MachineName
            $servicename = $service.Name
            $result = Invoke-Command -ComputerName $servername -ScriptBlock {Start-Service -Include $servicename} -ArgumentList $servicename 
        }
        
            if ($result -match "Service Started")
            {
                Write-Output 'Service is now Running'
            }

}

Just as an Fyi yes Start/Stop-service will work on a remote system, but only if get-service -name xxx -computername blah | start-service
is used.

... Start/Stop-service will work on a remote system, but only if ....
I just tested it right now and it works. I wasn't aware of that. Thank you.

Thank you, everyone, for the valuable input. Taking all I’ve learned I have modified the code as follows: (still very rudimentary but it does what I required). (and color coded)

$servers = import-csv c:\folder\servers.csv
Foreach ($server in $servers)
{
		$service = invoke-command -ComputerName $server.servername {Get-Service -Name 'PCNSSVC'}
        if ($Service.Status -ne 'Running')
    {
        write-host $server.Servername', ' -ForegroundColor White -NoNewLine; Write-Host $service.ServiceName', ' -ForegroundColor White -NoNewLine; Write-Host $service.status -ForegroundColor Red
        write-host $server.Servername', ' -ForegroundColor White -NoNewLine; Write-Host $service.ServiceName', ' -ForegroundColor White -NoNewLine; Write-Host 'Service Starting' -ForegroundColor Magenta
        Invoke-command -ComputerName $server.Servername {Get-Service -Name 'PCNSSVC'| Start-Service}
        Start-Sleep -Seconds 5
        #status
        $service = invoke-command -ComputerName $server.servername {Get-Service -Name 'PCNSSVC'}
        write-host $server.Servername', ' -ForegroundColor White -NoNewLine; Write-Host $service.ServiceName', ' -ForegroundColor White -NoNewLine; Write-Host $service.status -ForegroundColor Green
    }
        else
        {
        write-host $server.Servername', ' -ForegroundColor White -NoNewLine; Write-Host $service.ServiceName', ' -ForegroundColor White -NoNewLine; Write-Host $service.status -ForegroundColor Green        
        }
}