Skip to content

Commit 6ac072b

Browse files
Check HTTP Header before download in Save-WebFile.ps1
Prevent mistakenly downloading http contents indicating 404 error
1 parent 2076778 commit 6ac072b

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

Public/Functions/Other/Save-WebFile.ps1

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,17 @@ function Save-WebFile {
116116
else {
117117
Write-Verbose "cURL Source: $SourceUrl"
118118
Write-Verbose "Destination: $DestinationFullName"
119-
120-
if ($host.name -match 'ConsoleHost') {
121-
Invoke-Expression "& curl.exe --insecure --location --output `"$DestinationFullName`" --url `"$SourceUrl`""
122-
}
123-
else {
124-
#PowerShell ISE will display a NativeCommandError, so progress will not be displayed
125-
$Quiet = Invoke-Expression "& curl.exe --insecure --location --output `"$DestinationFullName`" --url `"$SourceUrl`" 2>&1"
119+
$Headers = Invoke-Expression "& curl.exe --head --silent --insecure --location --url `"$SourceUrl`""
120+
if ($Headers[0] -match "200.+OK") {
121+
if ($host.name -match 'ConsoleHost') {
122+
Invoke-Expression "& curl.exe --insecure --location --output `"$DestinationFullName`" --url `"$SourceUrl`""
123+
}
124+
else {
125+
#PowerShell ISE will display a NativeCommandError, so progress will not be displayed
126+
$Quiet = Invoke-Expression "& curl.exe --insecure --location --output `"$DestinationFullName`" --url `"$SourceUrl`" 2>&1"
127+
}
128+
} else {
129+
Write-Warning "Header status: $($headers[0])"
126130
}
127131
}
128132
#=================================================

0 commit comments

Comments
 (0)