Ezine 147 - PowerShell ServicesWindows PowerShell Scripting - Get-ServiceOur PowerShell mission for Windows services is to list them, to stop them, and especially to start them. I would like to begin this week with a thorough grounding both in the PowerShell syntax, and also the properties available to the get-Service command. Once we have mastered the basics of this verb-noun pair, then next week we will investigate tasks for other members of the 'Service' family of commands. PowerShell's Get-Service Topics
♣ This Week's SecretScripting services lends itself to one of my favorite techniques, namely having the GUI open while I execute the code. The advantage of this approach is two-fold; you can check what the script is doing by observing a value change in the Service GUI. Also, the GUI's menus and columns give me ideas for creating better scripts. If you like this technique, click on the Windows Start button, Run, type services.msc (do remember the .msc extension). One key role for a good computer techie is checking, starting and sometimes, stopping an operating system's service. Indeed, expertise with Windows Services is one discriminator between a real network administrator and a 'paper' MCSE impostor. This week's PowerShell scripts will get you started with my mission to monitor and control which Windows Services should be running on your servers and workstations. This Week's MissionLet us build-up logically. Firstly, we will list all the services on your computer. Next we will filter the scripts output so that it lists only services which are "Stopped", or "Running". From there we will set about adjusting the start-up type to manual, automatic or disabled. And then next week we will create advanced scripts, which will start or stop named services. Sometimes - like now, it's hard for me to stay focussed on the one item namely scripting with PowerShell. Instead I get distracted checking the list of services to see if any rogue maleware or grayware services have crept onto my computer. Then I have another run-through the list to see if services that should be disabled, are in fact running. However, the good news is that while this sidetracks me from writing code, I am increasing my list of useful jobs to automate with PowerShell. Example 1: Listing all the services on your computer
Instructions:
# clear-Host Learning PointsNote 1: It is said that PowerShell is a self-describing language, what this means is that you can interrogate the properties of an object directly. Don't miss a chance to experiment with my 'Trusty Twosome' of help and get-member. In this instance try: a) help get-Service -full Note 2: I have yet to find a PowerShell noun that is not singular, for example, Service (and not Services). Realization of this consistency saves me typos. Note 3: PowerShell commands are not case sensitive. Get-service works as well as get-Service. Challenge: Try filtering with: get-Service S* or use the square brackets and select a range: If you are looking for handy network utilities, try some of the free downloads at Tools4Ever Example 2: Manipulating the OutputFollowing research from get-Service | get-Member, we can refine the output so that we add extra properties, for example CanStop and ServiceType. This example also illustrates how we can 'Sort', or 'Sort-Object' on ServiceType rather than the 'Name' property. # clear-Host Learning PointsNote 1: The backtick ` tells PowerShell that the same command continues on the next line. Without the tiny backtick (`) there is no word-wrap, and thus PowerShell would interpret the new line as a new command. In this instance we want one long command, which happens to spill over two lines. Note 2: The -auto parameter produces more sensible column widths. Note 3: When learning, I like to give the full syntax, however, for production scripts Sort-Object is normally abbreviated to plain 'Sort', just as format-table is usually written as 'ft'. In fact, you can also omit the -property parameter and the script will still work: get-Service * |Sort ServiceType |ft name, servicetype, status, canstop, -auto Challenge 1: Research other properties, in particular more members of the Can* family. Challenge 2: Try an alternative sort criterion, for example Sort-Object Status. Guy Recommends:
The Free IP Address Tracker (IPAT)
| |||||
Custom Search
|
Guy Recommends: WMI Monitor and It's Free!
|
|
Home Copyright © 1999-2012 Computer Performance LTD All rights reserved Please report a broken link, or an error. | |