Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Issue with remotely restarting services

$
0
0

Hi Complete N00b here.  Hoping someone can offer some insight into this predicament.

I want to create a script that will gather server and service names from text files in order to stop or start services locally and remotely.  What is tripping me up is that in the server text file only the first server will get the restart message.  I have tried switching the order (there are only 2 servers), the script works only on the first server.  The text files are just plain lists.  For example the ServersApp.txt file reads like this:
Server01
Server02

The ServicesAPP.txt file is the same format:
Service01
Service02
Service03

Here is my code:
$servicesA = Get-Content D:\EASTools\PowerShell\Scripts\ServicesAPP.txt
$serversA = Get-Content D:\EASTools\PowerShell\Scripts\ServersAPP.txt

get-service $servicesA -computername $serversA | Set-Service -status stopped

Write-host "Action completed"

I get the same message repeated 4 times.  I can understand seeing it one time because the second server is missing one of the services in the list, but all of the other services exist in various states.  Keep in mind, if I make the second server name the first item in the list, the services will be stopped.

Get-Service : Cannot find any service with service name ''.
At D:\EASTools\Powershell\Scripts\StopSvcs.ps1:5 char:12
+ get-service <<<<  $servicesA -computername $serversA | Set-Service -status stopped
    + CategoryInfo          : ObjectNotFound: (:String) [Get-Service], ServiceCommandException
    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand




Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>