Skip to content

Commit 663d639

Browse files
committed
Added Ep8 - PowerShell Errors
1 parent a27dd09 commit 663d639

File tree

1 file changed

+174
-0
lines changed

1 file changed

+174
-0
lines changed
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
#Github PowerShell error documentation:
2+
#https://github.com/MicrosoftDocs/PowerShell-Docs/issues/1583
3+
4+
# Primary Pipeline aka output stream aka success pipeline
5+
# error pipeline
6+
7+
#-------------------------------------------------
8+
#non-terminating error
9+
1 / 0; Write-Host 'Hello, will I run after an error?'
10+
11+
#non-terminating errors don't stop loops
12+
$collection = @(
13+
'C:\Test\newcsv.csv',
14+
'c:\nope\nope.txt'
15+
'C:\Test\newcsv2.csv'
16+
)
17+
foreach ($item in $collection) {
18+
Get-Item $item
19+
}
20+
#-------------------------------------------------
21+
#terminating errors
22+
23+
# Without ErrorAction
24+
Get-Item -Path c:\nope\nope.txt; Write-Host 'Hello, will I run after an error?'
25+
# With ErrorAction
26+
Get-Item -Path c:\nope\nope.txt -ErrorAction Stop; Write-Host 'Hello, will I run after an error?'
27+
28+
#try/catch/finally
29+
#https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_try_catch_finally?view=powershell-6
30+
31+
# throw causes PowerShell to terminate
32+
try {
33+
1 / 0; Write-Host 'Hello, will I run after an error?'
34+
}
35+
catch {
36+
throw
37+
}
38+
39+
# this example will not go the catch and will run the Write-Host
40+
try {
41+
Get-Item -Path c:\nope\nope.txt; Write-Host 'Hello, will I run after an error?'
42+
}
43+
catch {
44+
Write-Host 'You are now in the catch'
45+
}
46+
47+
# this example will error and go directly to the catch
48+
try {
49+
Get-Item -Path c:\nope\nope.txt -ErrorAction Stop; Write-Host 'Hello, will I run after an error?'
50+
}
51+
catch {
52+
Write-Host 'You are now in the catch'
53+
}
54+
55+
56+
$processNames = @(
57+
'NotAProcess',
58+
'Notepad'
59+
60+
)
61+
foreach ($item in $processNames) {
62+
try {
63+
Get-Process -Nam $item
64+
}
65+
catch {
66+
Write-Host $item
67+
throw
68+
}
69+
}
70+
71+
#Write-Error simply prints the error for the user
72+
try {
73+
1 / 0; Write-Host 'Hello, will I run after an error?'
74+
}
75+
catch {
76+
# Maybe Log Something Right here
77+
Write-Error $_
78+
}
79+
80+
# Finally will log results regardless of what happens
81+
try {
82+
Get-Content -Path c:\nope\nope.txt -ErrorAction Stop
83+
}
84+
catch {
85+
Write-Error $_
86+
}
87+
finally {
88+
# log results to a logging file
89+
}
90+
#-------------------------------------------------
91+
92+
#The website exists, but the page does not
93+
try {
94+
$webResults = Invoke-WebRequest -Uri 'https://techthoughts.info/nope.htm'
95+
}
96+
catch {
97+
Write-Error $_
98+
}
99+
100+
#The website exists, but the page does not
101+
try {
102+
$webResults = Invoke-WebRequest -Uri 'https://techthoughts.info/nope.htm'
103+
}
104+
catch {
105+
$theError = $_
106+
if ($theError.Exception -like "*404*") {
107+
Write-Warning 'Web page not found. Check the address and try again.'
108+
#Retry code
109+
}
110+
else {
111+
throw
112+
}
113+
}
114+
115+
#The website does not exist
116+
try {
117+
$webResults = Invoke-WebRequest -Uri 'https://techthoughtssssssss.info/'
118+
}
119+
catch {
120+
$theError = $_
121+
$theError.Exception
122+
}
123+
124+
#The website exists, but the page does not
125+
try {
126+
$webResults = Invoke-WebRequest -Uri 'https://techthoughts.info/nope.htm'
127+
}
128+
catch {
129+
$theError = $_
130+
# see all the sub-properties
131+
$theError | Format-List * -Force
132+
}
133+
134+
#-------------------------------------------------
135+
# $Error
136+
137+
$Error
138+
1 / 0; $Error
139+
Get-Process -Name 'NotAProcess'
140+
$Error
141+
$Error.Clear()
142+
143+
$Error[5] | Format-List * -Force
144+
#-------------------------------------------------
145+
146+
#this example will help display some helpful message to the user
147+
#this example will only work in PowerShell 6.1+
148+
$uri = Read-Host 'Enter the URL'
149+
try {
150+
$webResults = Invoke-WebRequest -Uri $uri -ErrorAction Stop
151+
}
152+
catch {
153+
$statusCodeValue = $_.Exception.Response.StatusCode.value__
154+
switch ($statusCodeValue) {
155+
400 {
156+
Write-Warning -Message "HTTP Status Code 400 Bad Request. Check the URL and try again."
157+
}
158+
401 {
159+
Write-Warning -Message "HTTP Status Code 401 Unauthorized."
160+
}
161+
403 {
162+
Write-Warning -Message "HTTP Status Code 403 Forbidden. Server may be having issues. Check the URL and try again."
163+
}
164+
404 {
165+
Write-Warning -Message "HTTP Status Code 404 Not found. Check the URL and try again."
166+
}
167+
500 {
168+
Write-Warning -Message "HTTP Status Code 500 Internal Server Error."
169+
}
170+
Default {
171+
throw
172+
}
173+
}
174+
}

0 commit comments

Comments
 (0)