Writing User Interfaces with Wpk

Only available on StudyMode
  • Download(s) : 250
  • Published : September 18, 2012
Open Document
Text Preview
Writing User Interfaces with WPK
Quick Start
The Windows 7 Resource Kit companion CD includes many Windows PowerShell modules that you can use, but you might want to pay special attention to the WPK module. WPK or the "Windows Presentation Foundation (WPF) PowerShell Kit," it enables you to use Windows PowerShell commands to create and display rich user interfaces. With WPK, systems administrators and beginning scripters can make their jobs easier by building user interfaces to simplify complex tasks. This Quick Start topic will show how to use WPK to create the following real-world interfaces. * A dialog box that collects information from the user

* A simple process monitor
* A media player
To start,
1. Copy the WPK folder to your Modules directory.
$home\Documents\WindowsPowerShell\Modules
For example:
C:\Users\<User-Name>\Documents\WindowsPowerShell\Modules 2. Start Windows PowerShell ISE (Integrated Scripting Environment).

3. At the Windows Powershell ISE command prompt, type:
import-module WPK
The first time that you import the WPK module, it takes approximately one minute to generate the code that it needs to manage the Windows Presentation Foundation. The functions in the WPK module generate Windows PowerShell script code, so you can use them to create scripts as you discover more and more useful parts of the .NET framework.

Project 1: Hello, World

Let’s start with a message box that says "Hello, World." It’s very easy. At the Windows PowerShell ISE command prompt, type (or cut/paste): New-Label “Hello, World” –Show
This command uses the New-Label function in the WPK module. To enlarge the text, use the FontSize parameter. The following command increases the font size to 48 point text. New-Label “Hello, World” –Show -FontSize 48

Very complex items can take a while to complete. Instead of waiting, you can create the items as a background job, and continue using Windows PowerShell for other things while it processes.

To create the "Hello, World" dialog box as a background job, add the AsJob parameter. New-Label “Hello, World” -Show -FontSize 48 -AsJob
The AsJob parameter lets you use all of the features of Windows PowerShell background jobs, including the Wait-Job, Stop-Job, and Remove-Job cmdlets.

Hint! Hint!
* Use tab-completion to avoid those extra keystrokes. Type part of the command and press the <Tab> key. Windows PowerShell tries to complete the command. You can press <Tab> repeatedly to cycle through matching commands.

* To find a cmdlet to create a type of WPF control, such as a button, use the following command format: get-command -name *button* -module wpk
-or-
gcm *button* -m wpk

Project 2: Digital Clock

Now let’s create a simple digital clock. This project includes a UI task that everyone needs to do, which is to run something at a regular interval. We’re going to use events to set that interval, along with commands, to make the clock work. Here’s the command to create the clock. You can cut/paste it into Windows PowerShell ISE or type it. New-Label -FontSize 24 -On_Loaded {

Register-PowerShellCommand -scriptBlock {
$window.Content.Content = (Get-Date | Out-String).Trim() } -run -in "0:0:0.5"
} -AsJob
This command uses the New-Label function in the WPK module. The clock is part of a label, just like the label that contained the "Hello, World" message. The command uses the FontSize parameter to set the font size to 24. New-Label -FontSize 24 -On_Loaded {

Register-PowerShellCommand -scriptBlock {
$window.Content.Content = (Get-Date | Out-String).Trim() } -run -in "0:0:0.5"
} -AsJob
The On_Loaded parameter represents the WPF "Loaded" event. When the event occurs, that is, when label is loaded, the command in the On_Loaded script block runs. New-Label -FontSize 24 -On_Loaded {

Register-PowerShellCommand -scriptBlock {...
tracking img