Threading water with PowerShell…

The purpose of this blog is to show you how easy is to improve your existing PowerShell script using Windows form within your script. How many times you wish you can continue using your PowerShell console session when running your script containing a windows form. Well, I’m going to show you a simple way to keep using your PS console while running your PS Script form. This is accomplished thanks to Windows Presentation Foundation (WPF), which with the use of the Single Threaded Apartment (STA) allow us to run our script in a Runspace disconnecting it from our open PowerShell Console.

I’m going to open sample with PrimalScript 2009 editor and show the code without any WPF components:

This script will produce a windows filedialog box but stop you from keep using your open PowerShell session. Let’s copy/paste the code into our PS session and see the results:

As you can see, your PS session stopped and you won’t get back the prompt until you close the form:

So, how can we cut the umbilical cord so that you can leave your PowerShell Windows script application open while keep working in your current session. Here’s the basic block of PowerShell code the will allow the script to run disconnected from your current session:

As you can see in the above image, with eleven lines of code, you PS Script transform from Window form to WPF. Just insert your script code between the “## MyScript here…” and the “## End MyScript”. Then, save the new script file and run the script.

Let’s start this exercise:

Using one of the great tools from SAPIEN Technologies, PrimalForms 2009 and PrimalScript 2009. First were going to create a “Hello Word” script that display a Windows form. Then, we’re going export for PrimalForms solution to my PrimalScript editor so we can add the WPF components.

1. Build a PowerShell Windows Form sample:

2. Then, from the PrimalForms menu we click on “Export PowerShell | Export and edit with PrimalScript.

3. Exporting to PrimalScript will automatically, open the editor and load the code so you can keep making code changes if needed. Notice that when you export the PowerShell the name of the PrimalForms (*.pff) get saved as *.ps1 automatically.

4. Now, let’s open my WPF template so I can make changes to the HelloWorld.ps1 script.

5. Now, taking advantage of this awesome editor, I’m going to split the screen so I can have the code side-by-side and continue to copy/paste my WPF code. The first 9 code is going to be added just after the GenerateForm() function

6. Then, we add the last two lines of code.

7. Let’s save the script and run it to see the result. This will disconnect the script from the PowerShell console thanks to the $psCmd.Runspace.

Results:

Either way, if you copy/paste the code or execute the script form the prompt, you will get the same results -> you’re going to be able to continue using your PS console session.  Here’s all you need to give it a try:

# ==============================================================================================
# Microsoft PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 2009
# NAME: Sample_runWPF.ps1
# AUTHOR: Max Trinidad,
# DATE : 4/11/2010
#
# COMMENT: Adding WPF
#
# ==============================================================================================

$rs = [RunspaceFactory]::CreateRunspace()
$rs.ApartmentState, $rs.ThreadOptions = “STA”, “ReuseThread”
$rs.Open()
$psCmd = {Add-Type -AssemblyName PresentationCore,PresentationFramework,WindowsBase}.GetPowerShell()
$psCmd.Runspace = $rs
$psCmd.Invoke()
$psCmd.Commands.Clear()
$psCmd.AddScript(
{

## MyScript here...


## End MyScript

}

 

The source of this information – Microsoft PowerShell Team:

http://blogs.msdn.com/powershell/archive/2008/05/22/wpf-powershell-part-1-hello-world-welcome-to-the-week-of-wpf.aspx

Happy PowerShell Scripting!!!

Advertisements

About maxt2posh

I’m from Puerto Rico, have been working with computers since 1979. Throughout many years, I have worked with SQL Server Technologies, provided support to Windows Servers/Client machines, Microsoft Virtualization Technologies, and build some Visual Studio solutions. I’m a Microsoft PowerShell MVP since 2009 and MVP SAPIEN Technologies since 2010. I speak in most of the SQLSaturday, IT Pro and .NET camps events around the Florida’s State. Also founder of the Florida PowerShell User Group which meeting every 3rd Tuesday evening of the month.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s