Get-Service

rated by 0 users
This post has 7 Replies | 2 Followers

Top 200 Contributor
Posts 17
danarstevenson Posted: 05-11-2012 2:02 PM

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

Top 25 Contributor
Posts 296
Microsoft MVP
Top Contributor

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-Service
Set-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

Top 200 Contributor
Posts 17

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 W32Time

Get-Service -Name W32Time | Stop-Service -Verbose

Get-Service -Name W32Time

Write-Host Testing

Get-Service -Name W32Time

Get-Service -Name W32Time | Start-Service -Verbose

Get-Service -Name W32Time
Exit-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.

Top 200 Contributor
Posts 17

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 modules
Get-Module -ListAvailable | Import-Module

#-- Clear Screen
cls

#-- Declare Variables


#-- Start Script
Invoke-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

Top 25 Contributor
Posts 296
Microsoft MVP
Top Contributor

BITS is the name of a service as an example

You don't need to use invoke-command to stop remote services

Top 200 Contributor
Posts 17

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.

Top 10 Contributor
Posts 634
Microsoft MVP
Top Contributor

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.

 

Top 25 Contributor
Posts 296
Microsoft MVP
Top Contributor

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:

  • Set-service
  • WMI
  • PowerShell remoting via a session or Invoke-command
Page 1 of 1 (8 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.