1. Trang chủ >
  2. Công Nghệ Thông Tin >
  3. Kỹ thuật lập trình >

2 It’s typing class all over again

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (9.65 MB, 367 trang )


16



CHAPTER 2



Figure 2.6



Meet PowerShell



IntelliSense works like tab completion in the ISE.



values for that parameter. This works only for parameters that have a predefined set of allowable values (the set is called an enumeration). Again, hit

Escape to clear the command line—you don’t want to run that command yet.

The PowerShell ISE offers something similar to, and better than, tab completion:

IntelliSense. This feature operates in all four of the same situations that we showed

you for tab completion, except that you get a cool little pop-up menu, like the one

shown in figure 2.6. You can use your arrow keys to scroll up or down, find the item

you want, hit Tab or Enter to select it, and then keep typing.

IntelliSense works in the ISE’s Console Pane and in the Script Editor Pane.

CAUTION It’s very, very, very, very, very important to be very, very, very, very accurate when you’re typing in PowerShell. In some cases, a single misplaced

space, quotation mark, or even carriage return can make everything fail. If

you’re getting errors, double- and triple-check what you’ve typed.



2.3



Common points of confusion

Let’s quickly review some of the things that can muck up the works to make sure they

don’t trip you up as well:



www.it-ebooks.info



What version is this?



17



 Horizontal scroll bars in the console app—We’ve learned from years of teaching



classes—this trips up people every single time. Configure the console to not

have a horizontal scroll bar across the bottom of the window. We explained how

to do this earlier in this chapter.

 The 32-bit versus 64-bit issue—You should be running a 64-bit version of Windows

and using the 64-bit versions of PowerShell’s applications (the ones that don’t

say “(x86)”). We know for some folks it can be a big deal to go buy a 64-bit computer and a 64-bit version of Windows. But that’s the investment you’ll have to

make if you want to use PowerShell effectively. Most of what we cover in this

book will work fine on 32-bit, but when you’re working in a production environment, 64-bit makes all the difference.

 Make sure the PowerShell application’s window title bar says “Administrator”—If it

doesn’t, close the window, right-click the PowerShell icon again, and select Run

As Administrator. In a production environment, you might not always do this,

and later in the book we’ll show you how to specify credentials when you run

commands. But for the moment, you need to be sure the shell window says

“Administrator” or you’ll run into problems later.



2.4



What version is this?

It can be incredibly difficult to figure out which version of PowerShell you’re using, in

no small part because every released version installs to a directory named “1.0” (which

refers to the language engine of the shell, meaning every version has been made backward compatible to v1). With PowerShell v3, there’s an easy way to check your version:

PS C:\> $PSVersionTable

Name

---PSVersion

WSManStackVersion

SerializationVersion

CLRVersion

BuildVersion

PSCompatibleVersions

PSRemotingProtocolVersion



Value

----3.0

3.0

1.1.0.1

4.0.30319.17379

6.2.8250.0

{1.0, 2.0, 3.0}

2.2



Type $PSVersionTable and hit Enter. You’ll immediately see the version number for

every PowerShell-related piece of technology, including PowerShell itself. If this

doesn’t work, or if it doesn’t say “3.0” for “PSVersion,” you’re not using the right version of PowerShell for this book. Refer to chapter 1 for instructions on getting v3.

TRY IT NOW Don’t wait any longer to start using PowerShell. Start by checking your version number to ensure it’s 3.0. If it isn’t, don’t go any further

until you’ve installed v3.



PowerShell v3 can install side by side with v2. In fact, you can run PowerShell.exe

-version 2.0 to explicitly run v2. You can set it to run v2 if you have something that



www.it-ebooks.info



18



CHAPTER 2



Meet PowerShell



isn’t v3 compatible (which is rare). PowerShell v3’s installer doesn’t install v2; you’ll be

able to run only v2 if it was installed first. The installers for v2 and v3 will both

overwrite v1 if it’s already installed; they can’t exist side by side. All current versions of

Microsoft software (including the latest service packs of some older-generation software like Exchange Server 2007) will run with v2 installed.

TIP The newest versions of Windows install PowerShell v3 by default, but

include the PowerShell v2 engine. From PowerShell, run Add-WindowsFeature

powershell-v2 to install the v2 engine if you need it.



2.5



Lab

Because this is the book’s first lab, we’ll take a moment and describe how these are

supposed to work. For each lab, we’ll give you a few tasks that you can try and complete on your own. Sometimes we’ll provide a hint or two to get you going in the right

direction. From there, you’re on your own.

We absolutely guarantee that everything you need to know to complete every lab is

either in that same chapter or covered in a previous chapter (and the “previously covered” information is the stuff for which we’re most likely to give you a hint). We’re not

saying the answer will be in plain sight: most often, a chapter will have taught you how

to discover something on your own, and you’ll have to go through that discovery process to find the answer. It might seem frustrating, but forcing yourself to do it will

absolutely make you more successful with PowerShell in the long run. We promise.

Keep in mind that you can find sample answers at MoreLunches.com. Our answers

might not exactly match yours, and that will become increasingly true as we move on

to more complex material. In fact, you’ll often find that PowerShell offers a half

dozen or more ways to accomplish almost anything. We’ll show you the way we use the

most, but if you come up with something different, you’re not wrong. Any way that

gets the job done is correct.

NOTE



For this lab, you’ll need any computer running PowerShell v3.



We’ll start easy: we just want you to get both the console and the ISE set up to meet

your needs. Follow these five steps:

1

2



3



4



Select fonts and colors that work for you.

Make sure the console application has no horizontal scroll bar at the bottom.

We’ve already mentioned this about three times in this chapter, so maybe it’s

important.

In the ISE, get the Console Pane maximized; remove or leave the Commands

Explorer at your discretion.

In both applications, type a single quote, ', and a backtick, `, and make sure you

can easily tell the difference. On a U.S. keyboard (at least), a backtick is on one

of the upper-left keys, under the Escape key, on the same key as the tilde (~)

character.



www.it-ebooks.info



Further exploration

5



19



Also type (parentheses), [square brackets], , and {curly brackets} to make sure the font and size you’ve selected display well, so that all of

these symbols are immediately distinguishable. If there’s some visual confusion

about which is which, changes fonts or select a bigger font size.



We’ve already walked you through how to accomplish these steps, so you don’t have

any answers to check for this lab, other than to be sure you’ve completed all five of

the steps.



2.6



Further exploration

What Microsoft gives you isn’t the only way to use PowerShell; you’ll find other free

and commercial editors and shell environments designed specifically for PowerShell,

and you should try them out. Even the commercial ones come with a free trial period,

so there’s no reason not to take them all for a spin.

 PowerGUI —You’ll find a free edition from http://powergui.org and a profes-



sional (commercial) edition from http://quest.com/powershell

 SAPIEN PrimalScript and PrimalForms—Two commercial tools from http://

primaltools.com

 PowerSE and PowerWF—A free editor and a commercial workflow solution from

http://powerwf.com

 Idera PowerShell Plus—An editor and console environment from http://

idera.com

You can likely find others, but these four are the big players and the ones with which

we’re most familiar. We’re not associated with any of these companies (beyond appreciating their work). We’re often asked which of these we use the most, at which point

we have to admit we use Microsoft’s ISE, mainly because we’re constantly rebuilding

our virtual machines and we’re too lazy to keep reinstalling editors (and we haven’t

gotten around to writing a PowerShell script to do it for us). That said, when we do

find ourselves using a third-party tool, it’s usually PowerShell Plus, because we like the

way it provides an enhanced console as well as a script editor, and we like the integration between the two modes. But you should find an editor that meets your needs and

budget.

In January 2012, Don did a write-up of various PowerShell environments. Provided

you’re reading this book sometime before January 2014 (when the paper expires and

will be updated based on available products at that time), you can get the paper at

http://library.concentratedtech.com. It was written during the PowerShell v3 era, and

applies to specific versions of the products mentioned, but it might give you a good

starting point for learning the shell.



www.it-ebooks.info



Using the help system



In the first chapter of this book, we mentioned that discoverability is a key feature

that makes graphical user interfaces (GUIs) easier to learn and use, and that

command-line interfaces (CLIs) like PowerShell are often more difficult because

they lack those discoverability features. In fact, PowerShell has fantastic discoverability features—but they’re not that obvious. One of the main discoverability features is its help system.



3.1



The help system: how you discover commands

Bear with us for a minute as we climb up on a soapbox and preach to you.

We work in an industry that doesn’t place a lot of emphasis on reading,

although we do have an acronym, RTFM, that we cleverly pass along to users when

we wish they would “read the friendly manual.” Most administrators tend to dive

right in, relying on things like tooltips, context menus, and so forth—those GUI discoverability tools—to figure out how to do something. That’s how we often work,

and we imagine you do the same thing. But let’s be clear about one thing:

If you aren’t willing to read PowerShell’s help files, you won’t be effective with

PowerShell. You won’t learn how to use it, you won’t learn how to administer products

like Windows and Exchange with it, and you might as well stick with the GUI.



That’s about as clear as we can be. It’s a blunt statement, but it’s absolutely true.

Imagine trying to figure out Active Directory Users and Computers, or any other

administrative console, without the help of tooltips, menus, and context menus.

Trying to learn and use PowerShell without taking the time to read and understand

the help files is the same thing. It’s like trying to assemble that do-it-yourself

20



www.it-ebooks.info



The help system: how you discover commands



21



furniture from the department store without reading the manual. Your experience

will be frustrating, confusing, and ineffective. Why?

 If you need to perform a task and don’t know what command to use, the help sys-



tem is how you’ll find that command. Not Google or Bing, but the help system.

 If you run a command and get an error, the help system is what will show you

how to properly run the command so you don’t get errors.

 If you want to link multiple commands together to perform some complex task,

the help system is where you’ll find out how each command is able to connect

to others. You don’t need to search for examples on Google or Bing; you need

to learn how to use the commands themselves, so that you can create your own

examples and solutions.

We realize our preaching is a little heavy-handed, but 90 percent of the problems we

see students struggling with in class, and on the job, could be solved if those folks

would find a few minutes to sit back, take some deep breaths, and read the help. And

then read this chapter, which is all about helping folks understand the help they’re

reading.

From here on out, we’ll encourage you to read the help for several reasons:

 Although we’ll be showing you many commands in our examples, we’ll almost



never expose the complete functionality, options, and capabilities of each command. You should read the help for each and every command we show you, so

that you’ll be familiar with the additional things each command can do.

 In the labs, we may give you a hint about which command to use for a task, but

we won’t give you hints about the syntax. You’ll need to use the help system to

discover that syntax on your own in order to complete the labs.

We promise you that mastering the help system is the key to becoming a PowerShell

expert. No, you won’t find every little detail in there, and a lot of super-advanced

material isn’t documented in the help system, but in terms of being an effective dayto-day administrator, you need to master the help system. This book will make that system understandable, and it will teach you the concepts that the help skips over, but

it’ll only do this in conjunction with the built-in help.

Stepping off the soapbox now.



Command vs. cmdlet

PowerShell contains many different types of executable commands. Some are called

cmdlets, some are called functions, others are known as workflows, and so on. Collectively, they’re all commands, and the help system works with all of them. A cmdlet

is something unique to PowerShell, and many of the commands you run will be

cmdlets. But we’ll try to consistently use command whenever we’re talking about the

more general class of executable utility.



www.it-ebooks.info



Xem Thêm
Tải bản đầy đủ (.pdf) (367 trang)

×