Ezine 176 - Discover PowerShell's Service Family
Ezine 176 - Discover PowerShell's Service Family
People need sound reasons to learn PowerShell. In the case of
services, I can give them two; a simple PowerShell script can easily
identify which services are running on your computer. Furthermore, a slightly more
complicated script could restart a service automatically, thus saving
frustration and down-time.
Topics for PowerShell Service
♣
If you employ PowerShell to report and configure your operating system's
services, then you will be astonished by the sheer number of services, and
surprised by which services are started.
Scripting services will reveal two more small secrets. Firstly, the Display Name often differs from
the ServiceName (or plain 'name'). Secondly, I was shocked that
Windows Server 2008 does not have an Alerter service. Not that I ever
use it in a production server, however, Alerter was my favourite for
practicing stopping and restarting a harmless service. On reflection
removing the Alerter and the Messenger service make sense, it just means my
old scripts won't work on Windows Server 2008 without substituting a service
such as 'Spooler'
for 'Alerter'.
This Week's Mission is divided into four tasks.
- Get to know the PowerShell 'Service' family.
- Investigate the properties of get-Service
- List the Services actually installed on your machine
- Restart the Spooler Service
This week's main mission is to restart a named
service. Such scripts are handy to heal wounded machines
automatically. They are also a reminder that in the case of servers,
look to restart services rather than reboot the machine.
When you are a PowerShell beginner, the only verbs you use are 'get' and
'help'. The Services object introduces the more exciting verbs,
stop, start and restart. Moreover this variety helps to emphasise
the verb-noun structure of the PowerShell cmdlet.
Instructions:
Pre-requisite: Visit Microsoft's site and download the correct version of PowerShell for your operating system.
- Launch PowerShell
- Copy the six lines of code below (into memory)
- Right-click on the PowerShell symbol
- Edit --> Paste
- Press enter to execute the code.
Example 1 get-Command *service
Let us discover the members of the PowerShell 'Service' family.
clear-Host get-Command *service
Learning Points
Note 1: This script reminds us of the verbs that we
can apply to the 'Service' noun.
Note 2: The most interesting members of this
family are
start-Service, stop-Service and restart-Service.
Calculating IP Address
ranges is a black art, which many network managers solve by creating custom
Excel spreadsheets. IPAT cracks this problem of allocating IP addresses
in networks in two ways:
For Mr Organized there is a nifty subnet
calculator, you enter the network address and the subnet mask, then IPAT
works out the usable addresses and their ranges.
For Mr Lazy IPAT
discovers and then displays the IP addresses of existing computers.
Download the Free IP Address Tracker
Example 2: To List the Properties for get-Service
Let us list just the properties for get-Service (and not the methods).
clear-Host get-Service | get-Member -MemberType property
Learning Points
Note 1: Once you have identified interesting
properties, you can define which columns you wish get-Service to
display.
Example 3: List Services on Your Machine
This script will list all names of services installed on your computer.
It will also display the corresponding display name, and will reveal whether the status
of each service is running or
stopped.
# PowerShell script to list services # Author: Guy Thomas
# Version 1.0 November 2008 tested on PowerShell v 1.0
clear-Host
$Srv = get-Service | sort-object -descending Status $Srv | Format-Table ServiceName,
DisplayName, Status -auto $Srv.count
Learning Points
Note 1: Observe how employing the variable $Srv means we can
apply the .count method. Incidentally, I was shocked that there were
so many services on my operating system.
Note 2: However, if you seek simplicity, try the basic
command: get-Service
Challenge 1: You could amend the script to filter for
running services; just substitute this line with its where clause: $Srv = get-Service | where
{$_.status -eq "Running"}
Challenge 2: You could amend sort-Object to sequence
by ServiceName, or DisplayName. If you accept this challenge you could
delete or change -descending.
If you are looking for handy network utilities, try some of the free downloads at
Tools4Ever
One feature that surprised me was that we need to focus on the ServiceName
("Spooler") and not use the DisplayName ("Print Spooler").
# PowerShell script to restart a named service # Author: Guy Thomas
# Version 2.2 November 2008 tested on PowerShell v 1.0
Clear-Host $SrvName = "Spooler" restart-Service $SrvName
$ServiceAfter = get-service $SrvName "$SrvName is now " + $ServiceAfter.status
Learning Points
Note 1: We could simplify this basic script to one line:
restart-Service Spooler
Note 2: Alternatively, we could complicate the script
by introducing a command to stop the spooler service. I would only do
this in testing just to make sure the
script is indeed working as designed.
Clear-Host # PowerShell cmdlet to restart the Spooler service
# Phase one stop the service
$SrvName = "Spooler" $ServicePrior = get-service $SrvName
"$SrvName at beginning is " + $ServicePrior.status stop-Service $SrvName
# Phase two restart
$ServiceMiddle = get-service $SrvName "$SrvName in middle is " + $ServiceMiddle.status
set-Service $SrvName -startuptype
manual restart-Service $SrvName $ServiceAfter = get-service $SrvName
"$SrvName is now " + $ServiceAfter.status
Note 1: I admit this is an unnecessarily tortuous
script. The reason I left it in this scruffy state is to encourage you to
learn by playing. Thus I challenge you to improve on my offering.
One strategy is to comment-out lines (with #) and see what happens.
You could move on to amending commands and even adding your own. My
mission is complete when I become redundant!
Calculating IP Address
ranges is a black art, which many network managers solve by creating custom
Excel spreadsheets. IPAT cracks this problem of allocating IP addresses
in networks in two ways:
For Mr Organized there is a nifty subnet
calculator, you enter the network address and the subnet mask, then IPAT
works out the usable addresses and their ranges.
For Mr Lazy IPAT
discovers and then displays the IP addresses of existing computers.
Download the Free IP Address Tracker
Calculating IP Address
ranges is a black art, which many network managers solve by creating custom
Excel spreadsheets. IPAT cracks this problem of allocating IP addresses
in networks in two ways:
For Mr Organized there is a nifty subnet
calculator, you enter the network address and the subnet mask, then IPAT
works out the usable addresses and their ranges.
For Mr Lazy IPAT
discovers and then displays the IP addresses of existing computers.
Download the Free IP Address Tracker
Summary: PowerShell Scripts to Control Services
The operating system's services provide a rich vein to learn PowerShell while
you create scripts that can restart failed services.
This week's examples emphasis how learning PowerShell helps you to
understand your operating system. The benefit is that it becomes
easier to make sure the computer runs smoothly. For example,
researching PowerShell's service command prompts you to check which services
should be running, and which should be stopped or even disabled.
Furthermore, this research may persuade you look for general solutions which restart
services rather than rebooting the server with the consequent down-time for users.
If you like this page then please share it with your friends
See more PowerShell examples of process and service
• PowerShell Home •
Get-Process
• Stop-Process •
Get-WmiObject win32_service
•
Get-Service •
Start-Service •
Stop-Service •
Restart-Service •
Set-Service
• PowerShell Start-Sleep •
Windows
PowerShell
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.
Download my ebook: Getting Started with PowerShell - only $9.25
You get 36 topics organized into these 3 sections: 1) Getting Started 2) Real-life tasks 3) Examples of Syntax.
In addition to the ebook, you get a PDF version of this Introduction to PowerShell ebook It runs to 120
pages of A4.
|