Simplified syntax, included in Windows PowerShell 3.0, allows users to create some filter commands without applying script blocks. The simplified syntax more approximately matches natural language and is fundamentally valuable with sets of objects piped into Where-Object and ForEach-Object commands. The simplified and standard syntax can enumerate objects in the PowerShell to perform single action and multiple actions, respectively.
In this hands-on lab, learners will learn the enumeration of objects in the Pipeline using Windows PowerShell. First, they will use simplified enumeration syntax in Windows PowerShell to process an array of items and perform only one action. Next, they will use standard enumeration syntax to process an array of items and perform multiple actions. Finally, they will verify that they have completed the commands successfully.
Understand the Scenario
In this virtual lab, you are a system administrator for a company that needs to manage a large number of servers. In preparation for this process, you need to determine which Windows PowerShell commands are required to enumerate through an array of objects on a single server to perform custom actions. To accomplish this task, you will use an Azure resource group that contains an Azure virtual machine named LabVM. The virtual machine configuration could take five to ten minutes and may not be complete when you begin this challenge. You will receive a notification when the automated configuration is complete.
Configure your environment
In this first section of the lab, you will configure the environment necessary for enumerating objects. First, you will display the LabVM virtual machine, then ensure that the virtual machine status is running and ensure that the virtual machine has finished booting up and running before you attempt to connect. Next, you will connect and then download RDP File and connect to the virtual machine. When prompted to enter your credentials, you will sign in to the virtual machine as Student and open Windows PowerShell as an administrator and create a folder named C:\Class and then change to that folder.
Process objects using simplified syntax
There are two terms commonly used to describe the enumeration techniques: Simplified, sometimes referred to as Basic syntax, and Standard, sometimes referred to as Advanced syntax. In this section of the lab, learners will learn how to use simplified syntax to process objects. For this, they will:
- In Windows PowerShell, start Notepad.exe and use ForEach-Object Simplified syntax to expand the list of Modules associated with the Notepad process.
- Use ForEach-Object to call a method to stop the Notepad process and Use ForEach-Object to enumerate only the string values of all service names.
- Use ForEach-Object to output the same list of strings to a file named C:\Class\AllServiceNames.txt. and run a command to perform specific actions as a pipeline operation.
- Use ForEach-Object to call the Stop method for the FontCache, W32Time, and WinRM services and run a command to perform the specific actions as a pipeline operation using the most compact syntax available.
- Use ForEach-Object to call the Start method for the services found.
The ForEach-Object Simplified syntax was introduced in Windows PowerShell 3.0. The simplified syntax makes it easy to enumerate through objects and do only one thing.
Process objects using standard syntax
In this section of the lab, you will learn how to use the standard syntax to process objects. For this, you will:
- In Windows PowerShell, start Notepad.exe and use ForEach-Object Standard syntax for the commands. Use ForEach-Object to expand the list of Modules associated with the Notepad process and to call a method to stop the Notepad process.
- Run a command to perform specific actions as a pipeline operation:
- Run a command to perform the specific actions such as retrieving a list of exe files and displaying the names of files as a pipeline operation.
- Use the Restart-Service command to restart the Bits, Spooler, and WinRM services without using ForEach-Object.
You will notice that no messages are produced when a service is restarted successfully using the Restart-Service command. Next, you will create your command that will display messages when you restart services and again run a command to perform the specific actions as a pipeline operation.
The ForEach-Object Standard syntax has been around since Windows PowerShell 1.0. It provides a powerful tool similar to a traditional foreach loop, but it is more compact and convenient for pipeline usage. You can perform multiple operations inside the script block using the standard syntax.
Lab Summary Conclusion
After completing the “Enumerating Objects in the Pipeline using Windows PowerShell” virtual lab, you will have accomplished the following:
- Process objects using simplified syntax.
- Process objects using standard syntax.