Scenario.
You have written a Windows PowerShell script that may encounter errors when it is run in your environment.
You have to change the script to handle and log those errors.
Exercise one.
Add error handling to a function.
Task one Add Error handling to a function.
Note These instructions provide a starting point that does not require you to have completed any prior
lab tasks.
The starting point includes all tasks up to this one.
If you are working on your own script, you can compare it to the starting point before you attempt
this task.
One.
Open the windows PowerShell.
ISC as administrator two on the file menu.
Click open.
Three.
Navigate an open exercise.
One.
Task 1.14.
On the file menu.
Click Save as five in the file name box type C Colon backslash users backslash administrator dotted
date and backslash documents backslash windows PowerShell backslash module's backslash my tools backslash
my tools dot psm one and then press enter.
Six.
Click yes in the confirm save as dialog box.
Seven.
Use the mouse to highlight lines 25 through 29 in the script.
Eight.
Press Tab nine.
Online.
24.
Add the following.
Try ten.
Online 30.
Add the following catch.
Right.
Verbose error querying dollar computer name 11 Modify Line 25 to read as follows get scam instance class
name when 32 underscore logical disk filter drive type equals dollar drive type computer name.
Dollar computer name.
Error action.
Stop.
12 Press control plus s to save the file.
Task to test the function.
Note These instructions provide a starting point that does not require you to have completed any prior
lab tasks.
The starting point includes all tasks up to this one.
If you are working on your own script, you can compare it to the starting point before you attempt
this task.
One.
Open the Windows PowerShell ISC as administrator two on the file menu.
Click Open.
Three Navigate an open exercise.
One Task 2.14 on the file menu.
Click Save As.
File in the file name box type c colon backslash users backslash administrator a datum backslash documents
backslash windows powershell backslash module's backslash my tool's backslash my tools dot psm one in
press enter six in the confirm save as dialog box click yes seven press control plus d to switch to
the console pane eight take the following and press enter because the module may not be loaded.
This command may produce an error that you can safely ignore.
Remove module my tools nine Type the following and press enter get disk info comp bad verbose.
You should require verbose output as the script runs, ultimately resulting in an error message when
querying bad because it doesn't exist.
Ten.
Type the following to remove the module and then press enter remove module my tools exercise to add
error handling to a new function task one identify potential error points.
One.
Open the Windows PowerShell ISC as administrator.
Two on the file menu.
Click Open three.
Navigate and Open Exercise two Task 1.14.
Identify the get event log command as the command that could cause an error if an incorrect computer
name was specified.
Five Press F5 To run the script task to add error handling note ensure that you complete the previous
task before you attempt this task.
One Online nine of Exercise two Task 1.1.
Add the following.
Try to online 12.
Add the following catch.
Right verbose failed to connect to computer name three modify line tend to read as follows git event
log log name security newest macs entries.
Error Action.
Stop computer name.
Dollar computer name for press control plus as to save the file task three Test the function.
Note These instructions provide a starting point that does not require you to have completed any prior
lab tasks.
The starting point includes all tasks up to this one.
If you are working on your own script, you can compare it to the starting point before you attempt
this task.
One.
Open the Windows PowerShell AC two on the file menu.
Click Open three.
Navigate and Open Exercise two.
Task 2.1.
Four Press F5 To run the script, you should receive verbose output when connecting to bad.