@@ -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
0 commit comments