10325A_01
-
Upload
vu-kim-hieu -
Category
Documents
-
view
214 -
download
0
Transcript of 10325A_01
-
8/10/2019 10325A_01
1/55
Module 1
Fundamentals for
Using MicrosoftWindows PowerShell v2
-
8/10/2019 10325A_01
2/55
Module Overview
Install and customize Windows PowerShell v2
Explain where Windows PowerShell v2 fits into the MicrosoftWindows Management Framework
Create and use PSDrives to manipulate files, folders, andother storage systems
Locate, display, and interpret the built-in help for a cmdlet Extend the shell by using PSSnapins and modules
Locate the cmdlet or cmdlets that canperform a specific task
Use cmdlets within the shell pipeline
Display the properties, methods, andother members of an element
-
8/10/2019 10325A_01
3/55
Lesson 1: Windows PowerShell TechnologyBackground and Overview
Describe how Windows PowerShell can act as both acommand-line shell and a back-end engine for running
administrative commands
List the system requirements for Windows PowerShell v2
Install and customize Windows PowerShell v2
-
8/10/2019 10325A_01
4/55
The Purpose of Windows PowerShell
Windows PowerShell is not a scripting language
At least, it is not onlya scripting language
PowerShell is an engine designed to execute commands thatperform administrative tasks, for example
Creating user accounts
Configuring services
Deleting mailboxes
PowerShell provides a foundation upon which MicrosoftsGUI-based administrative tools can rest
Actions can be accomplished in its command-line console
Actions can also be invoked within GUIs by executing PowerShellcommands in the background
-
8/10/2019 10325A_01
5/55
Installing Windows PowerShell v2
Windows PowerShell is pre-installed by default in WindowsServer 2008 R2 and Windows 7
Windows PowerShell is a Web download for Windows XP,Windows Server 2003, Windows Vista, and Windows Server2008
Windows PowerShell requires the Microsoft .NET Framework
2.0 with Service Pack 1
The Windows PowerShell ISE requires the Microsoft .NETFramework 3.5 with Service Pack 1
Windows PowerShell exposes newer functionality in newer
versions of Windows Older versions of Windows will have a slightly reduced feature set,
because those versions do not contain the underlying functionalitythat a feature requires
-
8/10/2019 10325A_01
6/55
What Else Will You Need?
PowerShell is designed to be extended through snap-ins andmodules
Snap-ins and modules can be provided by Microsoft, or otherthird-party organizations
You can write your own snap-ins and modules
Snap-ins and modules tend to relate to particular products, roles,
features, or technologies
Snap-ins and modules may have system requirements beyondPowerShells own requirements
Snap-ins and modules will tend to have their own documentationand help
Think about modules and snap-ins as similar to the MMCssnap-ins
-
8/10/2019 10325A_01
7/55
Where Is Windows PowerShell Used?
Windows PowerShell is used within a number of Microsoftand third-party products, including:
Microsoft Exchange Server 2007 and later
Microsoft System Center Data Protection Manager
Microsoft System Center Operations Manager
Microsoft System Center Virtual Machine Manager
Microsoft SQL Server 2008 and later
Get-Mailbox | Sort Size | Select first 100 | Move-Mailbox Server2
-
8/10/2019 10325A_01
8/55
Demonstration: Installing the ISE inWindows Server 2008 R2
Learn how to install the Windows PowerShell ISE onWindows Server 2008 R2
-
8/10/2019 10325A_01
9/55
Discussion: Benefits of Having the Shell Everywhere
Will your organization consider a wide deployment ofPowerShell?
What questions or concerns might you have?
-
8/10/2019 10325A_01
10/55
Demonstration: Customizing the Shell
Learn how to customize the appearance of the consolewindow and the PowerShell ISE
-
8/10/2019 10325A_01
11/55
Lesson 2: Windows PowerShell As an InteractiveCommand-Line Shell
Explain the purpose and use of PSDrives
Navigate the Windows file system and other PSDrives byusing cmdlets
Explain the relationship between cmdlets, aliases, andparameters
Locate and interpret built-in help for a cmdlet, includingdetailed help and usage examples
Extend the shell by using a PSSnapin or module
Customize the output of a simple cmdlet
Locate the correct cmdlet given a specific administrativetask
-
8/10/2019 10325A_01
12/55
Discussion: What Shell Commands Do You Know?
What command would you use to perform each of thefollowing tasks:
Change directories
List the files and subdirectories in a directory
Copy a file
Display the contents of a text file
Delete a file
Move a file
Rename a file
Create a new directory
-
8/10/2019 10325A_01
13/55
Navigating the File System
PowerShell recognizes many command names you alreadyknow
Most common file-and-folder management commands from bothcmd.exe and common Unix shells are available
The parameters of the commands are often different
For example, the following command will generate an error:
External commands, such as ipconfig.exe, pathping.exe, and
so on, continue to run exactly as they always have The distinction is that external commands are external
executables, whereas commands such as dir are internal (orintrinsic) commands
dir /s
-
8/10/2019 10325A_01
14/55
Demonstration: Navigating the File System
Review how to navigate and manipulate the file systemwithin the shell
-
8/10/2019 10325A_01
15/55
Hierarchical Storage
The Windows file system is a hierarchical storage system
It consists of containers (folders, directories), which can containleaf elements (files) as well as other containers
The file system is not the only hierarchical storage system inWindows. Also organized in a hierarchy are:
The registry
The certificate store
Active Directory
One of PowerShells key design strategies is to adopt a set oftechniques or processes, and then use them for similar tasks
The commands to navigate the registry are the same as thoseused to navigate the file system
The commands to navigate Active Directory are the same asthose used to navigate the file system
-
8/10/2019 10325A_01
16/55
Demonstration: Navigating Other Forms of Storage
Learn how to navigate other forms of storage usingcommon hierarchical storage commands
-
8/10/2019 10325A_01
17/55
One Set of Commands for Many Forms of Storage
The ability to use a single set of commands to navigate manyforms of storage is made possible by a PowerShell feature
called a PSDrive provider
The PSDrive provider is an adapter that connects to a storagesystem and presents it to PowerShell
This enables common commands, such as cd and dir, to be used
against different kinds of storage Commands are passed to the provider of the drive you are
accessing, and the provider does whatever is necessary toimplement the command
Several providers ship with PowerShell by default:
The file system IIS
The registry SQL Server
The environment variables Active Directory
The certificate store
-
8/10/2019 10325A_01
18/55
Managing PSDrives
The Get-PSDrive cmdlet
Lists all currently-available drives. By default, this includes aFileSystem drive for each logical disk in your computer.
The New-PSDrive cmdlet
Creates a new drive. You must provide the drive name (whichdoes not include the final colon), the name of the provider to
use, and a starting location or path.
The type of location or path you provide will depend upon theprovider you are using for that drive.
The Remove-PSDrive cmdlet
Removes an existing drive. You can even remove the defaultdrives like HKCU: or ENV:, although these will be re-createdwhen you open a new shell window or tab.
-
8/10/2019 10325A_01
19/55
Demonstration: Managing PSDrives
Learn how to manage PSDrives within the shell
-
8/10/2019 10325A_01
20/55
Aliases, Not Commands
Most of the common commands youve seen in the shell areactually PowerShell aliases
Aliases are shorter nicknames for a command
Dir is an alias to the Get-ChildItem command
Cmdlets are built-in commands, which are distinct fromexternal commands such as ipconfig.exe in several ways:
Cmdlets use a consistent verb-singularNoun naming convention.
Cmdlet verbs are taken from a strictly controlled list of verbs.
Cmdlets are written in a .NET Framework language such asVisual Basic or C#, as they are physically packaged in .NET
Framework assemblies with a .DLL extension.
In reality, the shell does not contain any built-in cmdlets. Itloads several snap-ins automatically when you open a new shellwindow or tab.
-
8/10/2019 10325A_01
21/55
Managing Aliases
You are not limited to using the shells pre-defined aliases.
You can create your own aliases You can import and export aliases to share them with colleagues
and coworkers
Get-Alias displays a list of all defined aliases. You could alsorun Dir Alias: to see a directory listing of the ALIAS: drive.
New-Alias creates a new alias. You must provide the aliasname as well as the name of the command for which it willbe a nickname.
Del or Rm can be used to delete aliases from the ALIAS:
drive.
Import-Alias and Export-Alias enables you to import andexport alias lists from and to a file.
-
8/10/2019 10325A_01
22/55
Asking for Help
PowerShell at its core is defined for discoverability
It includes a built-in help system, aliased to Help
The PowerShell help system uses several parameters to reveal
more information about commands
-detailed displays more detailed help
-exampleswill display usage examples
-full displays everything, including detailed help, per-parameter
help, and usage examples
-online opens a Web browser and displays the cmdlet help fromthe Microsoft Web site. This Web site can contain updated orexpanded information that has not yet been released in a productupdate or service pack.
Help commandName
-
8/10/2019 10325A_01
23/55
Demonstration: Asking for Help
Learn how to use the Windows PowerShell help system
-
8/10/2019 10325A_01
24/55
Consistency Is Discoverability
Another of PowerShells discoverability features is in itsconsistent cmdlet names
Their verb-singular Noun construction enables you to often guessthe proper command for accomplishing a task
The Get-Command cmdlet
Makes it easier to locate a cmdlet with a partial cmdlet name, or
a guess
Get-Command accepts wildcards, so you can run Get-Command*proc* to discover cmdlets that have proc as part of theirname
Get-Command has a verb parameter, so you can run Get-
Command verb get to see all Get cmdlets
Get-Command has a noun parameter, so you can run Get-Command noun *event* to see all cmdlets that have event aspart of their noun
-
8/10/2019 10325A_01
25/55
Demonstration: Discovering Commands
Learn how to discover cmdlet names using a variety ofshell discoverability techniques
-
8/10/2019 10325A_01
26/55
Reading Help and Using Parameters
PowerShell cmdlets typically support one or moreparameters
The help file for each cmdlet includes a syntax summary for itsassociated parameters
Some parameters, called switches, dont require a value
Other parameters require a value, with the help file telling youwhat kind of value the parameter expects
Even other parameters have required values, which must be
provided in order for the cmdlet to function
Finally, some parameters are optional
-Recurse
-Exclude
[-Name]
[-Include ]
-
8/10/2019 10325A_01
27/55
Extending the Shell
The default cmdlets of the shell are not the only onesavailable
Microsoft and third-party software developers create additionalcmdlets and PSDrive providers
Additional cmdlets are made available through snap-insand modules
Get-PSSnapin
Add-PSSnapin
Remove-PSSnapin
Import-Module
Remove-Module
Get-Module
Get-PSSnapin registeredGet-Module list
-
8/10/2019 10325A_01
28/55
Demonstration: Extending the Shell
Learn how to extend Windows PowerShell
-
8/10/2019 10325A_01
29/55
Mini-Shells
Software developers can create mini-shells
A mini-shell is essentially a subset of the normal features ofPowerShell
In a mini-shell, a developer may add new cmdlets, block accessto cmdlets, or block access to certain parameters of cmdlets
The purpose of mini-shells is to provide a constrained
environment that helps ensure technical consistency They help ensure that product-specific extensions dont conflict
with other extensions that are already loaded
SQL Server 2008 ships with a mini-shell You cannot load other extensions within its mini-shell
You can load a normal PowerShell window and manually load theSQL Server 2008 extensions
-
8/10/2019 10325A_01
30/55
Progress Check
You have at this point learned most of the major skills youneed to use PowerShell effectively!
You know how to:
Add shell extensions
Get a list of available cmdlets
Locate, display, and work with help
Run cmdlets and see their output
Everything going forward will build upon these simple
foundation skills
Using these skills, you can teach yourself to use any newPowerShell feature or extension
The rest of this course will focus on teaching different cmdletsand the specialized skills for using them effectively
-
8/10/2019 10325A_01
31/55
Cmdlet Output
PowerShell includes a series of built-in defaults that definehow cmdlet output is displayed
Three main cmdlets are useful in customizing cmdletoutput:
Format-Table, which uses the alias Ft
Format-List, which uses the alias Fl
Format-Wide, which uses the alias Fw
Get-Service | Format-ListGet-Process | Fw
-
8/10/2019 10325A_01
32/55
Piping
The act of directing the output from one cmdlet to theinput of another is called piping
You have seen piping before:
Piping is used extensively in PowerShell It is not uncommon to string together a sequence of a half-
dozen cmdlets, all connected with pipes
Data flows from one cmdlet to another, gradually refining andmanipulating that data into what is needed at the time
PowerShell cmdlets are designed to work this way
In fact, Piping is so powerful and important that the entirenext lesson is dedicated to its topic
dir | more
-
8/10/2019 10325A_01
33/55
Demonstration: Customizing Cmdlet Output
Learn how to change the format of cmdlet output
Lab A: Using Windows PowerShell As an
-
8/10/2019 10325A_01
34/55
Lab A: Using Windows PowerShell As anInteractive Command-Line Shell
Exercise 1: Searching for Text Files
Exercise 2: Browsing the Registry
Exercise 3: Discovering Additional Commands and ViewHelp
Exercise 4: Adding Additional Commands to Your Session
Exercise 5: Learning How to Format Output
Estimated time: 50 minutes
Logon information
Virtual machine LON-DC1
Logon user name Contoso\Administrator
Password Pa$$w0rd
-
8/10/2019 10325A_01
35/55
Lab Scenario
You work as a systems administrator.
You have installed Windows Server 2008 R2 and need tolearn how to automate some tasks. You heard thatPowerShell was great for this sort of thing and need tostart learning how to use it.
You have used command-line utilities in the past and want
to get started working with PowerShell as quickly aspossible.
Youre also looking for tips on how you can discover thingsin PowerShell on your own.
-
8/10/2019 10325A_01
36/55
Lab Review
Review Questions
What is the name of the cmdlet with the alias dir?
How many Registry PSDrives are available in PowerShellby default?
How many commands are there with the noun Service?
What command is used to load modules into your currentsession?
How do you format PowerShell output in a list format?
-
8/10/2019 10325A_01
37/55
Lesson 3: Using the Windows PowerShell Pipeline
Explain how the Windows PowerShell pipeline works
Explain the purpose of attributes and actions
View the attributes and actions of a given element
Use the pipeline to pass elements from one cmdlet toanother
Explain how Windows PowerShell takes cmdlets elementoutput and creates a textual display
-
8/10/2019 10325A_01
38/55
The Pipeline
All Shell commands execute in something called a pipeline
You can imaging this to be a literal pipe through whichinformation can flow
Each cmdlet is a pumping station, and the pipeline connectseach pumping station to the next
Each station can take information out of the pipeline, or put
information into the pipeline Cmdlets execute in order from left to right, with the pipe
character (|) providing the delimiter between cmdlets
Get-Process Format-List
Import-CSV New-ADUserEnable-
ADAccount
-
8/10/2019 10325A_01
39/55
The Problem with Text
PowerShell is not the first shell to use a pipeline
Cmd.exe and Unix shells have used pipelines in the past The other shells, however, piped text from one command to
another
Piping text requires that text to be correctly formed as it passesfrom command to command
The following command wont work in cmd.exe:
Other shells required complex text manipulation (a la grep,sed, awk) to prepare one commands output text for pipinginto another commands input parameters
This is not efficient
plist | kill
-
8/10/2019 10325A_01
40/55
Cmdlets Do Not Produce Text
PowerShell discards text output in favor of object output
A PowerShell cmdlet always produces objects, not text
Objects are self-contained, functioning pieces of the OS, aserver technology, or feature
A process is an object
A service is an object A file or folder is an object
A user account, or group, or OU, or domain is an object.
Objects have properties, which describe attributes of the
objectPid Name Image--- ------------------------- -------------092 Notepad notepad.exe098 Windows Paint mspaint.exe112 Calculator calc.exe164 Windows PowerShell powershell.exe
-
8/10/2019 10325A_01
41/55
Terminology
Windows uses terminology that sometimes seems related tosoftware development
Keep in mind that these are just words
Using them does not imply that you need to become aprogrammer in order to use PowerShell
For example:
You have seen the word elementto refer to things that a cmdletplaces in the pipeline
The more formal word for elements is objects
These objects have attributes
The formal term for attributes isproperties
Dont let PowerShells terminology turn you off!
-
8/10/2019 10325A_01
42/55
Properties, or Attributes
Objects typically have one or more properties
These properties describe attributes of the object Some shells might represent these attributes in a list or table
However, as youve already learned, having to manipulatethis text is overly time-consuming
The data structure of an object, however, makes it easy toaccess properties of an object
Pid Name Image--- ------------------------- -------------092 Notepad notepad.exe
098 Windows Paint mspaint.exe112 Calculator calc.exe164 Windows PowerShell powershell.exe
This course will sometimes use the word properties, andother times use the word attributes
The actual words arent important
-
8/10/2019 10325A_01
43/55
Discussion: Object Attributes
What are some of the attributes of a Windows service?
What are some of the attributes of an Active Directoryuser account?
-
8/10/2019 10325A_01
44/55
Object Properties
PowerShell cmdlets produce objects
PowerShell cmdlets also accept objects as input Specific parameters of cmdlets can be designed to look forspecific properties on incoming objects
For example, the Get-Service cmdlet contains a parametercalled computerName
Data for parameter could also be fed from anothercmdlets output, without need for text manipulation
Get-Service computerName SEA-SRV2
Get-Computer
Inventory
Get-Service
-
8/10/2019 10325A_01
45/55
Finding Properties
The Get-Member cmdlet
Will show you the members of an object which has beenpassed into it
Shows the type name, the formal and unique name by whichan object is known
Is a cmdlet you will find yourself using often, to determine the
contents and characteristics of objects
Get-EventLog Security newest 10 | Get-MemberGet-Process | Format-List *
Get-Service | Get-Member | Out-GridView
-
8/10/2019 10325A_01
46/55
Demonstration: Working with Properties
Learn how to find and use object properties in the shell
-
8/10/2019 10325A_01
47/55
Common Parameters
Every cmdlet includes along withits other parameters
These are a short list of parameters that the shellautomatically adds to every object
To view common parameters, use the command:
Two useful common parameters are:
-confirm prompts you to confirm the operation on each objectthat is processed
-whatif does not actually perform the operation, but insteadcreates a list of what operations would have been performed
help about_commonParameters
-
8/10/2019 10325A_01
48/55
Redirecting Output
The shell provides several Out- cmdlets, which are used toredirect command output to a different location
Out-Gridview
Out-Printer
Out-File
Out-Host
Out-Null
Refer to these cmdlets help files for parameters that
further specify output characteristics
-
8/10/2019 10325A_01
49/55
Demonstration: Redirecting Output
Learn how to use output redirection
h f l
-
8/10/2019 10325A_01
50/55
The Power of a Pipeline
PowerShell passes objects in the pipeline until the veryend of the pipeline
At the end of the pipeline, whatever objects remain are passedto a special cmdlet called Out-Default
This passing to Out-Default is automatic
Out-Default passes objects to Out-Host by default , which
produces text that you read on the screen
Because objects are not converted to text until the laststep, you can continue to use their properties until thevery end
Get-Process Format-List[Out-
Default]
C i N M k A i
-
8/10/2019 10325A_01
51/55
Caution: Never Make Assumptions
Dont make assumptions about property names or thevalues they contain
For example, dont assume that a DriveLetter property existson an object when the actual property is in fact DeviceID
Always use Get-Member to verify property names, and
pipe objects to Format-List * to verify the contents ofthose properties
This process takes very little time to verify, but it will save youa great deal of time and frustration
L b B U i th Wi d P Sh ll Pi li
-
8/10/2019 10325A_01
52/55
Lab B: Using the Windows PowerShell Pipeline
Exercise 1: Stopping and Restarting a Windows Service
Exercise 2: Exploring Objects Returned by PowerShellCommands
Exercise 3: Processing PowerShell Output
Estimated time: 30 minutes
Logon information
Virtual machine LON-DC1 LON-CLI1
Logon user name Contoso\Administrator Contoso\Administrator
Password Pa$$w0rd Pa$$w0rd
L b S i
-
8/10/2019 10325A_01
53/55
Lab Scenario
You are server administrator in an enterprise. You have acomputer on which you want to make administrative
changes, such as working with Services.
Working with those services using the GUI addsadministrative overhead, so you wish to use WindowsPowerShell to accomplish the necessary tasks.
To better understand how Windows PowerShell willinteract with those services, you also need to explore themembers, properties, and output options for the necessarycommands.
L b R i
-
8/10/2019 10325A_01
54/55
Lab Review
Review Questions
How can you use PowerShell to see what a command willdo before you actually do it?
What are the two most common categories of members onobjects and what are they used for?
What command is used to show PowerShell data in a grid?
M d l R i d T k
-
8/10/2019 10325A_01
55/55
Module Review and Takeaways
Review Questions
How many optional parameters does the Ps commandhave?
What parameter, if any, of Get-EventLog will make thecmdlet connect to a remote computer?
What cmdlet or cmdlets exist to stop or start services?
How many properties does a Service object (which isproduced by using Get-Service) have?
What module might add cmdlets for managing ActiveDirectory?