PowerShell + GUI

Windows Forms WPF XAML WPK

25.04.2019 vjj 1 PowerShell vs. GUI • 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."

25.04.2019 vjj 2 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 • Jeffrey Snover [MSFT] Windows Management Partner Architect

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

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

25.04.2019 vjj 4 GUI over CLI Automation • "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 . 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."

25.04.2019 vjj 5 TCL/ • "People like to talk about '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 Tcl which 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."

25.04.2019 vjj 6 PowerShell/WPF

• "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."

25.04.2019 vjj 7 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 $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( {$.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 []$xaml = @' . . . '@

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

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

$ret = $XReader.ShowDialog()

25.04.2019 vjj 21 sample [xml]$xaml = @' Hello XAML in PowerShell World ! '@

$reader = (New-Object Xml.XmlNodeReader $xaml) $XReader = [Windows.Markup.XamlReader]::Load( $reader ) $ret = $XReader.ShowDialog()

25.04.2019 vjj 22 file.xaml Add-Type -AssemblyName presentationframework

[xml]$xaml = [Xml](get-content –Path ` :\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 WPK New-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