PowerShell Ezine, Logon Scripts

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.

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.


If you are looking for handy network utilities, try some of the free downloads at Tools4Ever


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.

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 SolarWinds ipMonitor

For more network research, I recommend ipMonitor.  My attraction to ipMonitor is because it inhabits that zone of part work, part play; Guy just could not put the dashboard away.  This excellent performance monitor will get you started in the quest to remove bottlenecks on your network.  SolarWinds provides this fully-functioning product free for 21 days.  So download and install ipMonitor, then start scrutinizing your computers Network, CPU, memory and disk performance.  You can also select from zillions more performance counters such as fan temperature and battery level.

Download SolarWinds ipMonitor (21 days eval)

ˆ

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.

See More Microsoft PowerShell WMI Examples:

Home   • PowerShell WMI   • WMI Services   • Memory   • Disk   • DNS   • Win32_pingstatus

WMI Class   • Win32_NetworkAdapter   • Win32_NetworkAdapterConfiguration   • Disable NIC

Examples   • Win32_product   • PowerShell -Query   • PowerShell -Filter

Please write in if you see errors of any kind.  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.

Download my ebook:Getting Started with PowerShell
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.

 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

Home Copyright © 1999-2010 Computer Performance LTD All rights reserved

Please report a broken link, or an error.