Ezine 148 Start Windows Services with PowerShellWindows PowerShell Scripting - Get-ServiceOn the work front, nothing major happens in August. Even if you are not on holiday, everyone else who matters seems to be. September on the other hand, has a feeling of back to work, a time for new initiatives. In the case of this ezine, September is a great time to trumpet my goal for everyone to learn PowerShell. Let me start by saying that there are several reasons that I send this newsletter. Firstly, to keep in touch with those who buy my ebooks and visit my site, secondly, to send out free updates, and thirdly, to encourage you to learn PowerShell. If none of these apply do unsubscribe, or write to me and I will remove you from the mailing list. PowerShell's Start-Service Topics
This Week's SecretFollowing on from my theme of back to work, my not so secret mission is to persuade anyone who uses a computer regularly to experiment with Windows PowerShell. Perhaps you already know DOS? There may have been times when this command line language has saved the day. PowerShell literally does everything that DOS does, but caters for that moment when you wished DOS had more wide-ranging capabilities. I really am not trying to sell you anything, it's just that I have this vision of PowerShell being the smart way to configure or interrogate computers, and I want you to keep one step ahead of those dinosaurs who stick with DOS commands. Also, perhaps you have always wanted to learn a scripting language such as VBScript or C++. If so then why not try PowerShell, it is easy to get started, challenging for intermediates, incredibly productive for experts. Our MissionOur mission is to start one (or more) of your operating system's services. We can also adapt the script to stop services, but that is less exciting. It is worth mentioning that another member of this family is called restart-Service. Incidentally, this mission follows on from the previous ezine where we just listed the services. The result of my preliminary experiment reveals that it's not possible to start a service whose start-up type is 'Disabled'. Good news, a walk-through with the Services GUI reveals that if you switch a service from Disabled to Manual, then you can start it. One extra thing, you need faith in the scripters' maxim: 'Any thing that you can do by clicking in a GUI, you can equal (or exceed) in a PowerShell script'. The Service Family (Each member has a different verb)get-Service: Useful for listing
the services Research for yourself with this one-liner Example 1: How to Start a Windows Service (Alerter)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!
Instructions:
Preliminary ScriptLet us check the service's status, and also let us 'warm up' with get-Service before we employ other members of the service family. # PowerShell cmdlet to check a service's status Learning PointsNote 1: I have decided to introduce the variable srvName to hold the value of the service. Note 2: Observe how I mix the ''literal phrases'' with the $variables and properties to produce a meaningful output. The Main Event - Starting Alerter# PowerShell cmdlet to start a named service Learning PointsNote 1: I prepared this script to help you appreciate the factors in controlling 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 Alerter -startuptype manual Note 2: Observe how the speech marks are slightly different in this script: My points are: a) Experiment yourself. b) To some extent, my scripts leave traces of my thinking process. If you are looking for handy network utilities, try some of the free downloads at Tools4Ever Example 2: How to Stop a Service (Alerter)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. # PowerShell cmdlet to stop the Alerter service Learning PointsNote 1: 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, first, it must make sure the -startuptype is set to manual. In Order to Explain a Trap - I Digress:What's in a Name? What do these groups of services have in common? Group A Alerter, Messenger, WebClient Group B Print Spooler, Telnet, Telephony and Windows Time More importantly, why won't 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 Display Name. What threw me into confusion was Group A, where both names are the same. Just to emphasise, if you wish to control 'Print Spooler', you need to script the Name 'Spooler'. If you double-check with the command: get-Service s* you see Name: Spooler, Display Name: 'Print Spooler'. ˆ Example 3: How to Restart a Service (Spooler)A classic service to practice the Restart-Service command is, "Spooler". One reason for choosing this particular service is that the printer gives more trouble than any other piece of hardware, and sometimes restarting the Spooler cures the problem. The inferior, but ruthless method of curing such printer jams is to reboot the computer. However, if the computer is also a server, this method is undesirable. The real life situation of a jammed printer spooler is not straightforward. The point is that it APPEARS to be running, but in fact it's not working. The smartest solution is to restart the service. As with previous examples, when you are learning, open the services.msc GUI and experiment with the settings. What you will discover is that you can also restart a service that has stopped. Production ScriptAll you really need is a one-liner: restart-Service "Spooler" Learning Script# PowerShell cmdlet to restart the Spooler service Learning PointsNote 1: My biggest fear is that in a production script I will misspell the name of the service. Thus, check for success by observing this system message: WARNING: Waiting for service 'Print Spooler (Spooler)' to finish starting... Summary of PowerShell's Start-ServiceIf your mission is to master the Start-Service command, commence with Get-Service. Once you have mastered the rhythm of the get-Service command, 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. See more Microsoft PowerShell tutorials• Windows PowerShell Home • Introduction • Cmdlets • Exchange 2007 • Profile.ps1 • $_.Pipeline If you see an error of any kind, do let me know. Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit. ^
| |||||