DiscoPosse – Using the chicken to measure IT
Technology, Cycling, Music and Madness


Technology

June 18, 2012

Using the PowerShell Restart-Service CmdLet on Remote Servers

PowerShellI pride myself on being an lazy admin. The preferred term is efficient, but what I mean by “lazy” is that I like to do the least amount of typing to achieve the desired result. Another key about my technique is that while I can use Remote Desktop or the Computer Management MMC to manage servers, I prefer to do as much through the command line as possible.

PowerShell remoting has just what I need to handle my task today. I have to restart a Windows Service on my remote server with limited effort. In the past I would use the sc.exe Resource Kit utility to query and manage services on remote servers:

SC \\DC1 QUERY

SC command

The results can be pared down using find or the Win32 build of grep which I use to go after specific services. Once I know the service name I can run two commands to stop and start the service:

SC \\DC1 STOP MyServiceName

SC \\DC1 START MyServiceName

Since I’m moving my day-to-day processes to PowerShell and PowerCLI wherever possible, now I can leverage the Get-Service, Stop-Service, Start-Service and most importantly the Restart-Service CmdLets.

There are two main reasons for the switch. Firstly, it’s a direction shift to use the native supported language for all my Windows based devices. Secondly, if you use a PowerShell console window you see that using SC.EXE is no longer possible because SC is an alias for the Set-Content CmdLet.

Set-Content CmdLet

Where I used to have to run the stop and start commands individually, now I can use the Restart-Service PowerShell CmdLet which will stop, wait and start the service for me without having to be monitored. Even more exciting is that if I have a few services that are running, I can use the pipeline to run the Restart-Service against more than one service in a simple one-liner.

We are faced immediately with a limitation of the Restart-Service. While this CmdLet works great when you are logged in locally, there is no option to run it against a remote server. It is implied and required that we are running it in a local session on the device. But our goal was to eliminate the need to be remotely attached to the server via RDP so we need to add one more quick step.

The solution is to use PowerShell remote sessions with the Enter-PSSession CmdLet. First we will launch our local PowerShell command prompt (perhaps you want to use my multi-environment PowerShell console). From within that console we simply initiate a remote PowerShell session using this:

Enter-PSSession DC1

Enter-PSSession

Now you will see that we are able to run PowerShell CmdLets as if we were on the DC1 console directly. This allows us to use the Restart-Service CmdLet now against the desired services on DC1 directly. I want to restart any services which begin with dfs so I can use the wildcard asterisk (*) when sending the service name to the CmdLet.

Note that your command prompt shows the active server on the left which in this case it reads [dc1]:

Restart-Service

Make sure that for cleanliness that you also issue the Exit-PSSession command to exit and free up the remote PowerShell session to the server:

I hope that you can save some time and effort with technique. I know that I certainly do.

Here are the TechNet articles for the CmdLets we worked with:

Stop-Service : http://technet.microsoft.com/en-us/library/ee177005.aspx

Start-Service : http://technet.microsoft.com/en-us/library/ee177001.aspx

Restart-Service : http://technet.microsoft.com/en-us/library/ee176942.aspx

Enter-PSSession : http://technet.microsoft.com/en-us/library/hh849707.aspx

Exit-PSSession : http://technet.microsoft.com/en-us/library/hh849743.aspx

 

 



About the Author

Eric





 
 

 
featured_powershell

Updating (same as parent folder) records with DNSCMD and PowerShell

In an earlier post on the site (Microsoft DNS record updates using PowerShell and DNSCMD) I noted how PowerShell cannot natively update records in MS DNS, however we could leverage the DNSCMD command and pass parameters using a...
by Eric
1

 
 
featured_powershell

CSV, yeah you know me! – PowerShell and the Import-Csv CmdLet – Part 3

This was a long overdue post, so thanks for sticking with me while I finally got back on track with our CSV, yeah you know me series (Here are Part 1 and Part 2). As I’d mentioned in the closing of Part 2, we want to be a...
by Eric
0

 
 
featured_powershell

Finding RDP sessions on servers using PowerShell

Have you ever needed to use RDP to get to a server console for some local admin work and then been bounced out because there are already active sessions? Or have you had your Active Directory account locked out because of an op...
by Eric
8

 

 
featured_powershell

It’s all about Progress: Using the PowerShell Write-Progress CmdLet

If you are like me and you like to know how your task is going in a PowerShell process, this is a great little tip for you. I’ve got a number of long running scripts that perform actions against a collection or query. The...
by Eric
2

 
 
alias-logo

A.K.A. – Using PowerShell Aliases

An interesting capability baked into the PowerShell environment is alias commands. Much like we can find in IOS (The Cisco one that is) and with other CLI environments, there are short commands available to save you some typing...
by Eric
0

 

 
powershell-for-developers-cover

DiscoPosse Review: Windows PowerShell for Developers by Doug Finke – O’Reilly Media

For those fans of PowerShell, and for those just getting on-board, PowerShell MVP Doug Finke has built the bridge for your journey with his latest book Windows PowerShell for Developers from O’Reilly Media. As a long time...
by Eric
0

 



Join Zipcar and get $50 in free driving Join Zipcar and get $50 in free driving Join Zipcar and get $50 in free driving

4 Comments


  1. William

    I love using this with Invoke-Command instead of entering a PS Session. Much easier for quick, single tasks


  2. Very cool! Even better. I’ll put together a post on that one. It’s a great way to shorten the effort for these small tasks.


  3. Sean

    SC is available in powershell. Just use SC.exe instead of just SC.


  4. Excellent point. Thanks for the tip!



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>