Effective Windows PowerShell.pdf

(912 KB) Pobierz
Effective Windows PowerShell
Effective Windows
PowerShell
Grok Windows PowerShell and Get More From It.
Keith Hill Copyright © 2007-2009
3/8/2009
466093422.002.png
Introduction
I am a big fan of the “Effective” series of programming books from Effective COM to Effective XML . Without
trying to be too presumptuous, I wanted to capture some of the tidbits I have picked up over the last couple of
years using Windows PowerShell interactively and writing production build and test scripts. These items were
written for PowerShell 1.0. Where appropriate I have added PowerShell 2.0 Update sections to discuss how the
item is affected by the upcoming 2.0 release. As a final note, a number of the PowerShell code snippets shown
use functionality from the PowerShell Community Extensions which can be downloaded from
http://www.codeplex.com/PowerShellCX.
This first item is pretty basic and I debated whether or not it belongs in an "Effective PowerShell" article.
However, these four cmdlets are critical to figuring out how to make PowerShell do your bidding and that makes
them worth covering. The following four cmdlets are the first four that you should learn backwards and
forwards. With these four simple-to-use cmdlets you can get started using PowerShell - effectively.
Key #1: Get-Command
This cmdlet is the sure cure to the blank, PowerShell prompt of death. That is, you just installed
PowerShell, fired it up and you're left looking at this:
Now what? Many applications suffer from the "blank screen of death" i.e. you download the app, install it and
run it and now you're presented with a blank canvas or an empty document. Often it isn't obvious how to get
started using a new application. In PowerShell, what you need to get started is Get-Command to find all the
commands that are available from PowerShell. This includes all your old console utilities, batch files, VBScript
files, etc. Basically anything that is executable can be executed from PowerShell. Of course, you didn't
download PowerShell just to run these old executables and scripts. You want to see what PowerShell can do.
Try this:
PS> Get-Command
CommandType Name Definition
----------- ---- ----------
Cmdlet Add-Content Add-Content [-Path] <Stri...
...
Page 1
466093422.003.png 466093422.004.png
Cmdlet Get-Command Get-Command [[-ArgumentLi...
...
By default, Get-Command lists all the cmdlets that PowerShell provides. Notice that Get-Command is one of
those cmdlets. Get-Command can list more information but how would you figure that out? This brings us to
the second command you need to know and will be using frequently in PowerShell.
Key #2: Get-Help
The Get-Help cmdlet provides help on various topics including what a specified cmdlet does, what parameters it
takes and usually includes examples of how to use the command. It will also provide help on general PowerShell
topics like globbing and operators. Say you want to know what all the help topics are in PowerShell. That’s
easy, just do this:
PS> Get-Help *
Name Category Synopsis
---- -------- --------
ac Alias Add-Content
asnp Alias Add-PSSnapin
...
Get-Command Cmdlet Gets basic informati...
Get-Help Cmdlet Displays information...
...
Alias Provider Provides access to t...
Environment Provider Provides access to t...
FileSystem Provider The PowerShell Provi...
Function Provider Provides access to t...
Registry Provider Provides access to t...
Variable Provider Provides access to t...
Certificate Provider Provides access to X...
...
about_Globbing HelpFile See Wildcard
about_History HelpFile Retrieving commands ...
about_If HelpFile A language command f...
about_logical_Operator HelpFile Operators that can b...
...
And if you only want to see the "about" help topics try this:
PS> Get-Help about*
Name Category Synopsis
---- -------- --------
about_Alias HelpFile Using alternate name...
about_Arithmetic_Oper... HelpFile Operators that can b...
about_Array HelpFile A compact data struc...
...
Now, let's try Get-Help on Get-Command and see what else we can do with Get-Command:
Page 2
466093422.005.png
PS> Get-Help get-command -detailed
NAME
Get-Command
SYNOPSIS
Gets basic information about cmdlets and about other elements of Wind
ows PowerShell commands.
...
PARAMETERS
-name <string[]>
Gets information only about the cmdlets or command elements with
the specified name. <String> represents all or part of the name o
f the cmdlet or command element. Wildcards are permitted.
-verb <string[]>
Gets information about cmdlets with names that include the specif
ied verb. <String> represents one or more verbs or verb patterns,
such as "remove" or *et". Wildcards are permitted.
-noun <string[]>
Gets cmdlets with names that include the specified noun. <String>
represents one or more nouns or noun patterns, such as "process"
or "*item*". Wildcards are permitted.
-commandType <CommandTypes>
Gets only the specified types of command objects. Valid values fo
r <CommandTypes> are:
Alias ExternalScript
All Filter
Application Function
Cmdlet (default) Script
TIP: You will want to use the -Detailed parameter with Get-Help otherwise you get very minimal parameter
information. Hopefully in PowerShell V3 they will fix the "default view" of cmdlet help topics to be a bit more
informative. There are a couple of things to learn from the help topic. First, you can pass Get-Command a
-CommandType parameter to list other types of commands. Let's try this to see what PowerShell functions are
available by default:
PS> Get-Command -commandType function
CommandType Name Definition
----------- ---- ----------
Function A: Set-Location A:
Function B: Set-Location B:
Function C: Set-Location C:
Function Clear-Host $spaceType = [System.Mana...
...
Function help param([string]$Name,[stri...
Page 3
466093422.001.png
Zgłoś jeśli naruszono regulamin