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

 ♣

This Week’s Secret

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

This Week’s Mission is divided into four tasks.

  1. Get to know the PowerShell ‘Service’ family.
  2. Investigate the properties of get-Service
  3. List the Services actually installed on your machine
  4. 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. 

Example 1: To List PowerShell’s Service Cmdlets

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.

Guy Recommends: The Free IP Address Tracker (IPAT) IP 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

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.

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

Example 4:  Command to Restart a Service Such as Spooler

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!

Guy Recommends: The Free IP Address Tracker (IPAT) IP 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

Guy Recommends: The Free IP Address Tracker (IPAT) IP 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   • PowerShell Start-Process   • Set-Service

Get-Service   • Start-Service   • Stop-Service   • Restart-Service   • Free WMI Monitor

PowerShell Start-Sleep   • Get-WmiObject win32_service   • 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.