-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.ps1
72 lines (60 loc) · 1.93 KB
/
gui.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Custom GUI with pwsh
# Tutorial by Dan Stolts of ITProGuru
# Add .NET assembly object for Windows Forms
Add-Type -Assembly System.Windows.Forms
Write-Host "Create form" (Get-Date)
$form = New-Object System.Windows.Forms.Form
$form.FormBorderStyle = "FixedToolWindow"
$form.Text = "PowerShell Custom GUI Window"
$form.StartPosition = "CenterScreen"
$form.Width = 740 ; $form.Height = 380
# Add buttons
$buttonPanel = New-Object System.Windows.Forms.Panel
$buttonPanel.Size = New-Object Drawing.Size @(400,40)
$buttonPanel.Dock = "Bottom"
# Cancel button
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Top = $buttonPanel.Height - $cancelButton.Height - 10
$cancelButton.Left = $buttonPanel.Width - $cancelButton.Width - 10
$cancelButton.Text = "Cancel"
$cancelButton.DialogResult = "Cancel"
$cancelButton.Anchor = "Right"
# OK button
$okButton = New-Object System.Windows.Forms.Button
$okButton.Top = $cancelButton.Top
$okButton.Left = $cancelButton.Left - $okButton.Width - 10
$okButton.Text = "OK"
$okButton.DialogResult = "OK"
$okButton.Anchor = "Right"
# Add buttons to panel
$buttonPanel.Controls.Add($okButton)
$buttonPanel.Controls.Add($cancelButton)
# Add button panel to form
$form.Controls.Add($buttonPanel)
# Set default actions for buttons
$form.AcceptButton = $okButton # ENTER = OK
$form.CancelButton = $cancelButton # ESCAPE = Cancel
# Label and Textbox
# Computername
$lblHost = New-Object System.Windows.Forms.Label
$lblHost.Text = "Host Name:"
$lblHost.Top = 10
$lblHost.Left = 5
$lblHost.Width = 150
$lblHost.AutoSize = $true
# Add to form
$form.Controls.Add($lblHost)
# Textbox
$txtHost = New-Object System.Windows.Forms.TextBox
$txtHost.TabIndex = 0
$txtHost.Top = 10
$txtHost.Left = 160
$txtHost.Width = 120
$txtHost.Text = $env:COMPUTERNAME
# Add to form
$form.Controls.Add($txtHost)
# Finalize form and show dialog
Write-Host "Show form" (Get-Date)
$form.Activate()
$result = $form.ShowDialog()
Write-Host $result