Windows
PowerShell - Microsoft's New Scripting Language
Introduction to Windows PowerShell
My mission is to two fold. Firstly, to encourage a new generation of script writers to learn
PowerShell; secondly I want to provide
experienced scripters with a bank of example scripts so that they can convert to
PowerShell.
To newbies I want to say: 'It really will be easy to get started. You
will be surprised by how many tasks on your Windows computer benefit from a
PowerShell script'. To the experienced scripters I say, 'I want to build a
reference library for that moment when you have forgotten the syntax for a
particular PowerShell command'.
To help you discover the benefits of PowerShell, I will not only provide
real life tasks, but also I will add learning points.
One of my greatest joys is when you modify the
code in my PowerShell tutorials to fit your
situation.
In 2009 Microsoft released PowerShell 2.0 CTP3. To check which version
you have, launch PowerShell and type these five characters: $host
One trigger for dabbling
with Windows PowerShell is so that you can configure Exchange Server 2007. However, I believe that the
pressing reason for learning PowerShell is for issuing WMI commands. When you want to interrogate your Microsoft operating system, three lines of
PowerShell will replace twenty lines of VBScript. PowerShell
is free, therefore all you need is a spare half hour to experiment with the potent new
verb-noun pairs at the PowerShell command line. Before you install
PowerShell, make sure that you have the correct
version for your XP, Vista or Windows Server 2003 operating system.
Think of learning PowerShell as a more convenient method of configuring the server than using the corresponding GUI. I can foresee running
PowerShell scripts that will diagnose server problems. With
PowerShell you can run queries
that are not possible from the GUI. On a good
day such a script would even cure the problem - magic!
To come back down to earth, let us
start with a trivial example to illustrate the capability of PowerShell. As you may know, the Task Manager will display all the processes, yet with
PowerShell you can
not only list all processes, but also group them by company (Microsoft, HP
and Roxio). All you need is this one-liner:
get-Process | group-Object company
PowerShell will achieve the same results as VBScript, but with fewer commands.
For instance, PowerShell makes scripting WMI much easier, for example,
try: get-WmiObject win32_BIOS.
More good news, you can even use PowerShell to replace VBScript for Active Directory scripts.
I
have a sense that there is a changing of the guard; out with old CMD commands, and in with new
PowerShell language. More importantly, the new PowerShell language will give you command line control
of the Windows operating system; this is similar to the power that Bash
commands exert over the UNIX operating system.
In a nutshell:
PowerShell really is the way of the future.
The main
reason for learning PowerShell is so that you can administer Exchange Server 2007,
and also the Longhorn server that will succeed Windows Server 2003.
Microsoft's thinking is that the point has arrived where complex
products like Exchange and Longhorn have so many menus and sub-menus to
configure via a GUI, that we need an alternative. Hence the easy-to-use scripting language
called PowerShell.
Note: Existing Windows executables, such as Ipconfig and ping, perform in the PS Shell just as they do in the old CMD 'DOS box'.
Simple PowerShell Examples
Preamble, Start, Run PowerShell. At the command line type (In that dark blue shell):
get-service
Here is another example, type:
get-WmiObject win32_computersystem
PowerShell Needs Less Code
I first realised the ability of
PowerShell when I saw three lines of PowerShell replace about
twenty
lines of VBScript. The task is to interrogate Win32_classes, we can either use the traditional VBScript and WMI, or the new
PowerShell with its verb-noun commands.
VBScript
Windows PowerShell
' Memory.vbs ' VBScript to discover how much RAM in computer ' Author Guy http://computerperformance.co.uk/ ' Version 1.3 - August 2005 '
-------------------------------------------------------' Option Explicit Dim objWMIService, objComputer, colComputer Dim strLogonUser, strComputer strComputer = "." Set objWMIService =
GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2") Set colComputer = objWMIService.ExecQuery _ ("Select * from Win32_ComputerSystem") For Each
objComputer in colComputer Wscript.Echo "System Name: " & objComputer.Name _ & vbCr & "Total RAM " & objComputer.TotalPhysicalMemory Next
WScript.Quit
' End of Sample Memory WMI / VBScript
# Windows PowerShell script # Displays ComputerName and memory $CompSys = get-WmiObject win32_computersystem
Microsoft refer to this new PowerShell command style as 'task-based'.
As with other scripting language, PowerShell lends itself to automating repetitive tasks,
especially as you can store the lines of code in cmdlets scripts to reuse
latter. For
programmers and developers, PowerShell offers the ability to manipulate
the operating system's objects, it is to gain this ability that you need to first install .Net Framework 2.0 (or 3.0).
Guy Recommends: SolarWinds Engineer'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
3) Once you have installed the downloaded executables, press the Windows key
+r, this launches the Run command (even in Vista), now type in the dialog
box: PowerShell. Visit Microsoft's
Windows PowerShell download center
The situation is that you are at the PowerShell prompt, and you want to get
started. The basic PowerShell command consists of two-word sentences. Start with a verb, follow with a
hyphen and end with a noun. Naturally,
you press carriage return when you have finished the command. Try
these PowerShell examples:
get-Process (Correct).
get - process (Wrong no spaces required next to the hyphen).
With time, and
plenty of practice, you will soon increase your
vocabulary. A good place to start is with nouns such as eventlog,
process and service. For example: get-Eventlog -list.
From the
above example, we learn that the hyphen has a second, or complimentary role, namely
to act as a modifier, switch or as PowerShell calls it a -parameter. If we study this command structure,
the role of a space, or lack of a space, becomes obvious, even intuitive, for example -list tells the command to display all the event logs. However, after get-Eventlog the code needs a breather,
hence a space BEFORE the -list modifier.
get-Eventlog - list (Wrong) get-Eventlog-list (Wrong)
get-Eventlog -list (Correct)
Perhaps the most useful command for beginners is: get-help.
Alternatively, you could try plain: help. Incidentally, from this we deduce that the default verb is 'get'. Test this theory by typing just a noun that
PowerShell understands for example: Process help eventlog
In each case 'get-' is assumed. (get-Process, get-help or get-Eventlog)
Other common verbs are: set, start, stop, new, add, copy and out (out as in output file).
PowerShell is a potent language, in this section I am just scratching
the surface. Once you get going, then you can create more complex
scripts by employing the usual scripting structures such as functions, loops and 'where' clauses.
If
PowerShell were a car, so far
we have only driven in first gear and I have barely touched the accelerator. For now, you will either have to trust me when I say
PowerShell has power, depth and flexibility, or else you can research for
your self. To give you one example, with PowerShell you can create new objects for example, DirectoryServices.DirectoryEntry or the simpler, system.DateTime. Keep in mind that it is thanks to .Net Framework that
PowerShell allows you to build such objects.
PowerShell is a friendly new scripting language for administering Windows and Exchange servers. Even in its RC2 form,
PowerShell makes tasks such as interrogating WMI objects easier. Moreover, one day
PowerShell
will be Microsoft's standard scripting shell, meanwhile my PowerShell
tutorials will make it easier for you to configure Exchange 2007, and
also Active Directory in Windows Server 2008.
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.
Guy
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.