-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3-Write-Warning.ps1
60 lines (51 loc) · 1.78 KB
/
3-Write-Warning.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
#region Setup
$question = 'Why is no one afraid of the Ning tribe attacking?'
$answer = 'RQB2AGUAcgB5AG8AbgBlACAAawBuAG8AdwBzACAAaQB0ACcAcwAgAGoAdQBzAHQAIA' +
'BhACAAdwBhAHIAIABOAGkAbgBnACAAYQBuAGQAIABuAG8AdABoAGkAbgBnACAAdABvACAAYgBl' +
'ACAAdwBvAHIAcgBpAGUAZAAgAGEAYgBvAHUAdAAuAA=='
$answer = [System.Text.Encoding]::Unicode.GetString(
[System.Convert]::FromBase64String($answer))
function whichKeyToPress {
param(
[string]$KeyToPress
)
Write-Host 'Press ' -ForegroundColor Green -NoNewline
Write-Host $KeyToPress -ForegroundColor Magenta -NoNewline
Write-Host ' for this example' -ForegroundColor Green
}
#endregion Setup
# ==============================================================================
function demoWarning {
Write-Warning $question
Write-Warning $answer
}
function demoWarningInquireContinue {
Write-Host 'Press ' -ForegroundColor Green -NoNewline
Write-Host 'Y' -ForegroundColor Magenta -NoNewline
Write-Host ' for this example' -ForegroundColor Green
Write-Warning -Message 'Do you want more Dad jokes?' -WarningAction Inquire
Write-Warning -Message 'As you wish'
}
function demoWarningInquireHaltCatch {
try {
whichKeyToPress -KeyToPress 'H' #code reduction example
Write-Warning -Message 'Do you want more Dad jokes?' -WarningAction Inquire
} catch {
Write-Warning -Message 'As you wish'
return
} finally {
Write-Warning -Message 'Exiting'
}
}
function demoWarningInquireHalt {
whichKeyToPress -KeyToPress 'H'
Wait-Debugger
Write-Warning -Message 'Do you want more Dad jokes?' -WarningAction Inquire
Write-Warning -Message 'As you wish'
}
Wait-Debugger
demoWarning
Wait-Debugger
demoWarningInquireContinue #Continue example
demoWarningInquireHaltCatch #Halt and catch example
demoWarningInquireHalt #Halt example