Welcome.
Microsoft Services
helps businesses
around the world
maximize their
investment in
Microsoft products
and technologies.
I've got a PowerShell
Secret: Putting a
GUI on your scripts
Microsoft Services
Premier Field
Engineering
OBJECTIVE
Understand how to
apply XML, XAML,
WPF and
PowerShell to
create an attractive
and functional user
interface
Why GUI?
Business Intelligence
Architecture
How To
Call to Action
2
Graphical
User
Interface
(GUI)
3
Tooling Up
.NET Framework 4
PowerShell (and ISE)
Visual Studio 2013 (or Visual Studio Express
2013)
Tooling Up
Scripting with Windows PowerShell
http://technet.microsoft.com/en-us/library/bb978526.aspx
Understanding Event Driven Programming - 23
http://channel9.msdn.com/Series/C-Sharp-FundamentalsDevelopment-for-Absolute-Beginners/Understanding-EventDriven-Programming-23
XPath Tutorial
http://www.w3schools.com/xpath/default.asp
WPF & XAML
Windows Presentation Foundation (WPF) is a nextgeneration presentation system for building
Windows client applications.
Extensible Application Markup Language (XAML) is
an XML-based markup language that is used to
implement an application's appearance declaratively.
It is typically used to create windows, dialog boxes,
pages, and user controls, and to fill them with
controls, shapes, and graphics…
6
How To…
Let’s Build It
7
Building the
Interface
To create interface fire up Visual Studio and create a new WPF project.
File > New > Project
8
Building the
Interface
9
Loading the
Dialog
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=1)]
[string]$XamlPath
)
[xml]$Global:xmlWPF = Get-Content -Path $XamlPath
#Add WPF and Windows Forms assemblies
try{
Add-Type -AssemblyName
PresentationCore,PresentationFramework,WindowsBase,system.windows.forms
} catch {
Throw "Failed to load Windows Presentation Framework assemblies."
}
#Create the XAML reader using a new XML node reader
$Global:xamGUI = [Windows.Markup.XamlReader]::Load((new-object
System.Xml.XmlNodeReader $xmlWPF))
#Create hooks to each named object in the XAML
$xmlWPF.SelectNodes("//*[@Name]") | %{
Set-Variable -Name ($_.Name) -Value $xamGUI.FindName($_.Name) -Scope Global
}
What’s in a
Name?
<Label Name="Label1" Content="Label"
HorizontalAlignment="Left" Margin="68,38,0,0"
VerticalAlignment="Top" Width="197"/>
<Button Name="Button1" Content="Button"
HorizontalAlignment="Left" Margin="307,41,0,0"
VerticalAlignment="Top" Width="75"/>
11
Putting it
Altogether
12
Wire an Event
#EVENT Handler
$button1.add_Click({
$Label1.Content = "Hello World"
})
Launching
the Window
#Launch the window
$xamGUI.ShowDialog() | out-null
Fire the Script
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
Set-ExecutionPolicy : Access to the registry key
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\Sh
ellIds\Microsoft.PowerShell' is denied. To change the
execution policy for the default (LocalMachine) scope,
start Windows PowerShell with the "Run as
administrator" option. To change the execution policy
for the current user, run "Set-ExecutionPolicy -Scope
CurrentUser".
Kick off your HelloWorld script: .\HelloWorld.ps1
Contact
Chris Conte
Premier Field Engineer
(813) 494.4849
chris.conte@microsoft.com
www.microsoft.com/microsoftservices
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or
other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must
respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or
after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION
other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must
respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided
after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION