-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShow-SupportFileDemoForm.ps1
More file actions
222 lines (198 loc) · 8.17 KB
/
Show-SupportFileDemoForm.ps1
File metadata and controls
222 lines (198 loc) · 8.17 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<#
============================================================================================================================
Script: Show-SupportFileDemoForm
Author: Smart Ace
Notes:
This script is used to demonstrate a basic ScriptoForm that has been customized after deployment that includes a support file.
============================================================================================================================
#>
#region Settings
$ENVIRONMENTS_FILE = "$PSScriptRoot\Environments.txt"
$ENVIRONMENTS_FILE_HASH = "CE29ACC6600D2BE76D67484D162568D6518F9EE9011FCB02602A4FA63AE8D383"
$SUPPORT_CONTACT = "Smart Ace"
#endregion
#region Assemblies
Add-Type -AssemblyName System.Windows.Forms
#endregion
#region Appearance
[System.Windows.Forms.Application]::EnableVisualStyles()
#endregion
#region Controls
$FormMain = New-Object -TypeName System.Windows.Forms.Form
$GroupBoxMain = New-Object -TypeName System.Windows.Forms.GroupBox
$LabelServerName = New-Object -TypeName System.Windows.Forms.Label
$TextBoxServerName = New-Object -TypeName System.Windows.Forms.TextBox
$LabelEnvironment = New-Object -TypeName System.Windows.Forms.Label
$ComboBoxEnvironment = New-Object -TypeName System.Windows.Forms.ComboBox
$ButtonRun = New-Object -TypeName System.Windows.Forms.Button
$ButtonClose = New-Object -TypeName System.Windows.Forms.Button
$StatusStripMain = New-Object -TypeName System.Windows.Forms.StatusStrip
$ToolStripStatusLabelMain = New-Object -TypeName System.Windows.Forms.ToolStripStatusLabel
$ErrorProviderMain = New-Object -TypeName System.Windows.Forms.ErrorProvider
#endregion
#region Forms
$ShowFormMain =
{
$FormWidth = 330
$FormHeight = 260
$FormMain.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon((Get-Process -Id $PID).Path)
$FormMain.Text = "ScriptoForm Demo"
$FormMain.Font = New-Object -TypeName System.Drawing.Font("MS Sans Serif",8)
$FormMain.ClientSize = New-Object -TypeName System.Drawing.Size($FormWidth,$FormHeight)
$FormMain.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
$FormMain.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$FormMain.MaximizeBox = $false
$FormMain.AcceptButton = $ButtonRun
$FormMain.CancelButton = $ButtonClose
$FormMain.Add_Load($FormMain_Load)
$FormMain.Add_Shown($FormMain_Shown)
$GroupBoxMain.Location = New-Object -TypeName System.Drawing.Point(10,5)
$GroupBoxMain.Size = New-Object -TypeName System.Drawing.Size(($FormWidth - 20),($FormHeight - 80))
$FormMain.Controls.Add($GroupBoxMain)
$LabelServerName.Location = New-Object -TypeName System.Drawing.Point(15,15)
$LabelServerName.AutoSize = $true
$LabelServerName.Text = "Server Name:"
$GroupBoxMain.Controls.Add($LabelServerName)
$TextBoxServerName.Location = New-Object -TypeName System.Drawing.Point(15,35)
$TextBoxServerName.Size = New-Object -TypeName System.Drawing.Size(($FormWidth - 50),20)
$TextBoxServerName.TabIndex = 0
$TextBoxServerName.CharacterCasing = [System.Windows.Forms.CharacterCasing]::Upper
$TextBoxServerName.MaxLength = 15
$TextBoxServerName.Add_TextChanged($TextBoxServerName_TextChanged)
$GroupBoxMain.Controls.Add($TextBoxServerName)
$LabelEnvironment.Location = New-Object -TypeName System.Drawing.Point(15,70)
$LabelEnvironment.AutoSize = $true
$LabelEnvironment.Text = "Environment:"
$GroupBoxMain.Controls.Add($LabelEnvironment)
$ComboBoxEnvironment.Location = New-Object -TypeName System.Drawing.Point(15,90)
$ComboBoxEnvironment.Size = New-Object -TypeName System.Drawing.Size(($FormWidth - 50),20)
$ComboBoxEnvironment.TabIndex = 1
$ComboBoxEnvironment.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList
$ComboBoxEnvironment.Add_SelectedIndexChanged($ComboBoxEnvironment_SelectedIndexChanged)
$GroupBoxMain.Controls.Add($ComboBoxEnvironment)
$ButtonRun.Location = New-Object -TypeName System.Drawing.Point(($FormWidth - 175),($FormHeight - 60))
$ButtonRun.Size = New-Object -TypeName System.Drawing.Size(75,25)
$ButtonRun.TabIndex = 100
$ButtonRun.Enabled = $false
$ButtonRun.Text = "Run"
$ButtonRun.Add_Click($ButtonRun_Click)
$FormMain.Controls.Add($ButtonRun)
$ButtonClose.Location = New-Object -TypeName System.Drawing.Point(($FormWidth - 85),($FormHeight - 60))
$ButtonClose.Size = New-Object -TypeName System.Drawing.Size(75,25)
$ButtonClose.TabIndex = 101
$ButtonClose.Text = "Close"
$ButtonClose.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$FormMain.Controls.Add($ButtonClose)
$StatusStripMain.SizingGrip = $false
$StatusStripMain.Font = New-Object -TypeName System.Drawing.Font("MS Sans Serif",8)
[void]$StatusStripMain.Items.Add($ToolStripStatusLabelMain)
$FormMain.Controls.Add($StatusStripMain)
[void]$FormMain.ShowDialog()
$FormMain.Dispose()
}
#endregion
#region Functions
function Invoke-FormAction
{
param
(
[Parameter(Mandatory, Position = 0)] [ScriptBlock]$Action,
[Parameter(Position = 1)] [ScriptBlock]$Reset = $null,
[Parameter(Position = 2)] [String]$StatusText = "Working...please wait"
)
try
{
$ToolStripStatusLabelMain.Text = $StatusText
$FormMain.Controls | Where-Object {$PSItem -isnot [System.Windows.Forms.StatusStrip]} | ForEach-Object {$PSItem.Enabled = $false}
$FormMain.Cursor = [System.Windows.Forms.Cursors]::WaitCursor
[System.Windows.Forms.Application]::DoEvents()
Invoke-Command -ScriptBlock $Action
}
finally
{
$FormMain.Controls | ForEach-Object {$PSItem.Enabled = $true}
$FormMain.ResetCursor()
if ($Reset) {Invoke-Command -ScriptBlock $Reset}
$ToolStripStatusLabelMain.Text = "Ready"
$StatusStripMain.Update()
}
}
#endregion
#region Handlers
$FormMain_Load =
{
if (Test-Path -Path $ENVIRONMENTS_FILE)
{
$ComboBoxEnvironment.Items.AddRange($(Get-Content -Path $ENVIRONMENTS_FILE))
$ComboBoxEnvironment.SelectedIndex = 0
}
}
$FormMain_Shown =
{
$ToolStripStatusLabelMain.Text = "Ready"
$StatusStripMain.Update()
$FormMain.Activate()
}
$TextBoxServerName_TextChanged =
{
if ($TextBoxServerName.TextLength -eq 0)
{
$ErrorProviderMain.Clear()
$ButtonRun.Enabled = $false
}
elseif ($TextBoxServerName.Text -match "[^a-z0-9A-Z\-]")
{
$ErrorProviderMain.SetIconPadding($TextBoxServerName,-20)
$ErrorProviderMain.SetError($TextBoxServerName,"The server name contains an invalid character.")
$ButtonRun.Enabled = $false
}
else
{
$ErrorProviderMain.Clear()
$ButtonRun.Enabled = $true
}
}
$ButtonRun_Click =
{
Invoke-FormAction -Action {
try
{
Start-Sleep -Seconds 2
[void][System.Windows.Forms.MessageBox]::Show(
"Simulation of work completed.`n`nServer: $($TextBoxServerName.Text)`nEnvironment: $($ComboBoxEnvironment.Text)",
"Results",
[System.Windows.Forms.MessageBoxButtons]::OK,
[System.Windows.Forms.MessageBoxIcon]::Information
)
}
catch
{
[void][System.Windows.Forms.MessageBox]::Show(
$PSItem.Exception.Message + "`n`nPlease contact $SUPPORT_CONTACT for technical support.",
"Exception",
[System.Windows.Forms.MessageBoxButtons]::OK,
[System.Windows.Forms.MessageBoxIcon]::Warning
)
}
} -Reset {
$ComboBoxEnvironment.SelectedIndex = 0
$TextBoxServerName.Clear()
$TextBoxServerName.Focus()
}
}
#endregion
#region Main
if ((Get-FileHash -Path $ENVIRONMENTS_FILE).Hash -eq $ENVIRONMENTS_FILE_HASH)
{
Invoke-Command -ScriptBlock $ShowFormMain
}
else
{
[void][System.Windows.Forms.MessageBox]::Show(
"The resource file used with this script is invalid. This could indicate tampering or unapproved changes to the source code of the file.`n`nFor security reasons this script will now exit.",
"Resource File Verification",
[System.Windows.Forms.MessageBoxButtons]::OK,
[System.Windows.Forms.MessageBoxIcon]::Information
)
}
#endregion