0% found this document useful (0 votes)
4 views

Lab Module 6

This document provides lab instructions for writing Windows PowerShell scripts, focusing on executing scripts, using positional parameters, and named parameters. It includes step-by-step exercises for creating and running scripts on a Windows 7 virtual machine, with tasks such as checking execution policies, creating scripts with command-line arguments, and executing them with both positional and named parameters. The lab is designed for system administrators to streamline their command usage and enhance script usability for others.

Uploaded by

shrikantnpar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab Module 6

This document provides lab instructions for writing Windows PowerShell scripts, focusing on executing scripts, using positional parameters, and named parameters. It includes step-by-step exercises for creating and running scripts on a Windows 7 virtual machine, with tasks such as checking execution policies, creating scripts with command-line arguments, and executing them with both positional and named parameters. The lab is designed for system administrators to streamline their command usage and enhance script usability for others.

Uploaded by

shrikantnpar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Lab Instructions: Windows PowerShell® Scripts

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.

� Task 1: Check the execution policy setting


• Check the execution policy to make sure scripts can be executed without needing to be signed.

� Task 2: Create a script


• Create a PowerShell script C:\Script.ps1 that contains a command will get all the running
powershell processes. You can use Notepad to do this then replace the file extension from .txt to
.ps1 once you are finished.

� Task 3: Execute a script from the shell


• Run the script from the PowerShell console calling the script name C:\Script.ps1.

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.

� Task 1: Copy commands into a script


• Create a script named C:\Script2.ps1 containing the following command:

Get-EventLog –LogName Application –Newest 100|Group-Object -


Property InstanceId|Sort-Object –Descending -Property
Count|Select-Object –Property Name,Count -First 5|Format-Table –
AutoSize

� Task 2: Use commands with hard-coded values as parameter values


• Run the script just created.

� Task 3: Identify variable portions of a command


• Review the script and determine what parts of the command could be considered variable
values.
� Task 4: Create positional script parameters
• Create a new script C:\Script3.ps1 that accepts positional script parameters for the four values
underlined below.

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.

Get-EventLog –LogName Application –Newest 100|Group-Object -


Property InstanceId|Sort-Object –Descending -Property
Count|Select-Object –Property Name,Count -First 5|Format-Table –
AutoSize

� Task 5: Execute a script from the shell


• Run the script just created by passing back the same values that were replaced by the
parameters (in the same order).

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.

� Task 1: Copy commands into a script


• Create a script C:\Script4.ps1 that contains the following command:
Get-Counter -Counter "\Processor(_Total)\% Processor Time" -
SampleInterval 2 -MaxSamples 3

� Task 2: Use commands with hard-coded values as parameter values


• Run the script just created.

� Task 3: Identify variable portions of a command


• Review the script and determine what parts of the command could be considered variable
values.

� Task 4: Create named script parameters


• Create a new script C:\Script5.ps1 that accepts named script parameters for the three values
underlined below.
Hint: Create a Param() block with three variables, such as $cpu, $interval, and $max.
Then, use those variables in place of the hard-coded values _Total, 2, and 3.

Get-Counter -Counter "\Processor(_Total)\% Processor Time" -


SampleInterval 2 -MaxSamples 3
Do not run this script now. You will run it in the next task.

� Task 5: Execute a script from the shell


• Run the script that you just created. When you do so, pass in the same values that were just
replaced by parameters by specifying the parameter names and the corresponding values.

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.

You might also like