PowerShell
Restart-Service Cmdlet

Windows PowerShell Restart-Service

The most efficient way for PowerShell to cure a hung service is with the verb 'restart'. Restart-Service xyz is just simpler and more efficient than stop-Service xyz, followed by start-Service xyz. If you are new to PowerShell Restart-Service, then I suggest that you begin with my Get-Service page.

Topics for PowerShell Restart-Service

The service which benefits most from Restart-Service is, "Spooler". The reason being the printer gives more trouble than any other piece of hardware, and often restarting the Spooler cures the problem. The inferior, but ruthless method of curing such printer jams is to reboot the computer. When the computer is also a fileserver, this technique is undesirable.

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems. Its also has the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

More importantly, why won’t the PowerShell’s service family interact with Group B?

The Answer: Some services have a ‘Display Name’ which differs from their ‘Service Name’, for example Telnet and Tlnsvr. How did I find this out? When I tried to start ‘Telnet’, or ‘Print Spooler’, nothing happened. Yet if I had a manual walk-through in the Service GUI, no problem. Then I ran Get-Service * and observed the two columns, Name and also Display Name. What threw me into confusion was Group A, where both names are the same.

Just to emphasise, if you wish to use the name ‘Print Spooler’, you need to specify the property -DisplayName.

In Windows Server 2003 days I choose the Alerter service for testing, partly because it’s relatively harmless service, and partly because its name is near the top of the list! However, since it’s been removed from Windows 7 and Server 2008 does I have chosen PLA (Performance Logs and Alerts) to test PowerShell’s service cmdlets.

Preliminary Script

Let us check the service’s status, and also let us ‘warm up’ with Get-Service before we employ other members of the service family.

Learning Points

Note 5: I have decided to introduce the variable $srvName to hold the value of the service. Hopefully this will emphasise the name of the service, and prompt you to change it as necessary for your project.

Note 6: Observe how I mix the ”literal phrases” with the $variables and properties to produce a meaningful output.

Guy Recommends: SolarWinds Free Wake-On-LAN Utility

Encouraging computers to sleep when they’re not in use is a great idea – until you are away from your desk and need a file on that remote sleeping machine!

WOL also has business uses for example, rousing machines so that they can have update patches applied. My real reason for recommending you download this free tool is because it’s so much fun sending those ‘Magic Packets’. Give WOL a try – it’s free.

Learning Points

Note 7: Observe how the speech marks are slightly different in this script compared with the previous: "$srvName is now " + $servicePrior.status compared with $srvName + " is now " + $servicePrior.status

Note 8: I prepared the above script to help you appreciate the factors needed to control a Windows Service. It also reflects my thinking process of how I learn about a command. On the other hand, for a production script you could take a much more ruthless approach and simplify the script thus:

Set-Service PLA -startuptype manualStart-Service PLA

Monitor Your Network with the Real-time Traffic Analyzer

The main reason to monitor your network is to check that your all your servers are available. If there is a network problem you want an interface to show the scope of the problem at a glance.

Even when all servers and routers are available, sooner or later you will be curious to know who, or what, is hogging your precious network’s bandwidth. A GUI showing the top 10 users makes interesting reading.

Another reason to monitor network traffic is to learn more about your server’s response times and the use of resources. To take the pain out of capturing frames and analysing the raw data, Guy recommends that you download a copy of the SolarWindsfree Real-time NetFlow Analyzer.

In real life, you may want a script which ensures that services such as: Telnet, Messenger and Routing and Remote Access are Stopped. Just for testing, you may need a script which reverses start-Service, just to be sure that it really is working as designed. Either way, here is a script which stops the service defined by $srvName.

Learning Points

Note 9: Observe how this script is the mirror image of the Start-Service script. It even disables the service once it has stopped. If you remember, when Example 1 wanted to start a service, it must first make sure the -startuptype is set to manual.

The '-Service' Family (Each member has a different verb)

Clear-HostGet-Command -Noun Service

Get-Service: Useful for listing the servicesSet-Service: Crucial parameter -startuptype Start-Service: The verb ‘start’ says it allStop-Service: Handy for scripts which prevent unwanted services running e.g. TelnetRestart-Service: A nice touch by the creator’s of PowerShell; this cmdlet removes the need to explicitly stop then start the service.

Guy Recommends: SolarWinds Engineer’s Toolset v10

This Engineer’s Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems. Guy says it helps me monitor what’s occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it’s like having free rein of a sweetshop. Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools. Try the SolarWinds Engineer’s Toolset now!

Surprise! Get-Service is amongst the cmdlets that support remoting, but stop, start and restart-Service are not on the list. More bad news, stop, start and restart-Service really don’t work on network machines. Thus you have to employ different techniques such as Get-WmiObject and InvokeMethod. Alternatively, you could enter-PSSession and then run restart-Service as if you were on the local machine. However, to get that working you have to first install and setup WinRM

If your mission is to master the start-Service command, commence with Get-Service. Once you have mastered the rhythm of the Get-Service Verb-Noun pair, move on to the Start, Stop, and restart-Service family of PowerShell commands. For scripting purposes, make sure that you use the true Service Name, and avoid the Service’s Display Name. A final piece of advice, open the Service GUI so that you can double-check that what your script is doing is what you intended.