Chapters/graphical-controllers-wpf/stack-services.ps1

#Requires -version 5.0

#WPF Demonstration using a stack panel
$form = New-Object System.Windows.Window
#define what it looks like
$form.Title = "Services"
$form.Height = 200
$form.Width = 300

#create the stack panel
$stack = New-object System.Windows.Controls.StackPanel

#create a label
$label = New-Object System.Windows.Controls.Label
$label.HorizontalAlignment = "Left"
$label.Content = "Enter a Computer name:"

#add to the stack
$stack.AddChild($label)

#create a text box
$TextBox = New-Object System.Windows.Controls.TextBox
$TextBox.Width = 115
$TextBox.HorizontalAlignment = "Left"

#set a default value
$TextBox.Text = $env:COMPUTERNAME

#add to the stack
$stack.AddChild($TextBox)

#create a button
$btn = New-Object System.Windows.Controls.Button
$btn.Content = "_OK"
$btn.Width = 75
$btn.VerticalAlignment = "Bottom"
$btn.HorizontalAlignment = "Center"

#this will sort of work
$OK = {
    Write-Host "Getting services from $($textbox.Text)" -ForegroundColor Green;
    Get-Service -ComputerName $textbox.Text | where status -eq 'running'
}
#add an event handler
$btn.Add_click($OK)

#add to the stack
$stack.AddChild($btn)

#add the stack to the form
$form.AddChild($stack)

#show the form
$form.ShowDialog()