0% found this document useful (0 votes)
18 views63 pages

Working With Pipeline

Module 2 covers working with the PowerShell pipeline, including understanding its structure, running commands, and managing object output. It details selecting, sorting, measuring, converting, exporting, and importing objects, as well as filtering and enumerating them. The module includes demonstrations and lab exercises to reinforce learning and practical application of these concepts.

Uploaded by

suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views63 pages

Working With Pipeline

Module 2 covers working with the PowerShell pipeline, including understanding its structure, running commands, and managing object output. It details selecting, sorting, measuring, converting, exporting, and importing objects, as well as filtering and enumerating them. The module includes demonstrations and lab exercises to reinforce learning and practical application of these concepts.

Uploaded by

suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 63

Module 2

Working with the Pipeline


Module Overview

Understanding the Pipeline


Selecting, Sorting, and Measuring Objects
Converting, Exporting, and Importing Objects
Filtering Objects Out of the Pipeline
• Enumerating Objects in the Pipeline
Lesson 1: Understanding the Pipeline

What Is the Pipeline?


Running Commands in the Pipeline
Pipeline Output
Discovering Object Members
Demonstration: Pipeline Basics
• When the Pipeline Contains Mixed Output
What Is the Pipeline?

• Windows PowerShell runs commands in a


pipeline
• In the console, each complete command
line is a pipeline
• Pipelines can contain one or more
commands, with multiple commands
separated by a vertical pipe character (|)
• Commands execute from left to right, with
the output of each command being piped
(passed) to the command after it
• The output of the last command in the
pipeline is what appears on your screen
Running Commands in the Pipeline

• Get-Service is a single-command pipeline.

• Multicommand pipelines look similar:


Get-Service | Out-File ServiceList.txt

• The preceding pipeline produces no output


on the screen. Why?
Pipeline Output

• Windows PowerShell commands usually


produce objects as their output
• Think of these as a table of data in
memory
Property
Collection

Name Status DisplayName


Object WinRM Running Windows
Remote
Management
VDS Running Virtual Disk
Discovering Object Members

• Object members include:


• Properties
• Methods
• Events

• Run a command that produces an object,


and pipe that object to Get-Member to
see a list of members
• Get-Member is a discovery tool, similar to
Help, that can help you learn to use the
shell
Demonstration: Pipeline Basics

In this demonstration, you will see how to


run commands in the pipeline and how to
use Get-Member
• Running multicommand pipelines
• Using Get-Member
When the Pipeline Contains Mixed Output

• Most commands produce just one kind of


object
• Some commands produce more than one
kind
• When working with more than one kind of
object in the pipeline, use extra caution
• Not every kind of object has the same members
• Output might not be what you originally
expected

• Get-Member can handle multiple kinds of


objects and will display each kind in a
separate list
Lesson 2: Selecting, Sorting, and
Measuring Objects

Sorting Objects on a Property


Demonstration: Sorting Objects
Measuring Objects
Demonstration: Measuring Objects
Selecting a Subset of Objects
Selecting Properties of Objects
Demonstration: Selecting Objects
Creating Calculated Properties
• Demonstration: Creating Calculated
Properties
Sorting Objects on a Property

• Commands determine their own default


sort order
• Sort-Object can re-sort objects in the
pipeline

• Example of use:
• Get-Service | Sort-Object Name -
Descending
Example: Sort-Object

Get-Service | Sort-Object –Property


Name

Get-Process | Sort Name,ID

Get-Process | Sort VM -Descending


Demonstration: Sorting Objects

In this demonstration, you will see how to


run commands in the pipeline and how to
use Get-Member
• Sort objects on a property
• Sort on multiple properties
• Specify sort options
Measuring Objects

• Measure-Object accepts a collection of objects


and counts them

• Add –Property to specify a single numeric


property, and then add:
• -Average to calculate an average
• -Minimum to display the smallest value
• -Maximum to display the largest value
• -Sum to display the sum

• Output is a measurement object, not whatever


you piped in
Example: Measure-Object

Get-Service | Measure-Object

Get-Process |
Measure-Object –Prop PM –Sum -Average
Demonstration: Measuring Objects

In this demonstration, you will see how to


use Measure-Object
• Count objects
• Calculate aggregate values
Selecting a Subset of Objects

• This is one of two uses for Select-Object

• Use parameters to select the specified


number of rows from the beginning or end
of the piped-in collection:
• -First for the beginning
• -Last for the end
• -Skip to skip a number of rows before selecting

• You cannot specify any criteria for choosing


specific rows
Example: Select-Object

Get-Service |
Sort-Object -Property Status |
Select-Object –First 10

Get-Process |
Sort VM –Descending |
Select –First 10
Selecting Properties of Objects

• This is the second use of Select-Object

• Use the –Property parameter to specify a


comma-separated list of properties
(wildcards are accepted) to include
• Can be combined with –First, -Last, and –
Skip to select a subset of rows
Example: Select-Object (2)

Get-Service |
Select-Object –Property Name,Status

Get-Process |
Sort PM –Descending |
Select –Property Name,ID,PM,VM –First
10
Demonstration: Selecting Objects

In this demonstration, you will see various


ways to use Select-Object
• Selecting subsets of a collection
• Selecting specific properties
Creating Calculated Properties

• Calculated (custom) properties let you


choose the output label and contents
• Each calculated property works like a
single regular property in the property list
accepted by Select-Object
• Calculated properties are created by using
a specific syntax
• Label defines the property name
• Expression defines the property contents
• Within the expression, use $PSItem (or $_) to
refer to the piped-in object
Calculated Property Hash Table

Hash table

Label key Label string


value

@{

n='VirtualMemory';
e={ $PSItem.VM } Semicolon
} Expression Expression
key script block
Formatting Tips

• Use shortcuts for specific memory


amounts:
• KB kilobyte
• MB megabyte
• GB gigabyte
• TB terabyte
• PB petabyte

• Use the –f format operator to format


numbers with a specified number of
decimal places
Example: Calculated Properties

Get-Volume |
Select-Object –Property DriveLetter,
@{
n='Size(GB)';
e={'{0:N2}' -f ($PSItem.Size / 1GB)}
},
@{
n='FreeSpace(GB)';
e={'{0:N2}' -f ($PSItem.SizeRemaining / 1GB)}
}
Demonstration: Creating Calculated
Properties

In this demonstration, you will see how to


use Select-Object to create calculated
properties
• Create output that has calculated properties
Lab A: Using the Pipeline

• Exercise 1: Selecting and Sorting Data

Logon Information

Virtual Machines: 10961B-LON-DC1, 10961B-LON-C


User Name : ADATUM\Administrator
Password: Pa$$w0rd

Estimated Time: 30 minutes


Lab Scenario

You must produce several basic


management reports that include specified
information about the computers in your
environment.
Lab Review

• Suppose that you wanted to produce output


that included all of an object’s properties
except one. What would be the most
efficient way to do that?
Lesson 3: Converting, Exporting, and
Importing Objects

Converting Objects to Another Form


Piping Output to a File
Demonstration: Converting and Exporting
Objects
Importing Data
• Demonstration: Importing Objects
Converting Objects to Another Form

• Conversion changes the form of data and


renders it more difficult to manipulate
• Supported formats include CSV, HTML, and
XML

• Two verbs:
• ConvertTo changes the form of the data
• Export changes the form of the data and writes
it to external storage

• Get-Command –Verb ConvertTo,Export


Piping Output to a File

• Out-File writes whatever is in the pipeline to a


text file
• The text file will be formatted exactly as the same
data would have appeared on the screen―there is
no conversion to another form
• Unless the data has been converted to another
form, the resulting text file will usually be suitable
only for viewing by a person

• As you start to build more complex commands,


you will need to keep track of what the pipeline
contains at each step
The Pipeline's Contents Can Change

Get-Service |
Sort-Object –Property Status |
Select-Object –Property Name,Status |
ConvertTo-CSV |
Out-File –FilePath ServiceList.csv
Demonstration: Converting and Exporting
Objects

In this demonstration, you will see different


ways to convert and export data
• Convert data to different forms
• Export the data to external storage
Importing Data

• Import commands are the opposite of


Export commands
• Two-step process to both read the raw data
and convert it into usable objects

• Import-CSV, Import-CliXML, and so on

• The reconstructed objects may lack some


of the members of the original objects,
depending on the capabilities of the data
format used
Importing vs. Reading

• Get-Content just reads the raw content of


a file― it does not interpret the data and
construct objects like an Import command
would do

• ConvertFrom commands can accept raw


data and interpret them, constructing
usable objects

• Import commands are basically a


combination of Get-Content and the
appropriate ConvertFrom command
Demonstration: Importing Objects

In this demonstration, you will see different


ways to read and import data
• Export and import some data
• Compare to reading the raw data
Lab B: Converting, Exporting, and
Importing Objects

Exercise 1: Converting Objects


• Exercise 2: Importing and Exporting Objects

Logon Information

Virtual Machines: 10961B-LON-DC1, 10961B-LON-C


User Name: ADATUM\Administrator
Password: Pa$$w0rd

Estimated Time: 30 minutes


Lab Scenario

You must convert management information


into different formats for use by other
people and processes in your environment.
Lab Review

Could you use ConvertTo-CSV or Export-CSV


to create a file that was delimited by using a
character other than a comma? For example,
could you create a tab-delimited file?
• The HTML produced by ConvertTo-HTML
looks very plain. The HTML standard offers a
way to specify visual styles for an HTML
document. This is known as a cascading
style sheet (CSS). Does the command offer
a way to attach a CSS?
Lesson 4: Filtering Objects Out of the
Pipeline

Comparison Operators
Basic Filtering Syntax
Advanced Filtering Syntax
Demonstration: Filtering
• Optimizing Filtering Performance
Comparison Operators

Comparison Case-InSensitive Case-sensitive


Equality -eq -ceq
Inequality -ne -cne
Greater than -gt -cgt
Less than -lt -clt
Greater than or equal -ge -cge
to
Less than or equal to -le -cle
Wildcard equality -like -clike
Basic Filtering Syntax

• The Where-Object command provides


filtering
• Basic syntax:

Get-Service |
Where Status –eq Running

Get-Process |
Where CPU –gt 20
Limitations of the Basic Syntax

• Supports only a single comparison―you


cannot compare two things
• Does not support property
dereferencing―you can refer to only direct
properties of the object piped into the
command

• Will not work:


Get-Service | Where Name.Length –gt 5
Advanced Filtering Syntax

• Supports multiple conditions and has no


restrictions on what kinds of expressions
you can use.
• Requires a filter script that contains your
filtering criteria. The filter script must
evaluate to either True or False.
• Inside the filter script, use $PSItem or $_
to refer to whatever object was piped into
the command.
Example: Advanced Filtering

Get-Service |
Where-Object –Filter {$PSItem.Status –eq
'Running' }

Get-Service | Where { $_.Status –eq 'Running' }

Get-Service | ? { $PSItem.Status –eq 'Running' }


Adding Multiple Criteria

• Use Boolean operators –and and –or to


combine multiple comparisons into a single
expression:

Get-Volume | Where-Object –Filter {


$PSItem.HealthStatus –ne 'Healthy'
-or
$PSItem.SizeRemaining –lt 100MB
}
Demonstration: Filtering

In this demonstration, you will see various


ways to filter objects out of the pipeline
• Filter using the basic syntax
• Filter using the advanced syntax
• Filter using multiple criteria
Optimizing Filtering Performance

• Move filtering as close to the beginning of


the command line as possible to improve
performance.
• Some commands have parameters that
can do some filtering for you. When
possible, use those parameters instead of
Where-Object.
Lab C: Filtering Objects

• Exercise 1: Filtering Objects

Logon Information

Virtual Machines: 10961B-LON-DC1, 10961B-LON-C


User Name: ADATUM\Administrator
Password: Pa$$w0rd

Estimated Time: 30 minutes


Lab Scenario

You have to retrieve management


information about the computers in your
environment. You want the output of your
commands to include only specified
information and objects.
Lab Review

Do you prefer the basic or advanced syntax


of Where-Object?
What is the difference between Select-Object
and Where-Object?
• In the first task of this lab, were you able to
achieve the goal without using the Where-
Object command?
Lesson 5: Enumerating Objects in the
Pipeline

The Purpose of Enumeration


Basic Enumeration Syntax
Demonstration: Basic Enumeration
Advanced Enumeration Syntax
• Demonstration: Advanced Enumeration
The Purpose of Enumeration

• Take a collection of objects…


• …and execute some action on each one of them,
one at a time

• Not necessary when the shell has a command that


can perform the action you need
• Useful when an object has a method that will do
what you want but the shell does not offer an
equivalent command

• The command is ForEach-Object (aliases


ForEach and %)
Basic Enumeration Syntax

Get-ChildItem –Path C:\Example –File


|
ForEach-Object –MemberType Encrypt

Get-ChildItem –Path C:\Example –File


|
ForEach Encrypt

Get-ChildItem –Path C:\Example –File


|
% –MemberType Encrypt
Limitations of the Basic Syntax

• Accesses only a single member (method or


property) of the objects that were piped
into the command

• Cannot:
• Execute commands or code
• Evaluate expressions
• Make logical decisions
Demonstration: Basic Enumeration

In this demonstration, you will see how to


use the basic enumeration syntax to
enumerate several objects in a collection
• Perform basic enumeration on several objects
Advanced Enumeration Syntax

• Allows you to perform any task by writing


commands in a script block
• Use $PSItem or $_ to reference the
objects that were piped into the command

Get-ChildItem C:\Test –File |


ForEach-Object { $PSItem.Encrypt() }

• Additional parameters allow you to specify


actions to take before and after the
collection of objects is processed
Demonstration: Advanced Enumeration

In this demonstration, you will see two ways


to use the advanced enumeration syntax
• Modify several registry values
• Specify pre-processing and post-processing
actions
Lab D: Enumerating Objects

• Exercise 1: Enumerating Objects

Logon Information

Virtual Machines: 10961B-LON-DC1, 10961B-LON-C


User Name : ADATUM\Administrator
Password: Pa$$w0rd

Estimated Time: 30 minutes


Lab Scenario

You are asked to complete several


management tasks by using Windows
PowerShell. These tasks require you to
perform actions on multiple objects.
Lab Review

• Do you prefer the basic or advanced syntax


of ForEach-Object?
Module Review and Takeaways

Review Question(s)
Real-world Issues and Scenarios
Best Practice
• Common Issues and Troubleshooting Tips

You might also like