Lab Module 6
Lab Module 6
Module 6
Contents:
Lab: Writing Windows PowerShell Scripts
Exercise 1: Executing Scripts
Exercise 2: Using Positional Script Parameters
Exercise 3: Using Named Script Parameters
Lab: Writing Windows PowerShell Scripts
Estimated time: 30 minutes
You are a system administrator, and have commands that you run regularly at work. To save
time and also to be able to more easily pass around your commonly used commands to others,
you have decided to copy your commands into a script. To make it easier for more junior
administrators or even desktop users to use your script, you’ve added simple command-line
arguments that can be passed dynamically to your script when run.
Lab Setup
For this lab, you will use the available virtual machine environment. Before you begin the lab,
you must:
1. Start the AD virtual machine. You do not need to log on but wait until the boot process is
complete.
2. Start the WINDOWS7 virtual machine, and then log on by using the following credentials:
• Username: NEWHORIZONS\administrator
• Password: Passw0rd
4. Open a Windows PowerShell session as Administrator.
Exercise 1: Executing Scripts
Scenario
To administer your systems, you like to copy commonly used commands into a script so you can
easily invoke them later. The main tasks for this exercise are carried out on WINDOWS7 and are
as follows:
1. Check the execution policy setting.
2. Create a script.
3. Execute a script from the shell.
Results: After this exercise, you will have created and run a simple script that lists all the
“powershell” processes.
Exercise 2: Using Positional Script Parameters
Scenario
With basic scripting firmly understood, you now want to expand a few scripts to make them
easier to use. You want to be able to easily pass parameters to your script so that they can be
more generalized.
The main tasks for this exercise are carried out on WINDOWS7 and are as follows:
1. Copy commands into a script.
2. Use commands with hard-coded values as parameter values.
3. Identify variable portions of a command.
4. Create positional script parameters.
5. Execute a script from the shell.
Hint: Create a script that accepts four parameters, such as $log, $new, $group, and $first. In addition to
defining those parameters in a Param() block, use those parameters in place of the hard-coded values in
the command.
Hint: To run the script, provide the four parameterized values, - such as
“Application,” 100, “InstanceId,” and 5, in the order in which they are defined in the Param() block.
Results: After this exercise, you will have created and run a script that uses positional
parameters instead of hard-coded values.
Exercise 3: Using Named Script Parameters
Scenario
You want to be able to easily pass parameters to your script so that they can be more generalized.
This time, however, you wish to use named parameters as opposed to positional parameters in
your script.
The main tasks for this exercise are carried out on WINDOWS7 and are as follows:
1. Copy commands into a script.
2. Use commands with hard-coded values as parameter values.
3. Identify variable portions of a command.
4. Create named script parameters.
5. Execute a script from the shell.
Hint: You need to provide values for the parameters you just added to the script. If your parameters were
$cpu, $interval, and $max, you run the script by using the -cpu, -interval, and -max parameters, passing
appropriate values to each.
Results: After this exercise, you will have created and run a script that uses named parameters
instead of using hardcoded values.