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
Table of Contents
Introduction................................................................................................................................................................1
Item 1: Four Cmdlets that are the Keys to Discovery within PowerShell..................................................................1
Key #1: Get-Command...........................................................................................................................................1
Key #2: Get-Help....................................................................................................................................................2
Key #3: Get-Member .............................................................................................................................................5
Key #4: Get-PSDrive ...............................................................................................................................................6
PowerShell 2.0 Update ...........................................................................................................................................7
Item 2: Understanding Output ..................................................................................................................................8
Output is Always a .NET Object ..............................................................................................................................8
Function Output Consists of Everything That Isn't Captured .................................................................................9
Other Types of Output That Can't Be Captured .................................................................................................. 11
Item 3: Know What Objects Are Flowing Down the Pipeline ................................................................................. 12
Item 4: Output Cardinality - Scalars, Collections and Empty Sets - Oh My! ........................................................... 15
Working with Scalars ........................................................................................................................................... 15
Working with Collections .................................................................................................................................... 16
Working with Empty Sets .................................................................................................................................... 17
Item 5: Use the Objects, Luke. Use the Objects! ................................................................................................... 19
Item 6: Know Your Output Formatters................................................................................................................... 22
Item 7: Understanding PowerShell Parsing Modes ................................................................................................ 31
Item 8: Understanding ByPropertyName Pipeline Bound Parameters .................................................................. 35
Item 9: Understanding ByValue Pipeline Bound Parameters................................................................................. 38
Item 10: Regular Expressions - One of the Power Tools in PowerShell ................................................................. 42
PowerShell 2.0 Update ........................................................................................................................................ 43
Item 11: Comparing Arrays..................................................................................................................................... 43
Item 12: Use Set-PSDebug -Strict In Your Scripts - Religiously ............................................................................... 45
PowerShell 2.0 Update ........................................................................................................................................ 47
Item 13: Commenting Out Lines in a Script File ..................................................................................................... 47
PowerShell 2.0 Update ........................................................................................................................................ 48
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.
Item 1: Four Cmdlets that are the Keys to Discovery within PowerShell
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
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
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
Plik z chomika:
QzyneC
Inne pliki z tego folderu:
Training Kit_70-642.zip
(45268 KB)
70-646 - Administration Windows Server 2008 - Training Kit.pdf.pdf
(10902 KB)
poradnik_WS08_podglad.pdf
(4068 KB)
Tajemnica SID.pdf
(52 KB)
Początki z Windows Server2008 Core.pdf
(314 KB)
Inne foldery tego chomika:
Amiga
Amiga Soft
Dokumenty
DVD Free RNS 510
Galeria
Zgłoś jeśli
naruszono regulamin