Running PowerShell scripts from WPF application - Visual Studio 2010 Project Template

by Klaus Graefensteiner 3. February 2011 02:41

Introduction

I was at WPF training last week in Phoenix, AZ that was provided by DevelopMentor. As part of the training I was working on a sample application that, of course, needed to be able to execute PowerShell scripts. Unfortunately it is not easy to add the required assembly System.Automation.Management to your project as reference. You have to open the *.cproj file with a text editor and add a Reference element for System.Automation.Management manually.You could also add the assembly System.Management.Automation.dll to your project from the Reference Assemblies folder, but that would require you to install the Windows 7 SDK.

image

Figure 1: WPF Essentials in Phoenix, AZ

This blog post combines all my findings and provides easy access to the reference assemblies and a project template that lets you create a Hello-World-WPF-With- PowerShell application.

Windows SDK

The System.Management.Automation.dll gets installed, besides in the GAC, in the reference assemblies folder as part of the Windows 7 SDK. In addition the SDK installs several PowerShell SDK code samples. These samples are located in:

C:\Program Files\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\windowspowershell

There are samples for creating your own scripting host, snap-in and PowerShell provider.

image

Figure 2: PowerShell SDK as part of the Windows SDK

The reference assemblies needed for using the PowerShell SDK are installed here:

C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0

image

Figure 3: 32-Bit PowerShell assemblies

and here:

C:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0

image

Figure 4: 64-bit assemblies

The samples and reference assemblies used in the Windows 7 PowerShell SDK can be downloaded here: PowerShell 2.0 SDK.zip

PowerShell namespaces and assembly references with access to SDK

When extending PowerShell you need to add a reference to the System.Management.Automation.dll and add the following namespaces to your project.

  • System.Management.Automation
  • System.Management.Automation.Host
  • System.Management.Automation.Runspaces

image

Figure 5: PowerShell SDK project configuration

PowerShell namespaces and assembly references without access to SDK

If you don’t have the SDK installed but still need to use PowerShell’s scripting engine in your project then you can either modify the *.cproj project file manually as described below or just download the sample projects or Visual Studio 2010 template that is ready to go.

image

Figure 6: Create new WPF Application

image

Figure 7: Open *.cproj with notepad

image

Figure 8: Add the line with System.Management.Automation to the Reference ItemGroup

image

Figure 9: The Hello World from WPF application

Download

The sample project and the WPFPowerShellHost Visual Studio 2010 template can be downloaded here: WPFPowerShellHostSampleAndProjectTemplate.zip

Tags: , , , , , , , , , , ,

WPF | Visual Studio | Tips & Tricks | PowerShell | How To

About Klaus Graefensteiner

I like the programming of machines.

Add to Google Reader or Homepage

LinkedIn FacebookTwitter View Klaus Graefensteiner's profile on Technorati
Klaus Graefensteiner

Klaus Graefensteiner
works as Developer In Test and is founder of the PowerShell Unit Testing Framework PSUnit. More...

Open Source Projects

PSUnit is a Unit Testing framwork for PowerShell. It is designed for simplicity and hosted by Codeplex.
BlogShell is The tool for lazy developers who like to automate the composition of blog content during the writing of a blog post. It is hosted by CodePlex.

Administration

About

Powered by:
BlogEngine.Net
Version: 1.6.1.0

License:
Creative Commons License

Copyright:
© Copyright 2013, Klaus Graefensteiner.

Disclaimer:
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Theme design:
This blog theme was designed and is copyrighted 2013 by Klaus Graefensteiner

Rendertime:
Page rendered at 5/21/2013 5:57:08 PM (PST Pacific Standard Time UTC DST -7)