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

Lab 12. Practicing Advanced Techniques

The document provides exercises for practicing PowerShell skills. The first exercise has the user create a profile script to populate server names into a variable to reduce workload. The second exercise has the user validate IP address input by checking the pattern and octet values. The third exercise reports disk information in an aligned column output. The fourth exercise creates a Get-NTFS function to retrieve and display file/folder permission summaries or details.

Uploaded by

Yoongi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lab 12. Practicing Advanced Techniques

The document provides exercises for practicing PowerShell skills. The first exercise has the user create a profile script to populate server names into a variable to reduce workload. The second exercise has the user validate IP address input by checking the pattern and octet values. The third exercise reports disk information in an aligned column output. The fourth exercise creates a Get-NTFS function to retrieve and display file/folder permission summaries or details.

Uploaded by

Yoongi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

PowerShell Basics - Exercises

Module 12: Practicing advanced techniques


Exercise 1: Creating a profile script

Scenario

You perform many server administration tasks from a Windows PowerShell prompt. As part of your
administration, you often need to place all servers in a variable. To reduce your workload, you have decided
to use a profile script to automatically populate $servers each time a user opens the PowerShell prompt.
Your have 12 servers and they are strategically named server1, server2, ..., server12.

Task 1: Create a profile script

1. Create a profile script that populates a variable named $servers with the name of the 12 servers.
Check the variable $profile for the correct profile location and name.

2. Open a Windows PowerShell prompt and verify that the profile script populated the variables
properly.

78 / 85
PowerShell Basics - Exercises

Solution to task 1

1. Open the editor of your choice and create a script with this content:

$servers = 1..12 | ForEach { 'wks' + $_ }

In the PowerShell console check the content of the $profile variable and save the script in that folder and
with that name.

2. Close the PowerShell console, reopen it and check that the $servers variable has the right content.

79 / 85
PowerShell Basics - Exercises

Exercise 2: Verifying the validity of an IP address

Scenario

You have several scripts that obtain an IP address from user input. Some script users have been
complaining that when they make a typing error, the script generates an error and stops, instead of
allowing them to fix it. To improve your scripts, you are developing code that will verify the validity of an IP
address based on its pattern and the values in each octet.

Task 1: Verify the validity of an IP address

1. Identify a regular expression that can verify the pattern of an IP address.

2. Identify a method for dividing the IP address into octets that you can evaluate to be between 0 and
255.

3. Create a script that requests an IP address from a user and then verifies the pattern and the value of
each octet. The validity of the pattern and each octet should display on the screen.

80 / 85
PowerShell Basics - Exercises

Solution to task 1

Create a script with the editor of your choice with the following content:

$ip = Read-Host "Enter an IP address"

If ($ip -match "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$") {


Write-Host "IP pattern is valid"
} Else {
Write-Host "IP pattern is not valid"
}

$octets = $ip.Split(".")

Foreach ($o in $octets) {


[int]$value = $o
If ($value -ge 0 -and $value -le 255) {
Write-Host "Octet $value is valid"
} Else {
Write-Host "Octet $value is not valid"
}
}

81 / 85
PowerShell Basics - Exercises

Exercise 3: Reporting disk information

Scenario

Over the past few months, there have been several instances where servers have experienced errors
because of low amounts of free disk space. You want to create a script that queries disk space on the
computer and writes the information to the screen in aligned columns. The disk space should display in
gigabytes (GB).

Task 1: Report disk information

1. Create a script that uses Get-Volume to query disk information.

2. Filter the information from Get-Volume to include only volumes on hard drives.

3. Sort the volumes by drive letter to simplify reading.

4. Display a header for the columns Drive, Size, and Free. Choose an appropriate alignment for each
column.

5. Display information for each volume, while using the same column alignment as the header. The Size
and Free columns should be in GB and only include two decimal places.

6. Verify that the script is working.

7. Add a fourth column for Status. If the value in the Free column is less than 10 GB, show a status of
Low.

8. Verify that the script is working.

82 / 85
PowerShell Basics - Exercises

Solution to task 1

Create a script with the editor of your choice with the following content:

$drives = Get-Volume | Where-Object DriveType -eq "Fixed" | Sort-Object -


Property DriveLetter

Write-Host "Drives on $ComputerName"


"{0,-5} {1,10} {2,15} {3,-10}" -f "Drive","Size","Free","Status"

Foreach($drive in $drives) {
If ($drive.SizeRemaining -le 10GB) {
$status = "Low"
} Else {
$status = "OK"
}
"{0,-5} {1,10:n2} {2,15:n2} {3,-10}" -f $drive.DriveLetter,
($drive.Size/1GB),($drive.SizeRemaining/1GB),$status
}

83 / 85
PowerShell Basics - Exercises

Exercise 4: Querying NTFS permissions

Scenario

You would like to speed up the process of viewing NTFS permissions on files and folders from a PowerShell
prompt. To support this, you are creating a module that can show a summary of permissions or detailed
NTFS permissions that Get-Acl retrieves.

Task 1: Query NTFS permissions

1. Create a script with a function named Get-NTFS that accepts a -Path parameter and a -Full
parameter. The -Full parameter will indicate when detailed NTFS permissions should be displayed.

2. Identify how to display summary access rules from an ACL.

3. Identify how to display detailed access rules from an ACL.

4. Verify that you can query summary access rules and detailed access rules by using the function.

5. Add functionality to request a folder path when the user does not provide it as a parameter.

6. Convert the script to a module and copy it to the required location for a module.

7. Verify that you can call the function for summary and detailed access rules.

84 / 85
PowerShell Basics - Exercises

Solution to task 1

Create a script with the editor of your choice with the following content:

function Get-NTFS {
param(
[string]$Path=(Read-Host "Enter file or folder to get permissions
for"),
[switch]$Full
)

$acl = Get-Acl $Path

If ($Full -eq $true) {


$acl.Access
} Else {
$acl.AccessToString
}

To be used as a module the script must be renamed to use the .psm1 file extension. You also need to copy
the file to a subfolder in Documents\WindowsPowerShell\Modules (or
Documents\PowerShell\Modules for PowerShell 7). The subfolder must have the same name as the file.

85 / 85

You might also like