PowerShell + GUI plus GUI.pdf · 2019. 4. 25. · GUI over CLI Automation ... •"People like to...

Post on 18-Jan-2021

6 views 0 download

transcript

PowerShell + GUI

Windows Forms

WPF

XAML

WPK

25.04.2019 vjj 1

PowerShell vs. GUI

25.04.2019 vjj 2

• Aaron (http://www.wiredprairie.us/blog/index.php/archives/288)

• "... the idea of a PowerShell cmdlets, etc. powering a WPF front end doesn’t make sense to me.

• I thought the whole idea of PowerShell was that it was a shell platform — an advanced command prompt?

• Instinctively, I think — no GUI. Mixing WPF and PowerShell together feels somehow wrong, like using a mud topping on a bowl of ice cream."

co to je shell ?

• User Interface, pomocí kterého uživatel komunikuje s operačním systémem

tj.

• sada příkazů, funkcí, pro komunikaci s OSem

+ způsob jejich zadávání

• script, GUI, voice, gestures, ... , mix vyhovující uživateli

PowerShell is NOT a CLI

25.04.2019 vjj 4

• Jeffrey Snover [MSFT]Windows Management Partner Architect

(http://blogs.msdn.com/powershell/archive/2008/05/25/powershell-and-wpf-wtf.aspx)

• "PowerShell is not a CLI. PowerShell is an AUTOMATION technology. Cmdlets are units of automation."

GUI over CLI Automation

25.04.2019 vjj 5

• "We've had a shockingly bad shell and pathetic command line coverage for decades and I was personally committed to fixing that. That said, if we just transformed a click click click of the mouse to a click click click of the keyboard - we would not have moved IT forward all that much. The point of CLIs is that you can AUTOMATE operations"

• "Mark Brown convinced me that layering GUIs over the automation had to be a top priority in order to become the mainstream of Microsoft. He pointed out that over the long term this might change but for now, we were a GUI dominated culture and that unless we could help people layer GUIs on top of PowerShell, automation would stay in the backwaters of the company. From that point on, we really focused on ensuring that the CLI was merely one of many consumers of the API. A number of groups starting with Exchange have decided that the PowerShell API is their developer story because it provides remoting, security, logging, etc."

TCL/TK

25.04.2019 vjj 6

• "People like to talk about Unix's influence on PowerShell. That is absolutely true but I also try to point out the huge influence of unsung super engineers behind VMS/DCL and AS400/CL. I used to talk about this a lot but for some reason I haven't in a while but Tcl/TK had a huge influence on me and thus on PowerShell. John Ousterhout invented Tclwhich was an embeddable scripting language. The first time I saw it, I fell in love. The language was OK but the architecture just captured my imagination - it just made sense. John also invented TK which provided a GUI toolkit for TCL. TK made it simple to throw together quick and dirty GUIs using Tcl."

PowerShell/WPF

25.04.2019 vjj 7

• "While I love Tcl/TK, I also think we can do a lot better. The clock has moved forward and we have new technologies available. WPF is just a glorious UI surface. It provides a simple surface with incredible compositional power and utility. WPF's use of XAML means that we can leverage experienced designers using professional tools to take our quick and dirty tools and put them on par with the best of best. Because these are scripts, they are easy to share and customize to meet your specific needs. Advanced scripters can produce PowerShell / WPF scripts for others and then when you get it and decide that you need to add an extra button or two, you have the source code to see how it does what it does and can easily tweak it to meet your needs. The point I'm making is that with PowerShell/WPF the skill set required to TWEAK a GUI is very much lower than the skill set to CREATE the GUI."

• "I predict that the early adopters will pick up PowerShell/WPF, love it, and start producing some amazingly cool (but maybe ugly) GUIs. After a while, the next layer of people will start tweaking those scripts. After they've done that a couple of times, they'll have enough examples to start creating their own. Once that happens, you are going to see an explosion of custom tools. If you don't like the tools that MSFT delivers to you, you could try to convince us to change them and then wait a couple years till we re-release them or you could take a couple of hours and write yourself a custom tool that exactly meets your needs. THAT is going to be fun."

Windows Forms

load namespace

25.04.2019 vjj 8

load namespace[AppDomain]::CurrentDomain.Load("System.Windows.Forms,

Culture=Neutral,

Version=2.0.0.0,

PublicKeyToken=b77a5c561934e089")

[Reflection.Assembly]::Load("System.Windows.Forms,

Culture=Neutral,

Version=2.0.0.0,

PublicKeyToken=b77a5c561934e089")

[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

25.04.2019 vjj 9

objekt Windows.Forms.Form

• mírné zahlcení displeje – table of members

[system.windows.forms.form].GetMembers() | ft

• větší zahlcení displeje – list of members

[system.windows.forms.form].GetMembers() | fl

• list of methods

$s = [system.windows.forms.form].GetMembers()

$s | where {$_.membertype -eq "method"} | ft Name,IsSecurityCritical

25.04.2019 vjj 10

Windows.Forms$forms = [System.Windows.Forms.Form]

$form = new-object $forms

$form.Width = 800

$form.Height = 400

$form.Top = 200

$form.Left = 200

$ret = $form.ShowDialog()

25.04.2019 vjj 11

Windows.Forms Dialog$btn = new-object system.windows.forms.button

$btn.Text = "Pokus"

$btn.Left = 50

$btn.Top = 50

$frm = new-object system.windows.forms.form

$ctr = $frm.Controls

$ctr.Add($btn)

$ret = $frm.ShowDialog()

25.04.2019 vjj 12

Windows.Forms Dialog$b = new-object System.Windows.Forms.Button

$b.Text = "CLOSE"

$b.Top = 200

$b.Left = 150

$b.Add_Click( {$r.Close()} )

$r = new-object System.Windows.Forms.Form

$r.ShowInTaskBar = $True

$r.Controls.Add($b)

$ret = $r.ShowDialog()

25.04.2019 vjj 13

WPF

Add-Type -AssemblyName PresentationFramework

25.04.2019 vjj 14

Hello World$label = New-Object Windows.Controls.Label

$label.Content = "Hello World"

$label.FontSize = 32

$label.Margin = 50

$window = New-Object Windows.Window

$window.Title = "Hello World"

$window.Content = $label

$window.SizeToContent = "WidthAndHeight"

$ret = $window.ShowDialog()

25.04.2019 vjj 15

InkCanvas$inkCanvas = New-Object Windows.Controls.InkCanvas

$inkCanvas.MinWidth = 300

$inkCanvas.MinHeight = 300

$window = New-Object Windows.Window

$window.Title = "Scribble on Me"

$window.Content = $inkCanvas

$window.SizeToContent = "WidthAndHeight"

$ret = $window.ShowDialog()

25.04.2019 vjj 16

Slider$slider = New-Object Windows.Controls.Slider

$slider.Maximum = 100

$slider.Minimum = 0

$slider.Margin = 50

$slider.Width = 350

$window = new-object Windows.Window

$window.Content = $slider

$window.SizeToContent = "WidthAndHeight"

$ret = $window.ShowDialog()

$slider.Value

25.04.2019 vjj 17

TextBox$label = New-Object Windows.Controls.Label

$label.Content = "Type something"

$label.FontSize = 20

$label.FontWeight = "Heavy"

$label.Margin = "50,50,50,10"

$text = New-Object Windows.Controls.TextBox

$text.FontSize = 20

$text.Margin = "50,10,50,50"

$stackPanel = new-object Windows.Controls.StackPanel

$stackPanel.Background = "LightGray"

$stackPanel.Children.Add($label) | out-null

$stackPanel.Children.Add($text) | out-null

$window = new-object Windows.Window

$window.Content = $stackPanel

$window.SizeToContent = "WidthAndHeight"

$ret = $window.ShowDialog()

$ret

$text.Text

25.04.2019 vjj 18

Event handler$window = New-Object Windows.Window

$eventHandler = [Windows.Input.MouseButtonEventHandler]{$this.Close()}

$window.Add_MouseDown($eventHandler)

$window.Content = " Click Me and I will Go Away "

$window.SizeToContent = "WidthAndHeight"

$window.FontSize = 32

$ret = $window.ShowDialog()

$ret

25.04.2019 vjj 19

XAML

Add-Type -AssemblyName presentationframework

25.04.2019 vjj 20

inline[xml]$xaml = @'

<Window ...>

. . .

</Window>

'@

$reader = (New-Object Xml.XmlNodeReader $xaml)

$XReader = [Windows.Markup.XamlReader]::Load( $reader )

$ret = $XReader.ShowDialog()

25.04.2019 vjj 21

sample[xml]$xaml = @'

<Window

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Window1" Height="300" Width="500">

<StackPanel>

<TextBlock Margin="30" FontSize="24">

Hello XAML in PowerShell World !

</TextBlock>

<Button Width="400" Margin="30" FontSize="24">

Animate

<Button.Triggers>

<EventTrigger RoutedEvent="Button.Click">

<EventTrigger.Actions>

<BeginStoryboard>

<Storyboard>

<DoubleAnimation From="400" To="100" Duration="0:0:3" AutoReverse="True" RepeatBehavior="2x"

Storyboard.TargetProperty="(Button.Width)" />

</Storyboard>

</BeginStoryboard>

</EventTrigger.Actions>

</EventTrigger>

</Button.Triggers>

</Button>

</StackPanel>

</Window>

'@

$reader = (New-Object Xml.XmlNodeReader $xaml)

$XReader = [Windows.Markup.XamlReader]::Load( $reader )

$ret = $XReader.ShowDialog()

25.04.2019 vjj 22

file.xamlAdd-Type -AssemblyName presentationframework

[xml]$xaml = [Xml](get-content –Path ` D:\Documents\XAML\Test.xaml)# the root object should be Window

$reader = (New-Object Xml.XmlNodeReader $xaml)

$XReader = [Windows.Markup.XamlReader]::Load( $reader )

$ret = $XReader.ShowDialog()

25.04.2019 vjj 23

WPK

Import-Module WPK

WPKNew-Window -Height 125 -Width 300 -Show {

New-Label "Hello World" -FontSize 50

}

25.04.2019 vjj 25

WPK$color = ("Red", "DarkRed", "Green", "Blue","DarkBlue", "DarkOrange" | Get-Random)

$dim = Get-Random –min 100 –max 500

New-Window -Title "See The Big $color Ball" `

-WindowStartupLocation CenterScreen `

-Width 500 -Height 500 `

-SizeToContent "WidthAndHeight" `

-Show {

New-Grid -Background "LightGray" {

New-StackPanel {

New-Ellipse -Width $dim -Height $dim -Margin 10 -Fill $color

New-Button -Content "E_xit" -Width 100 -Margin 10 -On_Click {

$Window.Close()

}

}

}

}

25.04.2019 vjj 26