PowerShell Ezine, Logon Scripts

Guy's Scripting Ezine 109 - Getting Started with PowerShell (Monad)

Guy's Scripting Ezine 109 - Getting Started with PowerShell (Monad)

This Week's Secret

Shortly after I wrote this article, Microsoft announced a name change: Monad will now be called PowerShell. 

Monad (PowerShell) is my sort of scripting program.  It is easy to get started easily, yet there is plenty of power and depth.  Above all, Monad is the way of the future, at the very least it will replace CMD, at the very most; Monad will give you new control over your server from the command line.

This Week's Mission

This week I want to persuade you to try Monad.  My mission is to convince you that there are benefits here-and-now for configuring your server by employing PowerShell.  Let us put aside the hype and forget about the future, just start today using Monad to interrogate the Processes, Services and Eventlogs.  So, put aside the GUIs and CMD, and experiment with PowerShell (Microsoft's Scripting Shell).

Once we have mastered Processes, Services and Eventlogs, which is no more that 20 minutes work, we will turn our attention to PowerShell with WMI*.  It is by choosing Monad instead of VBScript to querying WMI, where you will reap the greatest benefits in the spring of 2006.  In the future, you may well employ Monad to configure Exchange 12 or one day for Longhorn, but here and now, Monad will help you manage your Windows 200x Servers.

(* PowerShell and WMI will be featured in Ezine 110 next week)


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


Getting Started - Preliminary

I assume that you have downloaded and installed both .NET Framework 2 and Msh_Setup.msi.  You can then launch Monad by clicking on the Start menu, Run and type 'MSH' in the dialog box.  Incidentally don't worry about tiny Beta errors such as 'Cannot find the P:\ drive'. 

As a base line, think of PS as the Microsoft's new Scripting Shell which replaces cmd.  The power of PowerShell comes from the verb-noun pairs which interrogate the operating system.  My friend and Unix expert 'Barking' Eddie says MSH is like UNIX's bash.  Eddie also told me how Monad reminded him of an affair involving Ruby, Perl and a strange Python, but I will save that story for another day.  Next, I have three examples for you to see for yourself the abilities of PowerShell: Get-process, Get-service and Get-eventlog.

Get-process

At the MSH prompt type: get-process
Result: MSH returns a list of all running processes

Learning Points

The heart of PowerShell are verb-noun pairs.  Note and applaud Microsoft's consistency, the nouns are always singular: process, service and never: processes or services.  Professors of PowerShell claim they only need to learn 50 verbs to master PowerShell, so far I have only needed 20 verbs.  My point is it's easy to get started with PowerShell.

Get-member

Get-member is all you need to research your object or nouns.  For example:
get-process | get-member.

The only trick to this double statement is typing the correct pipe symbol.  You need ASCII 124 or this symbol: |  I hit the key next to the z on my keyboard, but the position may vary.  I don't want to make a mountain out of a molehill, but ASCII 124 displays as a | in most programs, but this pipe character displays as ¦ in the MSH shell.  To test the character, hold down the Alt key and on the numeric keypad type: 124.

One result of issuing the command: get-process | get-member, is that amongst the properties you see 'company'.  Finding company links to one of my most important messages with PowerShell, namely that you can do stuff in MSH that you cannot do with either the Task Manager or the CMD prompt.  To see what I mean try this:
get-process |sort-object company |format-table -groupby company

If you wish, you could append the old dos: | more,
get-process |sort-object company |format-table -groupby company | more

The property 'Company' and the verbs 'sort-object' and '-groupby' are useful and flashy for a demonstrations, but the real message is that PowerShell had loads of gears and extra dimensions.  My thesis is this: professional developers and programs can produce some stunning scripts or cmdlets.  Moreover, we ordinary mortals can dissect, amend and then use the commands to investigate our own servers. 

On this score, my mission is to collect PowerShell one-liners, two-liners, short cmdlets and publish them on my website.  It would be my pleasure to credit you if you send in such a cmdlet.  No script is too short, if it's cool and different from what's on my site already I will be delighted to receive your verb-noun | couplets.

ˆ

Get-service

Start with plain get-service.  Remember that get-service | get-member will display the properties. Next try: service |where {$_.status -eq "Running"}

Learning Points

Note 1: 'Where' is the classic filter.  Strictly it should be where-object.  Indeed I also omitted 'get' from service, this is because 'get' is the default verb.
get-service |where-object {$_.status -eq "Running"}

Note 2: $_. is a placeholder for the current object in the pipeline.  I just accept that 'where' statements, need: $_. followed by property.  In this instance the property is 'status'.

Note 3: -eq self evidently means equals or =   "Running" is a value of 'Status', "Stopped" would be an alternative value.

Get-eventlog

Here is a classic use of PowerShell; once you master 'get-eventlog' and you can quickly monitor the logs from the MSH command line.  These are ideas for you to experiment with:

  1. get-help eventlog
  2. get-eventlog -list
  3. get-eventlog application -newest 100
  4. get-eventlog system -n 500 | where {$_.eventid -lt 100}
  5. get-eventlog system -newest 2000 | where {$_.EntryType -eq "Error"}
  6. get-eventlog system -n 3000 | where {$_.EntryType -eq "Error"} |format-table eventid, EntryType, Message

 

Learning Points

Note 1:  Unlike process and service, eventlog needs an extra noun, you need to tell PowerShell whether to display the System, Application or one of the other logs.

Note 2: Remember get-eventlog system | get-member.  This is how I discovered 'EntryType'.

Note 3: Big eventlogs display thousands of records and it seems to take an age for the operating system to display them all, therefore -newest, or just -n permits you to truncate long lists.

Note 4: Roundup of general points.
a) PowerShell commands are not case sensitive Get or get, format-table or Format-Table. 
b) PowerShell takes wild cards, for example get-process a*
c) For help try get-help eventlog, or interestingly, get-help about*

Summary - Getting Started with PowerShell

In just 20 minutes you can get started with PowerShell.  Live in the present and investigate how MSH can interrogate your Windows server today.  Plan to phase out cmd in favour of MSH.  Seek PowerShell alternatives to GUIs. As a bonus, discover how PowerShell exposes extra properties and therefore extra possibilities for monitoring your servers.

Guy's request, please write in with any neat PowerShell commands that you find for your server.  I will share it with others by publishing on the website.  No script or cmdlet is too simple.

See more Microsoft PowerShell tutorials

Windows PowerShell Home  • Introduction  • Cmdlets  • Exchange 2007  • Profile.ps1  • $_.Pipeline

If you see an error of any kind, do let me know.  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.

 

Computer Training Software - Recommended Training VideosGuy Thomas recommends Computer Training Software

Their topics and material are ideal for getting you started with VBScript.  The videos are easy to follow and you can control the pace.  Try their free demo material and then see if you want to buy the full package. See more about VB Script Training CD.


 *


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.