-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathFunctionErrorHandlingExamplesPSF.ps1
More file actions
68 lines (68 loc) · 2.08 KB
/
FunctionErrorHandlingExamplesPSF.ps1
File metadata and controls
68 lines (68 loc) · 2.08 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
function With-PSF {
1..10 | ForEach-Object {
try {
Write-Host "With PSF $Psitem"
if ($psitem -eq 4) {
1 / 0
}
}
catch {
Write-PSFMessage -Message "I hit the catch and " -Level Output -ErrorRecord $_
Stop-PSFFunction -Message "Oh no we must stop " -AlwaysWarning
}
Write-Output "$Psitem I do run this code"
}
}
function With-PSFContinue {
1..10 | ForEach-Object {
try {
Write-Host " $Psitem"
if ($psitem -eq 4) {
1 / 0
}
}
catch {
Write-PSFMessage -Message "I hit the catch and " -Level Output -ErrorRecord $_
Stop-PSFFunction -Message "Oh no we must stop " -AlwaysWarning -Continue
}
Write-Output "$Psitem I do run this code"
}
}
function With-PSFSilentlyContinue {
1..10 | ForEach-Object {
try {
Write-Host "$Psitem"
if ($psitem -eq 4) {
1 / 0
}
}
catch {
Write-PSFMessage -Message "I hit the catch and " -Level Output -ErrorRecord $_
Stop-PSFFunction -Message "Oh no we must stop " -AlwaysWarning -SilentlyContinue
}
Write-Output "$Psitem I do run this code"
}
}
function With-PSFLabel {
:outer foreach ($x in 20..10) {
Write-Host "Outside $x - run 100 times"
1..100 | ForEach-Object {
try {
Write-Host "Inside $Psitem"
if ($psitem -eq 4) {
1 / 0
}
}
catch {
Write-PSFMessage -Message "I hit the catch and " -Level Output -ErrorRecord $_
Stop-PSFFunction -Message "Oh no we must stop " -AlwaysWarning -ContinueLabel outer -Continue
}
Write-Output "$Psitem I do run this code"
}
}
}
With-PSF
With-PSFContinue
With-PSFSilentlyContinue
With-PSFLabel # Everytime I hit 4 in the inside loop I will hit the catch and then go tot eh outer loop
Its slightly different with PSFStop