You are on page 1of 5

18/5/2017 Lab 

Answer Key: Module 3: Understanding How the Pipeline Works

Lab Answer Key: Module 3: Understanding How the
Pipeline Works

Lab: Working with Pipeline Parameter Binding

Exercise 1: Predicting Pipeline Behavior

Task 1: Review existing commands

For these tasks, you may run individual commands and Get­Member to see what kinds of
objects the commands produce. You may also view the Help for any of these commands.
However, do not run the whole command shown. If you do run the whole command, it may
produce an error. The error does not mean the command is written incorrectly.

1. This command is intended to list the services that are running on every computer in the
domain:

Get-ADComputer –Filter * | Get-Service –Name *

Will the command achieve the goal?

No. Get­Service does not accept ADComputer objects from the pipeline.

2. This command is intended to list the services that are running on every computer in the
domain:

Get-ADComputer –Filter * | Select @{n='ComputerName';e=


{$PSItem.Name}} | Get-Service
–Name *

Will the command achieve the goal?

https://skillpipe.com/en­GB/Book/BookPrintView/fa8015b2­74e9­4be0­a2a2­2064dd96fa54?ChapterNumber=17&AnnotationFilterOwn=true 1/5
18/5/2017 Lab Answer Key: Module 3: Understanding How the Pipeline Works

Yes. The ­­­ ComputerName parameter accepts pipeline input by using
ByPropertyName.

3. This command is intended to query an object from every computer in the domain:

Get-ADComputer –Filter * |
Select @{n='ComputerName';e={$PSItem.Name}} | Get-WmiObject –
Class Win32_BIOS

Will the command achieve the goal?

No. The ­­­ ComputerName parameter of Get­WmiObject does not accept pipeline
input.

4. The file Names.txt lists one computer name per line.

This command is intended to list the services that are running on every computer that is
listed in Names.txt.

Get-Content Names.txt |
Get-Service

Will the command achieve the goal?

No. Pipeline input of the type String will attach to the ­­­ Name parameter of Get­
Service by using ByValue. The ­­­ ComputerName parameter does not accept pipeline
input by using ByValue.

5. The file Names.txt lists one computer name per line.

This command is intended to list the services that are running on every computer that is
listed in Names.txt.

Get-Service –ComputerName (Get-Content Names.txt)

Will the command achieve the goal?

https://skillpipe.com/en­GB/Book/BookPrintView/fa8015b2­74e9­4be0­a2a2­2064dd96fa54?ChapterNumber=17&AnnotationFilterOwn=true 2/5
18/5/2017 Lab Answer Key: Module 3: Understanding How the Pipeline Works

Yes. Get­Content produces objects of the type String, and the ­­­ ComputerName
parameter can accept those objects.

6. This command is intended to list the services that are running on every computer in the
domain:

Get-Service –ComputerName (Get-ADComputer –Filter *)

Will the command achieve the goal?

No. The ­­­ ComputerName parameter cannot accept objects of the type ADComputer.

7. This command is intended to list the Security event log entries from every computer in
the domain:

Get-EventLog –LogName Security –ComputerName (Get-ADComputer –


Filter * |
Select –Expand Name)

Will the command achieve the goal?

Yes. The parenthetical command produces objects of the type String that are computer
names. Those are accepted by the ­­­ ComputerName parameter.

Task 2: Write new commands that perform specified tasks

In each of these tasks, you are asked to write a command that achieves a specified goal. Do
not run these commands. Write them on paper.

You may run individual commands and pipe their output to Get­Member to see what objects
those commands produce. You may also read the Help for any command.

1. Write a command displays the most recent 50 System event log entries from each
computer in the domain.

https://skillpipe.com/en­GB/Book/BookPrintView/fa8015b2­74e9­4be0­a2a2­2064dd96fa54?ChapterNumber=17&AnnotationFilterOwn=true 3/5
18/5/2017 Lab Answer Key: Module 3: Understanding How the Pipeline Works

Because Get­EventLog does not accept pipeline input for its ­­­ ComputerName
parameter, you have to write this command:

Get-EventLog –LogName System –Newest 50 –ComputerName (Get-


ADComputer –filter * |
Select-Object –ExpandProperty Name)

2. You have a text file that is named Names.txt that contains one computer name per line.
Write a command that uses Restart­Computer to restart each computer that is listed in
the file. Do not use a parenthetical command.

Because the ­­­ ComputerName parameter of Restart­Computer accepts pipeline input
by using ByValue, the following command will achieve the goal:

Get-Content Names.txt |
Restart-Computer

3. You have a file that is named Names.txt that contains one computer name per line. Write
a command that uses Test­Connection to test the connectivity to each computer that is
listed in the file.

The ­­­ ComputerName parameter of Test­Connection accepts pipeline input only by
using ByPropertyName. The objects produced by Get­Content do not have a property
named ComputerName, nor do they have an existing property that can be renamed to
ComputerName. Therefore, you must use a parenthetical command:

Test-Connection –ComputerName (Get-Content Names.txt)

4. Write a command that uses Set­Service to set the start type of the WinRM service to
Auto on every computer in the domain. Do not use a parenthetical command.

Because the ­­­ ComputerName parameter of Set­Service accepts pipeline input by using
ByPropertyName, you can write the following command:

https://skillpipe.com/en­GB/Book/BookPrintView/fa8015b2­74e9­4be0­a2a2­2064dd96fa54?ChapterNumber=17&AnnotationFilterOwn=true 4/5
18/5/2017 Lab Answer Key: Module 3: Understanding How the Pipeline Works

Get-ADComputer –filter * |
Select-Object @{n='ComputerName';e={$PSItem.Name}} |
Set-Service –Name WinRM –StartupType Auto

You must use Select­Object because the ADComputer objects have a Name property, not a
ComputerName property.

Task 3: To prepare for the next module

When you have finished the lab, revert the virtual machines to their initial state. To do this,
perform the following steps:

1. On the host computer, start Hyper­V Manager.

2. In the Virtual Machines list, right click 10961B­LON­DC1, and then click Revert.

3. In the Revert Virtual Machine dialog box, click Revert.

4. Repeat steps 2 and 3 for 10961B­LON­CL1.

Results: After completing this exercise, you will have reviewed and written several
Windows PowerShell ™ commands.

https://skillpipe.com/en­GB/Book/BookPrintView/fa8015b2­74e9­4be0­a2a2­2064dd96fa54?ChapterNumber=17&AnnotationFilterOwn=true 5/5

You might also like