diff --git a/private/configurations/settings/network.ps1 b/private/configurations/settings/network.ps1 new file mode 100644 index 0000000000..0f2c1c1f4a --- /dev/null +++ b/private/configurations/settings/network.ps1 @@ -0,0 +1,3 @@ +Set-DbatoolsConfig -FullName network.proxy.url -Value $null -Initialize -Validation string -Description "The URL of the network proxy." +Set-DbatoolsConfig -FullName network.proxy.username -Value $null -Initialize -Validation string -Description "The username for the network proxy." +Set-DbatoolsConfig -FullName network.proxy.securepassword -Value $null -Initialize -Validation securestring -Description "The secure password for the network proxy." diff --git a/public/Update-DbaBuildReference.ps1 b/public/Update-DbaBuildReference.ps1 index 6322b32cd3..9cc9ad62ff 100644 --- a/public/Update-DbaBuildReference.ps1 +++ b/public/Update-DbaBuildReference.ps1 @@ -53,16 +53,28 @@ function Update-DbaBuildReference { $EnableException ) $url = Get-DbatoolsConfigValue -Name 'assets.sqlbuildreference' + $proxyUrl = Get-DbatoolsConfigValue -FullName network.proxy.url + $proxyUsername = Get-DbatoolsConfigValue -FullName network.proxy.username + $proxySecurePassword = Get-DbatoolsConfigValue -FullName network.proxy.securepassword + if ($proxyUsername) { + $proxyCredential = New-Object System.Management.Automation.PSCredential ($proxyUsername, $proxySecurePassword) + } + try { $webContent = Invoke-TlsWebRequest $url -UseBasicParsing -ErrorAction Stop } catch { try { - Write-Message -Level Verbose -Message "Probably using a proxy for internet access, trying default proxy settings" - (New-Object System.Net.WebClient).Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials - $webContent = Invoke-TlsWebRequest $url -UseBasicParsing -ErrorAction Stop + Write-Message -Level Verbose -Message "Probably using a proxy for internet access, trying dbatools proxy settings" + $webContent = Invoke-TlsWebRequest $url -Proxy:$proxyUrl -ProxyCredential:$proxyCredential -UseBasicParsing -ErrorAction Stop } catch { - Write-Message -Level Warning -Message "Couldn't download updated index from $url" - return + try { + Write-Message -Level Verbose -Message "Probably using a proxy for internet access, trying default proxy settings" + (New-Object System.Net.WebClient).Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials + $webContent = Invoke-TlsWebRequest $url -UseBasicParsing -ErrorAction Stop + } catch { + Write-Message -Level Warning -Message "Couldn't download updated index from $url" + return + } } } return $webContent.Content