Skip to content

Commit b7b2767

Browse files
authored
Add Show-MessageBox function + minor correction
1 parent 487e953 commit b7b2767

1 file changed

Lines changed: 163 additions & 10 deletions

File tree

utilities/Utils.ps1

Lines changed: 163 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,164 @@
1-
function OnApplicationLoad {
1+
function OnApplicationLoad {
22
return $true #return true for success or false for failure
3-
}
3+
}
44
function script:OnApplicationExit {
55
$script:ExitCode = 0 #Set the exit code for the Packager
6-
}
6+
}
7+
function Show-MessageBox {
8+
<#
9+
.SYNOPSIS
10+
Displays a MessageBox using Windows WinForms
11+
12+
.Description
13+
This function helps display a custom Message box with the options to set
14+
what Icons and buttons to use. By Default without using any of the optional
15+
parameters you will get a generic message box with the OK button.
16+
17+
.Parameter Msg
18+
Mandatory: This item is the message that will be displayed in the body
19+
of the message box form.
20+
Alias: M
21+
22+
.Parameter Title
23+
Optional: This item is the message that will be displayed in the title
24+
field. By default this field is blank unless other text is specified.
25+
Alias: T
26+
27+
.Parameter OkCancel
28+
Optional:This switch will display the Ok and Cancel buttons.
29+
Alias: OC
30+
31+
.Parameter AbortRetryIgnore
32+
Optional:This switch will display the Abort Retry and Ignore buttons.
33+
Alias: ARI
34+
35+
.Parameter YesNoCancel
36+
Optional: This switch will display the Yes No and Cancel buttons.
37+
Alias: YNC
38+
39+
.Parameter YesNo
40+
Optional: This switch will display the Yes and No buttons.
41+
Alias: YN
42+
43+
.Parameter RetryCancel
44+
Optional: This switch will display the Retry and Cancel buttons.
45+
Alias: RC
46+
47+
.Parameter Critical
48+
Optional: This switch will display Windows Critical Icon.
49+
Alias: C
50+
51+
.Parameter Question
52+
Optional: This switch will display Windows Question Icon.
53+
Alias: Q
54+
55+
.Parameter Warning
56+
Optional: This switch will display Windows Warning Icon.
57+
Alias: W
58+
59+
.Parameter Informational
60+
Optional: This switch will display Windows Informational Icon.
61+
Alias: I
62+
63+
.Parameter TopMost
64+
Optional: This switch will make the form stay on top until the user answers it.
65+
Alias: TM
66+
67+
.Example
68+
Show-MessageBox -Msg "This is the default message box"
69+
70+
This example creates a generic message box with no title and just the
71+
OK button.
72+
73+
.Example
74+
$A = Show-MessageBox -Msg "This is the default message box" -YN -Q
75+
76+
if ($A -eq "YES") {
77+
..do something
78+
}
79+
else {
80+
..do something else
81+
}
82+
83+
This example creates a msgbox with the Yes and No button and the
84+
Question Icon. Once the message box is displayed it creates the A varible
85+
with the message box selection choosen.Once the message box is done you
86+
can use an if statement to finish the script.
87+
88+
.Notes
89+
Created By Zachary Shupp
90+
Email zach.shupp@hp.com
91+
92+
Version: 1.0
93+
Date: 9/23/2013
94+
Purpose/Change: Initial function development
95+
96+
Version 1.1
97+
Date: 12/13/2013
98+
Purpose/Change: Added Switches for the form Type and Icon to make it easier to use.
99+
100+
Version 1.2
101+
Date: 3/4/2015
102+
Purpose/Change: Added Switches to make the message box the top most form.
103+
Corrected Examples
104+
105+
.Link
106+
http://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox.aspx
107+
108+
#>
109+
Param (
110+
[Parameter(Mandatory=$True)][Alias('M')][String]$Msg,
111+
[Parameter(Mandatory=$False)][Alias('T')][String]$Title = "",
112+
[Parameter(Mandatory=$False)][Alias('OC')][Switch]$OkCancel,
113+
[Parameter(Mandatory=$False)][Alias('OCI')][Switch]$AbortRetryIgnore,
114+
[Parameter(Mandatory=$False)][Alias('YNC')][Switch]$YesNoCancel,
115+
[Parameter(Mandatory=$False)][Alias('YN')][Switch]$YesNo,
116+
[Parameter(Mandatory=$False)][Alias('RC')][Switch]$RetryCancel,
117+
[Parameter(Mandatory=$False)][Alias('C')][Switch]$Critical,
118+
[Parameter(Mandatory=$False)][Alias('Q')][Switch]$Question,
119+
[Parameter(Mandatory=$False)][Alias('W')][Switch]$Warning,
120+
[Parameter(Mandatory=$False)][Alias('I')][Switch]$Informational,
121+
[Parameter(Mandatory=$False)][Alias('TM')][Switch]$TopMost)
122+
[Parameter(Mandatory=$False)][Alias('AS')][Switch]$AutoSize)
123+
124+
#Set Message Box Style
125+
IF($OkCancel){$Type = 1}
126+
Elseif($AbortRetryIgnore){$Type = 2}
127+
Elseif($YesNoCancel){$Type = 3}
128+
Elseif($YesNo){$Type = 4}
129+
Elseif($RetryCancel){$Type = 5}
130+
Else{$Type = 0}
131+
132+
#Set Message box Icon
133+
If($Critical){$Icon = 16}
134+
ElseIf($Question){$Icon = 32}
135+
Elseif($Warning){$Icon = 48}
136+
Elseif($Informational){$Icon = 64}
137+
Else { $Icon = 0 }
138+
139+
#Loads the WinForm Assembly, Out-Null hides the message while loading.
140+
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
141+
142+
If ($TopMost) {
143+
#Creates a Form to use as a parent
144+
$FrmMain = New-Object 'System.Windows.Forms.Form'
145+
$FrmMain.TopMost = $true
146+
147+
#Display the message with input
148+
$Answer = [System.Windows.Forms.MessageBox]::Show($FrmMain, $MSG, $TITLE, $Type, $Icon)
149+
150+
#Dispose of parent form
151+
$FrmMain.Close()
152+
$FrmMain.Dispose()
153+
}
154+
Else {
155+
#Display the message with input
156+
$Answer = [System.Windows.Forms.MessageBox]::Show($MSG , $TITLE, $Type, $Icon)
157+
}
158+
159+
#Return Answer
160+
Return $Answer
161+
}
7162

8163
function Test-RegistryValue {
9164
<#
@@ -35,24 +190,22 @@ function Test-RegistryValue {
35190
}
36191

37192
function Set-RegistryKey {
38-
39193
[CmdletBinding()]
40-
param
41-
(
194+
param (
42195
[Object]$computername,
43196
[Object]$parentKey,
44197
[Object]$nameRegistryKey,
45198
[Object]$valueRegistryKey
46199
)
47-
try{
200+
try {
48201
$remoteBaseKeyObject = [microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',$computername)
49202
$regKey = $remoteBaseKeyObject.OpenSubKey($parentKey,$true)
50203
$regKey.Setvalue("$nameRegistryKey", "$valueRegistryKey", [Microsoft.Win32.RegistryValueKind]::DWORD)
51204
$remoteBaseKeyObject.close()
52-
}
53-
catch {
205+
}
206+
catch {
54207
$_.Exception
55-
}
208+
}
56209
}
57210

58211
function Search-LastDrive

0 commit comments

Comments
 (0)