Scenario
You are creating and troubleshooting Windows PowerShell commands. You have to predict and control how the shell will pass data from one command to another so that the commands run correctly.
Objectives
After completing this lab, students will be able to:
Predict the behavior of commands in the pipeline
Write commands that comply with specified pipeline behavior
Lab Setup
Estimated Time: 45 minutes
Virtual Machines: 10961B-LON-DC1, 10961B-LON-CL1 User Name: ADATUM\Administrator
Password: Pa$$w0rd
The changes that you make during this lab will be lost if you revert your virtual machines at another time during class.
For this lab, you will use the available virtual machine environment. Before you begin the lab, you must follow these steps:
1. On the host computer, move the pointer over the bottom left corner of the taskbar, click Start, and then click Hyper-V Manager on the Start screen.
2. In Hyper-V® Manager, click 10961B-LON-DC1, and in the Actions pane, click Start.
3. In the Actions pane, click Connect. Wait until the virtual machine starts.
4. Sign in by using the following credentials:
o User name: Administrator o Password: Pa$$w0rd o Domain: ADATUM
5. Repeat steps 2 through 4 for 10961B-LON-CL1.
6. This lab can be performed with the use of the 10961B-LON_CL1 virtual machine.
Exercise 1: Predicting Pipeline Behavior
Scenario
You have to review several Windows PowerShell commands and determine whether they will work. Some commands use pipeline input, but other commands do not. Without running the commands in their entirety, you have to decide whether they will achieve the stated goal.
You also have to write several Windows PowerShell commands that will achieve stated goals. You must not run these commands in the shell. Instead, write them on paper.
The main tasks for this exercise are as follows:
1. Review existing commands
MCT USE ONL Y. STUDENT USE PROHIBITED
03-12 Understanding How the Pipeline Works
2. Write new commands that perform specified tasks 3. To prepare for the next module
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?
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?
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?
4. The file Names.txt lists one computer name per line.
5. 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?
6. 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?
7. 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?
8. 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?
MCT USE ONL Y. STUDENT USE PROHIBITED
10961B: Automating Administration with Windows PowerShell 03-13
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 that uses Get-EventLog to display the most recent 50 System event log entries from each computer in the domain.
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.
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.
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.
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.
Question: Why do some commands accept pipeline input for a parameter such as – ComputerName, but other commands do not?
Question: Do you ever have to rely on pipeline input? Could you just rely on parenthetical commands?
MCT USE ONL Y. STUDENT USE PROHIBITED
03-14 Understanding How the Pipeline Works
Module Review and Takeaways
Best Practice: It is easy to start using Windows PowerShell and not think about what the shell is doing for you. Always take a moment to examine each command that you write, and think about what the shell will do. Think about what objects will be produced by each command, and how those will be passed to the next command.
Review Question(s)
Question: Because pipeline input binding is handled invisibly by the shell, it can be difficult to troubleshoot. Are there any tools that can help you troubleshoot pipeline input?
Real-world Issues and Scenarios
Sometimes, command authors do not realize how useful and important pipeline input can be, and they do not create their parameters to accept pipeline input. All that you can do in those cases is submit a request to the command author to support pipeline input in a future release.
MCT USE ONL Y. STUDENT USE PROHIBITED
04-1