Windows PowerShell


Windows PowerShell - WMI Services

Windows PowerShell - WMI Services

Here is a handy PowerShell script to investigate the operating system's services.  While I focus on the Alerter service, you could easily modify the script to experiment with other Windows Services.

Topics for PowerShell - WMI Services

Mission

PowerShell script to check whether a service is installed.  If the service is present to report its status.

Script 1 - The basic command

# PowerShell script
# Guy Thomas October 2006
# Script to check whether a service is installed
# Version 1.1
cls
get-service -display "alerter"

Script 2 - Introducing a $Variable

The idea here is to introduce PowerShell $Variable, and also to check the properties and methods with Get-Member

cls
$sName = "alerter"
$service = get-service -display $sName
$sName |Get-Member

Challenge  Research other services, for example 'Print Spooler', 'Windows Shadow Copy' or 'Windows Time'.

Script 3 - Introducing If.. Else Logic.

Let us consider the possibility that the service is not installed; after all one, use of the script is to check service availability.  What we are going to do is introduce 'If ... Else' logic.  The exclamation mark ! checks for a null value for the service name.  Should this if logic be true, then we declare ' This service is not installed on the computer', except we use a variable in place of 'This service'.

# PowerShell script
# Guy Thomas October 2006
# Script to check whether a service is installed
cls
$sName = "alerter"
$service = get-service -display $sName -ErrorAction SilentlyContinue
if ( ! $service )
{ $sName + " is not installed on this computer. `nDid you

really mean: " + $sName }
else { $sName + " is installed."
$sName + "'s status is: " + $service.Status }

 

PowerShell Learning Points

Note 1: -ErrorAction SilentlyContinue.  If a script encounters a runtime error it displays a helpful error message, then continues.  This switch or modifier merely suppresses that error message.

Note 2:  `n This grave accent (ASCII 096) followed by n tells the script to insert a carriage return.

Note 3:  PowerShell is particular about brackets

Note 4:  CLS is a quirk of mine, this old DOS command clears the screen, but you should not use it production scripts. 

Marco points out the following error with PrimalScript, which is caused by CLS:
Line 2: Exception calling "SetBufferContents" with "2" argument(s): "The method or operation is not implemented."
*** PowerShell Script finished. ***

  ˚

See also more PowerShell WMI topics

PowerShell Home  • WMI  • Classes  • Service  • WMI File  • WMI Query  • DNS  • Disk

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

Webcomputerperformance.co.uk

Guy Recommends: SolarWinds Exchange Monitor

Exchange Monitor from SolarWindsHere is a free tool to monitor your Exchange Server

 

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

Please report a broken link, or an error.