Implement Custom Error Handling
This IT Pro Challenge helps learners understand how to use Windows PowerShell Integrated Script Environment (ISE) to analyze a script, implement the ErrorAction parameter, and use try/catch blocks to create custom error messages that are informative, and write those errors to a file.
Already have an account? Sign In »
In this IT Pro Challenge, learners will understand how to use Windows PowerShell Integrated Script Environment (ISE) to run routine server maintenance activities like analyzing and modifying a script, implementing the ErrorAction parameter to suppress error messages, and using try/catch blocks to create an informative custom message. As proof of concept, learners will also learn how to log these errors to a file. The skills acquired in the lab are critical for the role of network engineer or system administrator.
The scenario for this virtual lab is that you are a system administrator responsible for a third-party Windows PowerShell administrative script repository. All of the scripts work, but some of them produce vague, uninformative error messages. Additionally, some of the scripts do not terminate properly when there are issues with the script. Your job is to add custom error handling to the scripts to improve the messaging. To accomplish this, you need to analyze a script that queries multiple remote machines for operating system information. Then you will implement the ErrorAction parameter and add a Try/Catch block to suppress the default messaging and add an informative custom message. Finally, you need to modify the script to log a specific error and append a custom error to the error log.
Configure your environment
To begin, you will sign in to the Azure portal and display a virtual machine that has already been created (LabVM); ensure that the virtual machine status is Running. Then you will use the Remote Desktop Protocol (RDP) to connect to the virtual machine and launch Windows PowerShell as an administrator. Use the New-Item -Path "C:" -Name "[Directory Name]" -ItemType "Directory" command to create a folder on the C:\ drive and then try to switch to that folder.
Analyze the GetVersion script
In this step, you are provided with a script, which you will copy into Windows PowerShell Integrated Scripting Environment (ISE) and save as GetVersion.ps1 in the folder you created in the first step of the lab. Then, you will open Windows PowerShell and use the C:[Name]\GetVersion.ps1 command to run the script and observe the behavior. You should see a PSCComputerName, Caption, Version, and BuildNumber.
Implement the ErrorAction parameter
Now you will run the GetVersion.ps1 script again. This time, the script will generate an error because the log file already exists in the folder. You will then modify the script to use the ErrorAction parameter to ignore this error when you create the log folder. You will replace the md command by running the following code: md (Split-Path $LogFile) -ErrorAction SilentlyContinue | Out-Null.
The ErrorAction parameter only works on non-terminating errors; it is designed to override the value of the $ErrorActionPreference variable, which defaults to Continue. That means the script will continue to run even when this error is triggered; the error message is suppressed unless you use the ErrorAction parameter.
You will run the script one more time to see the effect of the ErrorAction parameter (no message displays). You should see a PSCComputerName, Caption, Version, and BuildNumber.
Use a Try Catch block to produce a custom error message
In Windows PowerShell, you will run the C:[Directory Name]\GetVersion.ps1 -ComputerName [Computer Name] command to run the GetVersion.ps1 script using the ComputerName parameter. You should get a non-terminating error.
Now you will use Windows PowerShell ISE to replace the Get-WmiObject command with a Try block that uses the ErrorAction parameter to suppress the error message. Then, after the Try block, you will add a Catch block to the code to generate a custom error message.
NOTE: Try blocks are used to encapsulate code that you want to monitor for errors; Catch blocks are used to handle, or "catch," errors from the Try block. A Catch block can specify an error type and then handle that error accordingly (in this case, with a custom message). A Try block may have several Catch blocks to handle different error conditions.
You will rerun the script to verify your work; you will see your custom error message.
In Windows PowerShell, you will run the BadCommand command, and then you will run the script using both the LogErrors and ComputerName parameters. Now you will open the error log (C:\Logs\Error.log) and look for the reference to BadCommand.
Next, you will use Windows PowerShell ISE to modify the script to log the specific error generated by the Get-WmiObject command and append a custom error message to the log file (another Try/Catch block). As a proof of concept, you will run the script once more with the same parameters, open the log file, and make sure that you can see the custom error message that you created.
By taking this virtual lab, you will learn how to analyze a reference script and use the Windows PowerShell ISE to implement the ErrorAction parameter, create try/catch blocks for error handling and custom messaging, and to log errors and a custom message to a file.