Windows PowerShell


Windows PowerShell get-WmiObject win32_service

Windows PowerShell get-WmiObject win32_service

PowerShell provides two ways of scripting Windows services.  For lists of services I employ get-WmiObject win32_service.  However, when I want to deal with a particular service I choose get-Service followed by nameOfService.

Topics for PowerShell get-WmiObject win32_service

 ♣

Our Mission

Our main mission is to produce a script which returns interrogates Windows services and generates a list of selected properties.  In particular, I want to group then count how many services operate under each of the three 'StartName' accounts: LocalSystem, LocalNetwork and LocalService. 

I also want to draw your attention, or remind you, of three useful scripting techniques:
1) `backtick for word-wrap. 
2) | for Pipelining the output of a basic instruction into a new command
3) How to control the output with group-Object, and format-Table.

Preliminary Check of the Properties with Get-Member

Let us start with a preliminary script which employs get-Member to research the properties of services. From the resulting list we can compare the properties, and then decide whether to choose get-Service or get-WmiObject win32_service to script a particular task.

# PowerShell script to compare:
#    get-Service with
#    get-WmiObject win32_service
# Author: Guy Thomas
# Version 1.6 May 2009 tested on PowerShell v2 CTP3

get-WmiObject win32_service | get-Member -memberType Property

Note 1:  You could filter get-WmiObject win32_service by appending this command:
|where {$_.name -match "__"}

Note 2:  To digress into the world of WMI, get-WmiObject has zillions more classes.  If you are curious try this: get-WmiObject -list.

Investigating Security Accounts for Services

Let us investigate StartName.  This property reveals that each service relies on one of three built-in security accounts: LocalSystem, LocalService and LocalNetwork.  Background research unearths that LocalSystem is the most powerful as it accesses system security privileges, which are not available to the other two accounts.

# Script to list the StartName values get-WmiObject win32_service
# Author: Guy Thomas
# Version 1.2 May 2009 tested on PowerShell v2 CTP3

get-WmiObject win32_service | group-object -Property StartName `
| format-table Name, Count -auto

Note 1:  Any problems, simplify the script to:
get-WmiObject win32_service |format-table Name, StartName -auto

Note 2:  The tiny backtick at the end of the line `
means the same PowerShell command continues on the next line.  As with many scripting languages, there is no automatic word-wrap, thus end-of-line normally means that the command terminates, the backtick extends the commands to the next line. 

Trap: With Backtick: beware, there must be no space after the `backtick.

Note 3: You could add this pipeline to refine the command to include only "Running" services:
| Where-Object {$_.state -eq "Running"} `

Note  Out-GridView: PowerShell v 2.0 introduces a new cmdlet to control data display.  See more on how to pipe the results into out-GridView.

Guy Recommends: SolarWinds Engineer's Toolset v10Engineer's Toolset v10

The Engineer's Toolset v10 provides a comprehensive console of utilities for troubleshooting computer problems.  Guy says it helps me monitor what's occurring on the network, and the tools teaches me more about how the system literally operates.

There are so many good gadgets, it's like having free rein of a sweetshop. Thankfully the utilities are displayed logically: monitoring, discovery, diagnostic, and Cisco tools.  Download your copy of the Engineer's Toolset v 10

Grouping and Counting the Windows Services

Here is where pipelining comes into its own.  Once we get a simple command working, then we refine the output by piping the output into a new command.  I once thought that there was a limit of three such pipes; however, I have successfully tested a chain of 5.  I have to say that beyond 3 pipes and the script gets a bit complicated for my liking.

# Script to list the StartName values get-WmiObject win32_service
# Author: Guy Thomas
# Version 1.2 May 2009 tested on PowerShell v2 CTP3

get-WmiObject win32_service | Where {$_.state -eq "Running"} `
| group-object -Property StartName `
| format-table Name, Count -auto

Note 1:  The above script incorporates a filter.  Talking of filters, my friend 'Mad' Mick says that
 -filter "state = 'running'" is superior to | Where {$_.state -eq "Running"} ` Do you know, I think Mick is right, -filter is better than where-object in this case.

Note 2:  Strictly speaking, I should use where-Object rather than plain 'Where'.  However PowerShell is forgiving, and providing there is no ambiguity it uses its built-in aliases for the shortened forms.  For instance you could use 'group' rather than group-Object.

# Script to list the StartName values get-WmiObject win32_service
# Author: Guy Thomas
# Version 1.2 May 2009 tested on PowerShell v2 CTP3

get-WmiObject win32_service -filter "state = 'running' " `
| sort StartName | group-object -Property StartName `
| format-table Name, Count -auto

Guy's GUI Tip

There are numerous PowerShell authors who have more scripting knowledge than I, but none seem to share my penchant for comparing what I see in the Windows utility with the output of a script.  In this instance I recommend launching Services.msc so that we can make comparisons between the GUI and  the results of my PowerShell services script. 

Firstly, this GUI always surprises me with the sheer number of services; moreover, each successive version of Windows spawns yet more services.  Secondly, be aware that the 'Name' column in the GUI corresponds to the 'Caption' property when you use win32_service.  Furthermore 'Log on As' corresponds to 'StartName' in the script.

  ˚

Out-file

If I could digress and hark back to the verbosity of VBScript; because it took so much code to list the services, I feared that it would confuse beginners if I added another 10 lines of VBScript in order to output the list of services to a text file.  With PowerShell there is no such worry, all you need to store the results is to append these few words:
| out-file "D:\PowerShell\Scripts\services.txt"

# Script to list the StartName values get-WmiObject win32_service
# Author: Guy Thomas
# Version 1.2 May 2009 tested on PowerShell v2 CTP3
get-WmiObject win32_service -filter "state = 'running' " `
| sort StartName | group-object -Property StartName `
| format-table Name, Count -auto
| out-file "D:\PowerShell\Scripts\services.txt"  

Summary of Scripting Services with PowerShell

My main point is that there are two methods for scripting PowerShell services.  Plain get-Service has fewer properties than the WMI alternative.  From a practical point of view, these scripts show you how to group and count the local accounts under which, the Windows services operate.  Tip, where possible observe the built-in GUI alongside your PowerShell script, in this case launch Services.msc and make comparisons between the GUI's columns and PowerShell's properties.

See more PowerShell examples of process and service

PowerShell Home  • get-Process  • stop-Process  • get-Service  • start-Service  • gwmi win32_service

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-2009 Computer Performance LTD All rights reserved

Please report a broken link, or an error.