Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

WPF from PowerShell, the making of the WPF WMI Explorer Part1

Part 1, "a Hello PowerShell World!" WPF application

In the PowerScripting Podcast episode 26 , (A great source of PowerShell information, b.t.w. did you notice that they did go weekly ? ) my PowerShell WMI Explorer WPF Edition first beta was mentioned, together with Jaykul's posts about STA mode in PowerShell.

Nice, but it was also mentioned that it was a bit a developer thing,

I will try to explain in this series  why WPF support in PowerShell is useful for IT pro's also,Think a bit about it like what HTA is for VbScript, a light weight framework for building some user input forms etc,

In this series I will give some more simple examples of using XAML from PowerShell, build up from my experiences building the PowerShell WMI Explorer WPF Edition first beta  , starting with a "Hello PowerShell World!" WPF Application.

First thing to start with WPF forms are stored as XAML, and easy to use from PowerShell,

Create a WPF form

I will start with an Hello world example.

<Window  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <TextBlock>Hello World!</TextBlock>
</Window>

We need a window control and some namespaces as boiler code and we are ready to go, our first WPA form.

Start this form from PowerShell

To Use this from PowerShell we need to have PowerShell 2 CTP 2 started in STA mode, and load the WPF ('presentationframework') library (.NET 3.5) as we see in Hello world example below . ( I also added some sizing to the form here )

Add-Type -AssemblyName presentationframework


[xml]$xaml = @'
<Window
  xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Window1" Height="50" Width="200">
  <TextBlock>
    Hello PowerShell World !
  </TextBlock>
</Window>
'@

$reader=(New-Object Xml.XmlNodeReader $xaml)
$Form=[Windows.Markup.XamlReader]::Load( $reader )
$Form.ShowDialog() | out-null

image

what did happen there :

  • on the first line I load the needed assembly with Add-Type (This Cmdlet is also new before we needed to use [System.Reflection.Assembly]::LoadWithPartialName('presentationframework') for this)
  • I save the XAML a variable using a Here String
  • I load and start the form in the next 3 lines

That's all some more simple lines of boiler code and we can start any XAML form we create,

why I like XML as a format is that it is that you can write it by hand, and/or with an editor, In the case of the WMI explorer I used Visual Studio 2008 (that you can download in a Free Express version), but it can be any other XAML editor of choice.

This is how the WPF forms editor in VS2008 looks like :

image

Below in the Editor you can see the XAML generated, you can paste that code into the Here string in the PowerShell example above to show your own form,

*Note*

if you use VS2008 as an editor and you start a new WPF project as you can see above, in the XAML code you need to remove the XClass attribute from the form, in this case :

x:Class="WpfApplication7.Window1"

This is to attach the Application to the XAML file but as we are going to use PowerShell for this we have to remove this attribute before we can use it from PowerShell

But that's it for now, more about creating the forms in the editor next post, and then I show also how to add functionality to the form from PowerShell.

PS don't forget to check Jaykul's blog also, for some cool WPF from PowerShell examples.

Enjoy,

Greetings /\/\o\/\/

Published Monday, May 19, 2008 4:36 PM by MoW
Filed under: ,

Comments

# PowerShell ???? ?????????? ???? ?????????? &laquo; PowerShell ?? ???????????? ??????????????

# vbscript code examples

Sunday, June 01, 2008 7:31 PM by vbscript code examples
Anonymous comments are disabled