Windows PowerShell V2 Sessions and Debugging
- Posted: Apr 09, 2009 at 12:17PM
- 2 comments
Loading user information from Channel 9
Something went wrong getting user information from Channel 9
Loading user information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Right click “Save as…”
This video demonstration walks through the steps of creating and using sessions in Windows PowerShell v2. It also introduces the debugging functions such as working with breakpoints.
See additional information via MSDN Code Gallery.
The Universal Code Execution Model means that you can run your cmdlets, scripts, and functions in any session, locally or remotely. A Windows PowerShell session is actually a persistent connection to the computer using the Windows Remote Management (WinRM) service. Creating sessions is performed using the New-PSSession cmdlet.
First, display the current process ID using the $pid environment variable. Next, create a new session on the localhost using New-PSSession.
The Invoke-Command cmdlet will cause script to be invoked on the given session, and in this case will simply output the value of $pid, the process ID, on the given session. This is a quick way to verify that the session is truly a different process from the original state.
The Get-PSSession cmdlet will display any running sessions. This does not include your terminal window which is not managed as a connection. Without running New-PSSession, the Get-PSSession cmdlet will not return any objects.
Next, execute the Some-Functions.ps1 script file containing a simple Square-Root function. Once invoked, you can execute the functions in the session, but not in the local terminal. The Import-PSSession cmdlet, run next, imports the sqrtAdvance function so it can be used locally.
Finally, the Remove-PSSession cmdlet is invoked to destroy the session and its underlying connection. Calling Get-PSSession confirms that it is no longer active.
Next, the video covers debugging using the Integrated Scripting Environment (ISE). The Set-PSBreakpoint cmdlet allows you to declare a breakpoint on a line, variable, or command. In the video, the breakpoint is placed on the variable "s1" which is the session variable. A breakpoint also specifies AccessMode, by default, Write. As soon as anything attempts to modify the value of "s1" code execution will break.
The existing script for working with sessions is started and as soon as the New-PSSession cmdlet is run, the breakpoint takes effect. ISE has a Debug menu which supports creating and managing breakpoints, stepping through and resuming code, and evening showing the call stack. The video steps through the code and demonstrates these features.