Share via


Exercise 1: PowerShell v2 Basic Features

Figure 1

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

Figure 2

Note:
The following exercise must be carried out on your client machine.

  1. 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
    Note:
    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:

    Figure 3

    Note:
    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.
  2. Type the following command in the command pane and press the green play button (or press enter):

    PowerShell

    gps
    Note:
    As you can see, the PowerShell command returns the list of all running processes on the machine into the output pane:

    Figure 4

  3. 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:

    PowerShell

    $x = 2 + "123" $y = "2" + 123 $z = 2 + "0xabc" (2 + 2) -eq 5
  4. Press the Run Script button to execute the script (shortcut F5):

    Figure 5

    Note:
    The 4 lines of commands will execute and any output will be displayed in the output window:

    Figure 6

  5. Clear the result window by clicking on the Clear Output Pane button:

    Figure 7

  6. Now, find out what the value of $x is by typing into the command pane:

    PowerShell

    $x
    Note:
    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.
  7. 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:

    PowerShell

    (2 + 2) -eq 5
  8. With the above command highlighted, right click on it and select Run Selection:

    Figure 8

    The selected expression will be executed and the results will be shown in the results pane:

    Figure 9

    Note:
    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.
  9. From the command window, type the following command to output all running processes into GridView format:

    PowerShell

    gsv | out-gridview
    Note:
    The requested information will be displayed in a GridView panel:

    Figure 10

    Note:
    Please note that the out-gridview command is not supported in Windows Server 2008 R2 Server Core
  10. 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:

    Figure 11

  11. 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:

    Figure 12

  12. 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:

    Figure 13

  13. Try to find the following information using the GridView:
    1. Find the number of processes that have more than 300 handles
    2. Find the number of DLLs in C:\Windows\System32 that have the number 32 in their name

Part 2: Debugging PowerShell with the ISE

Figure 14

Note:
The following instructions have to be carried out on your client machine.

  1. If not already open, invoke a PowerShell ISE window by accessing:

    Start All Programs Accessories Windows PowerShell Windows PowerShell ISE
  2. 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”
  3. From the File menu, select Open
  4. Open the file located at the following path:

    C:\Server 2008 R2 Labs\PowerShell v2 for Developers\Exercise-1\Some-Functions.ps1
    Note:
    The content of the file should be the following:

    PowerShell

    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
    Note:
    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.
  5. Type the following lines into the command window to load the contents of the some-functions.ps1 script:

    PowerShell

    get-content some-functions.ps1. .\some-functions.ps1
  6. Invoke the script by typing:

    PowerShell

    .\Some-Functions.ps1
    Note:
    At this point, since there are no breakpoints defined, nothing should happen
  7. 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:

    PowerShell

    Set-PSBreakpoint .\some-functions.ps1 -line 4,7
  8. The output pane will display the following information confirming that the breakpoints were successfully set:

    Figure 15

    Note:
    You should also see the breakpoints set for you in the Scrip Pane:

    Figure 16

  9. Let’s invoke the foo function to see if indeed the breakpoints will be reached by typing the following into the command pane:

    PowerShell

    foo
    Note:
    Your script should run and immediately after you should see a breakpoint on the some-functions.ps1 file being reached:

    Figure 17

    Note:
    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'
  10. Press F5 to reach the next breakpoint.

    Note:
    As expected, line 7 will turn yellow to indicate that the breakpoint is currently at this line:

    Figure 18

  11. Press F5 once again to finish debugging the script.
  12. 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
  13. Invoke the script once more by typing:

    PowerShell

    .\Some-Functions.ps1
    Note:
    As expected, the script execution will stop at the line that contains the function:

    Figure 19

  14. From the main menu, select DebugStop Debugger (Shift + F5) to stop the script execution.
  15. 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:

    PowerShell

    Set-PSBreakpoint -Variable hist -mode read Set-PSBreakpoint -Variable hist -mode write
  16. Invoke the script once more by typing:

    PowerShell

    .\Some-Functions.ps1
    Note:
    The first breakpoint that will be reached will be on line 9 as the variable hist is being modified:

    Figure 20

  17. Press F5 twice to see if line 11 will hit the breakpoint.
  18. Once you have verified if line 11 triggered a breakpoint, press F5 once more to exit the script execution.
  19. In order to get a listing of the currently set breakpoints, type:

    PowerShell

    Get-PSBreakpoint
    Note:
    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
    Note:
    Let’s format this information so that it is easier on the eye
  20. Type the following command to format the information into a table:

    PowerShell

    Get-PSBreakpoint | sort-object -property id | format-table id, variable, AccessMode, line, enabled -auto
  21. Take note of the ID of the breakpoint for the reading of variable hist (in the case presented above the ID is 3)
  22. Type the following command to disable the breakpoint that is triggered every time the hist variable is read:
    Note:
    You must substitute X below by the ID number you obtained in step 22 above:

    PowerShell

    Disable-PSBreakpoint X
  23. Invoke the script once more by typing:

    .\Some-Functions.ps1
    Note:
    The breakpoints should only stop on lines 9 and 10
  24. Finally, clean all breakpoints by typing the following command:

    PowerShell

    Get-PSBreakpoint | Remove-PSBreakpoint
  25. Get the breakpoints again to verify that they were cleared:

    PowerShell

    Get-PSBreakpoint
    Note:
    No breakpoint should be displayed.

    Note:
    Please wait for the Instructor to resume the lecture.

    Note:
    Done Early? Try the Extra Credit!Try to set up the breakpoints using the menu items from the ISE

Exercise-1: Conclusions

Figure 21

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.