ive got a powershell secret

Post on 21-Jan-2017

236 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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.

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

3

Graphical User Interface (GUI)

Tooling Up

What You'll need

.NET Framework 4

PowerShell (and ISE)

Visual Studio 2013 (or Visual Studio Express 2013)

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

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…

7

How To…Let’s Build It

Drag picture to placeholder or click icon to add

8

Building the Interface

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

9

Building the Interface

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 }

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

12

Putting it Altogether

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

© 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) 494.4849chris.conte@microsoft.com

www.microsoft.com/microsoftservices

top related