PowerShell Function Get-ServiceStatus

Check the Status of Windows ServicesPowerShell Function Get-ServiceStatus

The purpose of this cmdlet-style function is to filter the Windows services.  By Scripting with Windows PowerShell you can list which services are 'Running', and which are 'Stopped'.

My overall aim is to provide useful examples of PowerShell functions, which you can modify for your situation. Indeed, this function lacks perfection, however, I have appended ideas to improve the PowerShell code.

Guy's Get-ServiceStatus Function (Cmdlet)

 ♣

Planning the Get-ServiceStatus Function

The default output of this function is to list Windows services whose Status is "Running".  Furthermore I have created a parameter so that you can append -Status Stopped, and thus get a different list of Microsoft and other services.

1) Header Section – Help and Description
I leave this section until I am comfortable with the function, only then do I make comments here in the SYNOPSIS section <# ….Adding notes… #>.

2) Parameters
Param(
Observe the relationship between $Status in the Param, and -Status when you modify the Get-ServiceStatus function at the command line.  My advice is to return to the parameter's section once you have the basic Process {Get-Service instructions} working.

3) Process Keyword
This contains the engine of your script.  It could contain one elegant PowerShell command, or alternatively a series of If … ElseIf statements, which refine the base Get-Service command.

4) Begin and End
These are optional sections, I have not included a Begin or End section in this particular function.

Creating a PowerShell Function Called Get-ServiceStatus

Try this script and understand its deficiencies, then make improvements.  I have suggestions to help you further down the page.

Function Global:Get-ServiceStatus {
<#.SYNOPSIS
PowerShell function list the status of Windows services.
.DESCRIPTION
Includes parameters to list all services.
In truth, this function needs more work on its logic.
.EXAMPLE
Get-ServiceStatus -Stopped
.EXAMPLE
Get-ServiceStatus -All
#>
[cmdletbinding()]
Param
(
[String]$Status ="Running",
[String]$All ="Filter"
      )
Process  {
Clear-host
If ($All -eq "Filter"){
Get-Service | Where-Object {$_.Status -eq "$Status"}
          } # End of If
Else {
"All Services"
Get-Service | Group-Object Status
        } # End of Else
    } # End of Process section
} # End of Function

3 Commands to try

  • Get-Help Get-ServiceStatus -Full
  • Get-ServiceStatus -Status stopped
  • Get-ServiceStatus -All Services 

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Problems with My Get-Service Status Function

One major defect with this function is that the user has to guess what to add after the -All param.  We can refine the PowerShell code in these ways:

Function Improvements

1) Create an $All [Switch] Parameter.

2) Adjust the If … Else statements.

3) While not essential, you could add a Begin and End Section.

Param (
[String]$Status ="Running",
[Switch]$All
)

———————————————

Process {
Clear-host
If ($All){
"All Services"
Get-Service | Group-Object Status
   }
Else {
Get-Service | Where-Object {$_.Status -eq "$Status"}

Result
Get-ServiceStatus -All  This works on its own; the user does not have to guess what to type.

PowerShell Learning Points

Process
If you take away just one concept from this script, it's that Process is not only a keyword, but also it is the heart of the PowerShell function.  In this instance, my 'Process' section is very much based on Microsoft's built-in Get-Service cmdlet.

It's not uncommon to create functions which simply encapsulate what you can achieve with a normal cmdlet / function.  In this example I have bolted-on a 'Where' clause to Microsoft's Get-Service cmdlet.

Engineer's Toolset v10Guy Recommends: SolarWinds Engineer’s Toolset v10

This Engineer’s Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems.  Guy says it helps me monitor what’s occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it’s like having free rein of a sweetshop.  Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools.  Try the SolarWinds Engineer’s Toolset now!

Download your fully functional trial copy of the Engineer’s Toolset v10

Problems Creating PowerShell Functions

Experts make it look easy, but it is a major challenge for a beginner to create a PowerShell Function.  There are at 3 sections, and numerous Microsoft syntax rules to master, and it's easy to give up and get the task done without using functions.

Firstly, there is the emotional problem that you can use PowerShell perfectly well without bothering with functions.  Secondly, there are so many terms vying for your attention, for example: param, mandatory=$true and process.

There is good news, creating a PowerShell function is most satisfying, and when you are learning you can just ignore factors such as synopsis and help if you don't fancy them.

My technique was to learn by doing, building on success, gradually coming to embrace terms such as Global, [String] and to understand the significance of the brackets and the punctuation.

See more on PowerShell function parameters »

Summary of Create Get-ServiceStatus Function

The purpose of this PowerShell function is to filter the Windows services, so that you can see which are running, and which are 'Stopped'.  My aim is to provide useful examples of PowerShell functions, which you can modify for your needs.

If you like this page then please share it with your friends

 


See More PowerShell Function Parameters

Scripting PowerShell Function   • PowerShell Function Clear-Recyclebin   • PowerShell Get-File

Create PowerShell Function Get-ServiceStatus  • PowerShell Function Get-Driver  • PowerShell Outlook

Show-BalloonTip   • PowerShell Function Get-IPConfig   • Free Permissions Analyzer

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.