ive got a powershell secret

16
I've got a PowerShell Secret: Putting a GUI on your scripts Microsoft Services Premier Field Engineering Welcome. Microsoft Services helps businesses around the world maximize their investment in Microsoft products and technologies.

Upload: chris-conte

Post on 21-Jan-2017

242 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ive got a powershell secret

I've got a PowerShell Secret: Putting a GUI on your scripts

Microsoft ServicesPremier Field Engineering

Welcome.Microsoft Services helps businesses around the world maximize their investment in Microsoft products and technologies.

Page 2: Ive got a powershell secret

2

Why GUI?Business IntelligenceArchitectureHow ToCall to Action

OBJECTIVE

Understand how to apply XML, XAML, WPF and PowerShell to create an attractive and functional user interface

Page 3: Ive got a powershell secret

3

Graphical User Interface (GUI)

Page 4: Ive got a powershell secret

Tooling Up

What You'll need

.NET Framework 4

PowerShell (and ISE)

Visual Studio 2013 (or Visual Studio Express 2013)

Page 5: Ive got a powershell secret

Tooling Up Scripting with Windows PowerShellhttp://technet.microsoft.com/en-us/library/bb978526.aspx Understanding Event Driven Programming - 23http://channel9.msdn.com/Series/C-Sharp-Fundamentals-Development-for-Absolute-Beginners/Understanding-Event-Driven-Programming-23 XPath Tutorialhttp://www.w3schools.com/xpath/default.asp

Page 6: Ive got a powershell secret

6

WPF & XAML

Windows Presentation Foundation (WPF) is a next-generation 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…

Page 7: Ive got a powershell secret

7

How To…Let’s Build It

Drag picture to placeholder or click icon to add

Page 8: Ive got a powershell secret

8

Building the Interface

To create interface fire up Visual Studio and create a new WPF project.    File > New > Project

Page 9: Ive got a powershell secret

9

Building the Interface

Page 10: Ive got a powershell secret

Loading the Dialog

[CmdletBinding()]Param( [Parameter(Mandatory=$True,Position=1)] [string]$XamlPath) [xml]$Global:xmlWPF = Get-Content -Path $XamlPath #Add WPF and Windows Forms assembliestry{ 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 }

Page 11: Ive got a powershell secret

11

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"/>

Page 12: Ive got a powershell secret

12

Putting it Altogether

Page 13: Ive got a powershell secret

Wire an Event

#EVENT Handler $button1.add_Click({ $Label1.Content = "Hello World"})

Page 14: Ive got a powershell secret

Launching the Window

#Launch the window$xamGUI.ShowDialog() | out-null

Page 15: Ive got a powershell secret

Fire the Script

Set-ExecutionPolicy -ExecutionPolicy UnrestrictedSet-ExecutionPolicy : Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\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

Page 16: Ive got a powershell secret

© 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 after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION© 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 after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION

Contact

Chris ContePremier Field Engineer(813) [email protected]

www.microsoft.com/microsoftservices