Exercise 1: PowerShell v2 Basic Features
In this exercise you will go through some of the new features in PowerShell v2 in order to become acquainted with their syntax and the different ways they can be used. You will also focus on using the ISE to execute commands and debug PowerShell scripts by setting breakpoints using the commands discussed in the background for this exercise.
Part 1: PowerShell’s ISE
The following exercise must be carried out on your client machine.
In order to start getting acquainted with the new v2 PowerShell Integrated Scripting Environment, access the following menu item:
Start All Programs Accessories Windows PowerShell Windows PowerShell ISE
After a short while, PowerShell’s ISE will start up PowerShell’s ISE is made up three main panes: The script pane, the output pane, and the command pane:
The script pane (which supports tabs) is where you edit, save, and run scripts (or segments of the script). The output pane is where any output from the script or the command pane will be displayed. Finally, the command pane as is analogous to the shell environment you used in PowerShell v1 to run commands.
Type the following command in the command pane and press the green play button (or press enter):
gps
As you can see, the PowerShell command returns the list of all running processes on the machine into the output pane:
- Let’s try some other commands to become familiarized with the PowerShell ISE. Type the following text into the Untitled1.ps1 script in the script pane:
$x = 2 + "123" $y = "2" + 123 $z = 2 + "0xabc" (2 + 2) -eq 5
Press the Run Script button to execute the script (shortcut F5):
The 4 lines of commands will execute and any output will be displayed in the output window:
Clear the result window by clicking on the Clear Output Pane button:
- Now, find out what the value of $x is by typing into the command pane:
$x
The value of the variable $x will be displayed in the results window. The command window is your gateway to execute any expression within your current script.
- The ISE allows you to execute portions of your code without having to type them into the command window. Clear the result pane and from the script pane highlight the following command:
(2 + 2) -eq 5
With the above command highlighted, right click on it and select Run Selection:
The selected expression will be executed and the results will be shown in the results pane:
Another new feature in PowerShell v2 is the ability to display output in a GridView format. A GridView format allows you to sort items easily in order to find exactly what you are looking for.
From the command window, type the following command to output all running processes into GridView format:
gsv | out-gridview
The requested information will be displayed in a GridView panel:
Please note that the out-gridview command is not supported in Windows Server 2008 R2 Server Core
Once in the GridView, you can sort any of the columns by clicking on its header. For instance, click on the Status column to sort all processes by their status state:
You can also quickly find what you are looking for by typing a partial name in the search field. Type in id to find any services that have the term id:
You can further drill down in the search results if you wanted to filter the actual results more by clicking on the Add button and selecting your filtering conditions:
- Try to find the following information using the GridView:
- Find the number of processes that have more than 300 handles
- Find the number of DLLs in C:\Windows\System32 that have the number 32 in their name
Part 2: Debugging PowerShell with the ISE
The following instructions have to be carried out on your client machine.
- If not already open, invoke a PowerShell ISE window by accessing:
Start All Programs Accessories Windows PowerShell Windows PowerShell ISE
- From the command window, type the following command to change directories to the location where the scripts of this exercise are located:
cd “C:\Server 2008 R2 Labs\PowerShell v2 for Developers\Exercise-1”
- From the File menu, select Open
- Open the file located at the following path:
C:\Server 2008 R2 Labs\PowerShell v2 for Developers\Exercise-1\Some-Functions.ps1
The content of the file should be the following:
function foo{ write-host 1; write-host 2; write-host 3; write-host 4; write-host 5; write-host 6; } $hist = get-history $magicnumber = get-random; $histcopy = $hist
When this script is invoked, it will store the PowerShell buffer history inside the $hist variable and will assign a random number to the $magic-number variable. Furthermore, the script has a function called foo that will write a number (1-6) on a new line.
- Type the following lines into the command window to load the contents of the some-functions.ps1 script:
get-content some-functions.ps1. .\some-functions.ps1
- Invoke the script by typing:
.\Some-Functions.ps1
At this point, since there are no breakpoints defined, nothing should happen
- In order to set a breakpoint on lines 4 & 7 of the some-functions.ps1 script, type into the command window write the following line:
Set-PSBreakpoint .\some-functions.ps1 -line 4,7
The output pane will display the following information confirming that the breakpoints were successfully set:
You should also see the breakpoints set for you in the Scrip Pane:
Let’s invoke the foo function to see if indeed the breakpoints will be reached by typing the following into the command pane:
foo
Your script should run and immediately after you should see a breakpoint on the some-functions.ps1 file being reached:
Note that line 7 (write-host 6) is highlighted red, which means that a breakpoint has been set for that particular line.
Pay attention to the output pane as it will display information on the breakpoints reached:
PS C:\Server 2008 R2 Labs\PowerShell v2 for Developers\Exercise-1> foo 1 2 [DBG]>>> Hit Line breakpoint on 'C:\Server 2008 R2 Labs\PowerShell v2 for Developers\Exercise-1\some-functions.ps1:4'
Press F5 to reach the next breakpoint.
As expected, line 7 will turn yellow to indicate that the breakpoint is currently at this line:
- Press F5 once again to finish debugging the script.
- You can also set breakpoints when a particular function is called. Let’s set a breakpoint when the get-random function is called by typing in the command:
Set-PSBreakPoint -Command get-random
Invoke the script once more by typing:
.\Some-Functions.ps1
As expected, the script execution will stop at the line that contains the function:
- From the main menu, select DebugStop Debugger (Shift + F5) to stop the script execution.
- You can also set breakpoints when a particular variable is read or is written to. Let’s focus on the $hist variable and set both breakpoints (read and write) by typing:
Set-PSBreakpoint -Variable hist -mode read Set-PSBreakpoint -Variable hist -mode write
Invoke the script once more by typing:
.\Some-Functions.ps1
The first breakpoint that will be reached will be on line 9 as the variable hist is being modified:
- Press F5 twice to see if line 11 will hit the breakpoint.
- Once you have verified if line 11 triggered a breakpoint, press F5 once more to exit the script execution.
- In order to get a listing of the currently set breakpoints, type:
Get-PSBreakpoint
Your output should be similar to the one below:
ID Script Line Command Variable Action -- ------ ---- ------- -------- ------ 0 some-functions.ps1 4 1 some-functions.ps1 7 2 get-random 4 hist 3 hist
Let’s format this information so that it is easier on the eye
- Type the following command to format the information into a table:
Get-PSBreakpoint | sort-object -property id | format-table id, variable, AccessMode, line, enabled -auto
- Take note of the ID of the breakpoint for the reading of variable hist (in the case presented above the ID is 3)
- Type the following command to disable the breakpoint that is triggered every time the hist variable is read:
You must substitute X below by the ID number you obtained in step 22 above:
Disable-PSBreakpoint X
- Invoke the script once more by typing:
.\Some-Functions.ps1
The breakpoints should only stop on lines 9 and 10
- Finally, clean all breakpoints by typing the following command:
Get-PSBreakpoint | Remove-PSBreakpoint
Get the breakpoints again to verify that they were cleared:
Get-PSBreakpoint
No breakpoint should be displayed.
Please wait for the Instructor to resume the lecture.
Done Early? Try the Extra Credit!Try to set up the breakpoints using the menu items from the ISE
Exercise-1: Conclusions
By outputting PowerShell command results to a grid-view, you can easily and quickly sort and filter information. This new view will become an invaluable tool when the information to examine is exhaustive and needs to be presented in an elegant and easy to understand manner.
PowerShell v2 includes a powerful ISE that makes writing and debugging scripts a much simpler task than before. The ISE allows you run commands directly and will let you set breakpoints in scripts in order to examine the values of your variables. Breakpoints can be set using the menu options in the ISE or can be set by invoking the corresponding commandlet; which gives you more control over the conditions you would like the breakpoints to meet in order to activate.