Use Breakpoints to Analyze and Debug a Script
Learn On Demand
Learn On Demand Pro Series

Time
45 minutes
Difficulty
Beginner

This IT Pro Challenge lab shows you how to use error messages to troubleshoot a script and Windows PowerShell breakpoints, which allows you to see the value of different variables as a script executes. As a result, you will become proficient in modifying script files and verifying your changes via Windows PowerShell.

Join over 2 million IT and cyber professionals advancing their careers

OR

Sign up with Google

Already have an account? Sign In »

Overview

In this IT Pro Challenge, you will learn how to use error messages in concert with Windows PowerShell breakpoints and the Get-Uptime command in order to find and troubleshoot any issues with a script. You will learn how to modify the script based on the error messages and test your changes. Using breakpoints gives you insight into how the values of variables are changed by code, and you can use this insight to troubleshoot the script. The skills acquired in this lab are useful for careers in network or systems administration.

Overview

The scenario for this hands-on lab is that you are the system administrator for your company. You have acquired a Windows PowerShell module from another administrator, but the module isn't working properly. Because you're not the one who wrote the script, not all the causes of the errors are readily apparent. You need to use the information from the error messages in conjunction with Windows PowerShell breakpoints to troubleshoot the issues so that you can begin using the module.

Configure your environment

First, you will use Remote Desktop Protocol (RDP) to connect to the virtual machine and then launch Windows PowerShell as an administrator. Use the New-Item -Path "C:" -Name "Class" -ItemType "Directory" command to create a folder named C:\Class and then change to that folder.

Save the UpTime module

Create a folder (UpTime) in C:\Program Files\WindowsPowerShell\Modules\UpTime. In Windows PowerShell ISE, add the following code and then save it as UpTime.psm1 __in the …\UpTime folder__:

Function Get-Uptime { Param ( [string[]]$ComputerName = "." )
foreach ($Computer in $ComputerName) { $os = Get-WmiObject win32_operatingsytem -ComputerName $Computer $LastBoot = $os.ConvertToDateTime($os.lastbootuptime) $UpTime = (Get-Date) - $LasBoot $UpTimeObj = New-Object PSObject -Property @{ComputerName=$Comptuer; Days=$UpTime.Days; Hours=$UpTime.Hours; Minutes=$UpTime.Minutes} Write-Output $UpTimeObj } }

Use error messages to find and fix problems

In Windows PowerShell ISE, run the Get-Uptime -ComputerName [virtual machine name] command. You should see an "invalid class" error. You need to change the script based on the error and run Get-Uptime again. This time, you will get a different error. Again, you need to update the script based on the error message, remove the module from memory, and run Get-Uptime for the third time.

Use a breakpoint to find an error

When you run Get-Uptime, you should see that the ComputerName value is blank. In Windows PowerShell ISE, you need to add a line breakpoint on the __New-Object __command line. Run Get-Uptime again. When the script reaches the breakpoint, the code stops executing, and you can hover your mouse over the variables to see their values. When you see a value that needs to be changed, you can update the code and then remove the breakpoint once the error is resolved.

Summary Conclusion

By taking this virtual lab, you will learn how to use Windows PowerShell commands to create a folder, write code to modify the UpTime module, save the UpTime PowerShell module. You will also learn how to troubleshoot and fix errors by using a combination of error messages and PowerShell breakpoints.