Skip to content

Commit 27f3fef

Browse files
Save-DbaKbUpdate - Add UseWebRequest switch and BitsTransfer fallback (#10278)
1 parent 118aed5 commit 27f3fef

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

public/Save-DbaKbUpdate.ps1

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ function Save-DbaKbUpdate {
3636
Use this workflow to preview available downloads with Get-DbaKbUpdate, then pipe selected results for download.
3737
Particularly useful when working with KBs that have multiple file options.
3838
39+
.PARAMETER UseWebRequest
40+
Forces the use of Invoke-WebRequest instead of Start-BitsTransfer for downloading files.
41+
Use this when running in a non-interactive context such as a scheduled task or SQL Agent job,
42+
where Start-BitsTransfer may fail because it requires a logged-in user session.
43+
3944
.PARAMETER EnableException
4045
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
4146
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
@@ -104,6 +109,7 @@ function Save-DbaKbUpdate {
104109
[string]$Language,
105110
[parameter(ValueFromPipeline)]
106111
[object[]]$InputObject,
112+
[switch]$UseWebRequest,
107113
[switch]$EnableException
108114
)
109115
process {
@@ -141,8 +147,15 @@ function Save-DbaKbUpdate {
141147
$file = "$Path$([IO.Path]::DirectorySeparatorChar)$fileName"
142148
}
143149

144-
if ((Get-Command Start-BitsTransfer -ErrorAction Ignore)) {
145-
Start-BitsTransfer -Source $link -Destination $file
150+
if (-not $UseWebRequest -and (Get-Command Start-BitsTransfer -ErrorAction Ignore)) {
151+
try {
152+
Start-BitsTransfer -Source $link -Destination $file
153+
} catch {
154+
Write-Message -Level Verbose -Message "Start-BitsTransfer failed, falling back to Invoke-WebRequest: $PSItem"
155+
Write-Progress -Activity "Downloading $fileName" -Id 1
156+
Invoke-TlsWebRequest -Uri $link -OutFile $file -ErrorAction Stop
157+
Write-Progress -Activity "Downloading $fileName" -Id 1 -Completed
158+
}
146159
} else {
147160
Write-Progress -Activity "Downloading $fileName" -Id 1
148161
Invoke-TlsWebRequest -Uri $link -OutFile $file -ErrorAction Stop

tests/Save-DbaKbUpdate.Tests.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Describe $CommandName -Tag UnitTests {
1717
"Architecture",
1818
"Language",
1919
"InputObject",
20+
"UseWebRequest",
2021
"EnableException"
2122
)
2223
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty

0 commit comments

Comments
 (0)