Lab 12. Practicing Advanced Techniques
Lab 12. Practicing Advanced Techniques
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.
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:
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
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.
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:
$octets = $ip.Split(".")
81 / 85
PowerShell Basics - Exercises
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).
2. Filter the information from Get-Volume to include only volumes on hard drives.
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.
7. Add a fourth column for Status. If the value in the Free column is less than 10 GB, show a status of
Low.
82 / 85
PowerShell Basics - Exercises
Solution to task 1
Create a script with the editor of your choice with the following content:
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
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.
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.
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
)
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