Hello,
Trying to stop a service via the Get-Service cmdlet to a remote server.
Both 2008R2
Logged in as Domain Admin
Get-Service -ComputerName NAME -Name "IBM Cognos" | Stop-service -Verbose
I get this
WARNING: Waiting for service 'IBM Cognos (IBM Cognos)' to finish stopping...
But it never stops
If I run this locally it works great but heres the whole story <quick one>
I have two servers each with a single service that must come down in a specific order and then come back up in a specific order.
I was hoping with using the command listed above that it would wait for the local service to quit then send the command the second server to stop the service. Instead of using a wait command in a batch file since if the service doesn't stop in the time allotted it can corrupt data.
Any ways thanks for the help in advance.
Dana
Stop-Service does not work on a remote computer.
The only service cmdlets that work remotely are get- and set-
PS> Get-Help *service -Parameter computername | select name
Name----Get-ServiceSet-Service
if you want to stop a service on a remote computer
Get-Service BITS -ComputerName myserver | Set-Service -Status "Stopped"
see examples 5 & 6 in
get-help set-service -full
Thanks for the quick reply, I will have to check out the BITS option
Here is what I was toying with to see if this would work
Enter-PSSession <server>Get-Service -Name W32TimeGet-Service -Name W32Time | Stop-Service -VerboseGet-Service -Name W32TimeWrite-Host TestingGet-Service -Name W32TimeGet-Service -Name W32Time | Start-Service -VerboseGet-Service -Name W32TimeExit-PSSession
There is a limitation on the sessions one can enter in to , 5 . I changed that to 50 and didnt have an error with open sessions. Other then that it seems to work.
If anyone else has any ideas please post. I am trying to keep this all in side PowerShell also not calling psexec or something like that.
This is the final version of the script. The invoke command worked great and I think I will be using this more in the future.
#-- The purpose of this script is to restart services in a special sequence#-- Load modulesGet-Module -ListAvailable | Import-Module#-- Clear Screencls#-- Declare Variables#-- Start ScriptInvoke-Command -ComputerName <server name> -ScriptBlock { Get-Service -Name "service" | Stop-Service -Verbose}I may not need the verbose in there since I will be calling this from a script but for testing it was nice to see messages
BITS is the name of a service as an example
You don't need to use invoke-command to stop remote services
Hmmm It didnt seem to work for me like that. Yep I understood BITS being the service name. From what I was reading that the Get-Service will not stop services on remote servers. Which is odd since the command is in cmdlet for it.
The Get-Service is much cleaner way I would think but didnt seem to work for me.
Any thoughts would be great.
You can stop services with Stop-Service and also with Set-Service. Set-Service can work locally and remotely. Stop-Service can only work locally.
So you should play with this:
set-service -Name Spooler -Status 'Stopped' -ComputerName targetcomputer
The only disadvantage in comparison to Stop-Service is that Set-Service is missing a -Force switch, so if a service has dependant services it will throw an error and will not stop. With Stop-Service, you can use -Force to stop the service regardless of dependant services.
Get-Service will not stop services at all. Thats not its job. The verb is Get which means it gets information.
As I said at the start of my original reply
"Stop-Service does not work on a remote computer."
If you want to stop a service on a remote machine you have a number of options: