From 8e4a9e6bda8e46b38a5b22a9debc824b41b24df8 Mon Sep 17 00:00:00 2001 From: = <=> Date: Wed, 29 Nov 2023 13:40:40 +0100 Subject: [PATCH 01/11] Update-DbaBuildReference - Add Parameter Proxy Add optional parameters 'Proxy' and 'ProxyCredential' --- public/Update-DbaBuildReference.ps1 | 30 ++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/public/Update-DbaBuildReference.ps1 b/public/Update-DbaBuildReference.ps1 index 6322b32cd3..c9a12fa418 100644 --- a/public/Update-DbaBuildReference.ps1 +++ b/public/Update-DbaBuildReference.ps1 @@ -11,6 +11,13 @@ function Update-DbaBuildReference { .PARAMETER LocalFile Specifies the path to a local file to install from instead of downloading from Github. + .PARAMETER Proxy + Specifies the URI for the proxy to be used. By default, a connection without a proxy is made. If it fails, a retry using default proxy + settings is made. + + .PARAMETER ProxyCredential + Specifies Credential for the proxy, when parameter "Proxy" is supplied. Only required if the proxy needs authentication. + .PARAMETER EnableException By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message. This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting. @@ -32,6 +39,11 @@ function Update-DbaBuildReference { Looks online if there is a newer version of the build reference + .EXAMPLE + PS C:\> Update-DbaBuildReference -Proxy $ProxyURI -ProxyCredential $ProxyCred + + Looks online if there is a newer version of the build reference using the proxy supplied. + .EXAMPLE PS C:\> Update-DbaBuildReference -LocalFile \\fileserver\Software\dbatools\dbatools-buildref-index.json @@ -42,7 +54,12 @@ function Update-DbaBuildReference { [CmdletBinding(DefaultParameterSetName = 'Build')] param ( [string]$LocalFile, - [switch]$EnableException + [switch]$EnableException, + [Parameter(ParameterSetName = 'Build')] + [Parameter(Mandatory, ParameterSetName = 'CustomProxy')] + [URI]$Proxy, + [Parameter(ParameterSetName = 'CustomProxy')] + [pscredential]$ProxyCredential ) begin { @@ -50,11 +67,15 @@ function Update-DbaBuildReference { [CmdletBinding()] param ( [bool] - $EnableException + $EnableException, + [URI] + $Proxy, + [pscredential] + $ProxyCredential ) $url = Get-DbatoolsConfigValue -Name 'assets.sqlbuildreference' try { - $webContent = Invoke-TlsWebRequest $url -UseBasicParsing -ErrorAction Stop + $webContent = Invoke-TlsWebRequest $url -Proxy:$Proxy -ProxyCredential:$ProxyCredential -UseBasicParsing -ErrorAction Stop } catch { try { Write-Message -Level Verbose -Message "Probably using a proxy for internet access, trying default proxy settings" @@ -113,7 +134,7 @@ function Update-DbaBuildReference { Stop-Function -Message "Unable to read content from $LocalFile" } } else { - $newContent = Get-DbaBuildReferenceIndexOnline -EnableException $EnableException + $newContent = Get-DbaBuildReferenceIndexOnline -Proxy:$Proxy -ProxyCredential:$ProxyCredential -EnableException $EnableException } # If new data was sucessful read, compare LastUpdated and copy if newer @@ -124,6 +145,5 @@ function Update-DbaBuildReference { $newContent | Out-File $writable_idxfile -Encoding utf8 -ErrorAction Stop } } - } } \ No newline at end of file From ffa0a6caa3ee4fd1d94124818f69dda5aca68377 Mon Sep 17 00:00:00 2001 From: = <=> Date: Thu, 30 Nov 2023 11:56:25 +0100 Subject: [PATCH 02/11] Update-DbaBuildReference - Proxy Credential Implemented Parameter ProxyUseDefaultCredentials --- public/Update-DbaBuildReference.ps1 | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/public/Update-DbaBuildReference.ps1 b/public/Update-DbaBuildReference.ps1 index c9a12fa418..c3caf41102 100644 --- a/public/Update-DbaBuildReference.ps1 +++ b/public/Update-DbaBuildReference.ps1 @@ -18,6 +18,10 @@ function Update-DbaBuildReference { .PARAMETER ProxyCredential Specifies Credential for the proxy, when parameter "Proxy" is supplied. Only required if the proxy needs authentication. + .PARAMETER ProxyUseDefaultCredentials + Uses the credentials of the current user to access the proxy server. Requires Proxy parameter to be supplied. + ProxyCredential and ProxyUseDefaultCredentials can't be used at the same time. + .PARAMETER EnableException By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message. This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting. @@ -56,10 +60,13 @@ function Update-DbaBuildReference { [string]$LocalFile, [switch]$EnableException, [Parameter(ParameterSetName = 'Build')] - [Parameter(Mandatory, ParameterSetName = 'CustomProxy')] + [Parameter(Mandatory, ParameterSetName = 'Proxy')] + [Parameter(ParameterSetName = 'ProxyDefaultCredential')] [URI]$Proxy, - [Parameter(ParameterSetName = 'CustomProxy')] - [pscredential]$ProxyCredential + [Parameter(ParameterSetName = 'Proxy')] + [pscredential]$ProxyCredential, + [Parameter(ParameterSetName = 'ProxyDefaultCredential')] + [switch]$ProxyUseDefaultCredentials ) begin { @@ -71,9 +78,14 @@ function Update-DbaBuildReference { [URI] $Proxy, [pscredential] - $ProxyCredential + $ProxyCredential, + [switch] + $ProxyUseDefaultCredentials ) $url = Get-DbatoolsConfigValue -Name 'assets.sqlbuildreference' + if ($ProxyUseDefaultCredentials) { + $ProxyCredential = [System.Net.CredentialCache]::DefaultNetworkCredentials + } try { $webContent = Invoke-TlsWebRequest $url -Proxy:$Proxy -ProxyCredential:$ProxyCredential -UseBasicParsing -ErrorAction Stop } catch { @@ -134,7 +146,7 @@ function Update-DbaBuildReference { Stop-Function -Message "Unable to read content from $LocalFile" } } else { - $newContent = Get-DbaBuildReferenceIndexOnline -Proxy:$Proxy -ProxyCredential:$ProxyCredential -EnableException $EnableException + $newContent = Get-DbaBuildReferenceIndexOnline -Proxy:$Proxy -ProxyCredential:$ProxyCredential -ProxyUseDefaultCredentials:$ProxyUseDefaultCredentials -EnableException $EnableException } # If new data was sucessful read, compare LastUpdated and copy if newer From 4a04512f61be11a17854c43556c66ba205d73681 Mon Sep 17 00:00:00 2001 From: = <=> Date: Thu, 30 Nov 2023 13:21:59 +0100 Subject: [PATCH 03/11] Invoke-TlsWebRequest - UseBasicParsing Parameter named parameter UseBasicParsing ensures mapping of unnamed parameters --- private/functions/Invoke-TlsWebRequest.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/private/functions/Invoke-TlsWebRequest.ps1 b/private/functions/Invoke-TlsWebRequest.ps1 index 9bde9da27f..4aba39fadd 100644 --- a/private/functions/Invoke-TlsWebRequest.ps1 +++ b/private/functions/Invoke-TlsWebRequest.ps1 @@ -1,5 +1,5 @@ function Invoke-TlsWebRequest { - + Param ([switch]$UseBasicParsing) <# Internal utility that mimics invoke-webrequest but enables all tls available version @@ -14,7 +14,7 @@ function Invoke-TlsWebRequest { [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor $_ } - Invoke-WebRequest @Args + Invoke-WebRequest @Args -UseBasicParsing:$UseBasicParsing [Net.ServicePointManager]::SecurityProtocol = $currentVersionTls } \ No newline at end of file From 00fcdaedc65c9d229e6cdd182b792461e6e267e6 Mon Sep 17 00:00:00 2001 From: Simone Bizzotto Date: Tue, 12 Dec 2023 21:56:01 +0100 Subject: [PATCH 04/11] polished PR --- private/functions/Invoke-TlsWebRequest.ps1 | 7 +++++-- public/Update-DbaBuildReference.ps1 | 16 +++++++++++++--- tests/Update-DbaBuildReference.Tests.ps1 | 2 +- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/private/functions/Invoke-TlsWebRequest.ps1 b/private/functions/Invoke-TlsWebRequest.ps1 index 4aba39fadd..88ddf96884 100644 --- a/private/functions/Invoke-TlsWebRequest.ps1 +++ b/private/functions/Invoke-TlsWebRequest.ps1 @@ -1,5 +1,8 @@ function Invoke-TlsWebRequest { - Param ([switch]$UseBasicParsing) + Param ( + [switch]$UseBasicParsing, + [switch]$ProxyUseDefaultCredentials + ) <# Internal utility that mimics invoke-webrequest but enables all tls available version @@ -14,7 +17,7 @@ function Invoke-TlsWebRequest { [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor $_ } - Invoke-WebRequest @Args -UseBasicParsing:$UseBasicParsing + Invoke-WebRequest @Args -UseBasicParsing:$UseBasicParsing -ProxyUseDefaultCredentials:$ProxyUseDefaultCredentials [Net.ServicePointManager]::SecurityProtocol = $currentVersionTls } \ No newline at end of file diff --git a/public/Update-DbaBuildReference.ps1 b/public/Update-DbaBuildReference.ps1 index c3caf41102..9002b7fd53 100644 --- a/public/Update-DbaBuildReference.ps1 +++ b/public/Update-DbaBuildReference.ps1 @@ -29,7 +29,7 @@ function Update-DbaBuildReference { .NOTES Tags: Utility, SqlBuild - Author: Simone Bizzotto (@niphold) | Friedrich Weinmann (@FredWeinmann) + Author: Simone Bizzotto (@niphlod) | Friedrich Weinmann (@FredWeinmann) Website: https://dbatools.io Copyright: (c) 2018 by dbatools, licensed under MIT @@ -83,11 +83,21 @@ function Update-DbaBuildReference { $ProxyUseDefaultCredentials ) $url = Get-DbatoolsConfigValue -Name 'assets.sqlbuildreference' + $webRequestParams = @{ + Uri = $url + UseBasicParsing = $true + } + if ($Proxy) { + $webRequestParams['Proxy'] = $Proxy + } + if ($ProxyCredential) { + $webRequestParams['ProxyCredential'] = $ProxyCredential + } if ($ProxyUseDefaultCredentials) { - $ProxyCredential = [System.Net.CredentialCache]::DefaultNetworkCredentials + $webRequestParams['ProxyUseDefaultCredentials'] = $ProxyUseDefaultCredentials } try { - $webContent = Invoke-TlsWebRequest $url -Proxy:$Proxy -ProxyCredential:$ProxyCredential -UseBasicParsing -ErrorAction Stop + $webContent = Invoke-TlsWebRequest @webRequestParams -ErrorAction Stop } catch { try { Write-Message -Level Verbose -Message "Probably using a proxy for internet access, trying default proxy settings" diff --git a/tests/Update-DbaBuildReference.Tests.ps1 b/tests/Update-DbaBuildReference.Tests.ps1 index ef2227e6b5..66f0f24be6 100644 --- a/tests/Update-DbaBuildReference.Tests.ps1 +++ b/tests/Update-DbaBuildReference.Tests.ps1 @@ -6,7 +6,7 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' { Context "Validate parameters" { It "Should only contain our specific parameters" { [array]$params = ([Management.Automation.CommandMetaData]$ExecutionContext.SessionState.InvokeCommand.GetCommand($CommandName, 'Function')).Parameters.Keys - [object[]]$knownParameters = 'LocalFile', 'EnableException' + [object[]]$knownParameters = 'LocalFile', 'Proxy', 'ProxyCredential', 'ProxyUseDefaultCredentials', 'EnableException' Compare-Object -ReferenceObject $knownParameters -DifferenceObject $params | Should -BeNullOrEmpty } } From 3fad3a415288e5d63b8403392c72e400218cc4bb Mon Sep 17 00:00:00 2001 From: Simone Bizzotto Date: Tue, 12 Dec 2023 22:47:41 +0100 Subject: [PATCH 05/11] fixed formatting and params checking for inline help --- public/Update-DbaBuildReference.ps1 | 6 ++-- tests/InModule.Help.Tests.ps1 | 49 ++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/public/Update-DbaBuildReference.ps1 b/public/Update-DbaBuildReference.ps1 index 9002b7fd53..0f151dd3ff 100644 --- a/public/Update-DbaBuildReference.ps1 +++ b/public/Update-DbaBuildReference.ps1 @@ -64,7 +64,7 @@ function Update-DbaBuildReference { [Parameter(ParameterSetName = 'ProxyDefaultCredential')] [URI]$Proxy, [Parameter(ParameterSetName = 'Proxy')] - [pscredential]$ProxyCredential, + [PSCredential]$ProxyCredential, [Parameter(ParameterSetName = 'ProxyDefaultCredential')] [switch]$ProxyUseDefaultCredentials ) @@ -77,14 +77,14 @@ function Update-DbaBuildReference { $EnableException, [URI] $Proxy, - [pscredential] + [PSCredential] $ProxyCredential, [switch] $ProxyUseDefaultCredentials ) $url = Get-DbatoolsConfigValue -Name 'assets.sqlbuildreference' $webRequestParams = @{ - Uri = $url + Uri = $url UseBasicParsing = $true } if ($Proxy) { diff --git a/tests/InModule.Help.Tests.ps1 b/tests/InModule.Help.Tests.ps1 index 277981cc47..8f52458ce9 100644 --- a/tests/InModule.Help.Tests.ps1 +++ b/tests/InModule.Help.Tests.ps1 @@ -37,6 +37,52 @@ if ($SkipHelpTest) { return } $includedNames = (Get-ChildItem "$PSScriptRoot\..\public" | Where-Object Name -like "*.ps1" ).BaseName $commands = Get-Command -Module (Get-Module dbatools) -CommandType Cmdlet, Function, Workflow | Where-Object Name -in $includedNames +function Get-ParametersDefaultFirst +{ + param + ( + [Parameter(Mandatory = $true, + ValueFromPipeline = $true)] + [System.Management.Automation.CommandInfo] + $Command + ) + + begin + { + $Common = 'Debug', 'ErrorAction', 'ErrorVariable', 'InformationAction', 'InformationVariable', 'OutBuffer', 'OutVariable', 'PipelineVariable', 'Verbose', 'WarningAction', 'WarningVariable' + $parameters = @() + } + process + { + if ($defaultPSetName = $Command.DefaultParameterSet) + { + $defaultParameters = ($Command.ParameterSets | Where-Object Name -eq $defaultPSetName).parameters | Where-Object Name -NotIn $common + $otherParameters = ($Command.ParameterSets | Where-Object Name -ne $defaultPSetName).parameters | Where-Object Name -NotIn $common + + $parameters += $defaultParameters + if ($parameters -and $otherParameters) + { + $otherParameters | ForEach-Object { + if ($_.Name -notin $parameters.Name) + { + $parameters += $_ + } + } + $parameters = $parameters | Sort-Object Name + } + } + else + { + $parameters = $Command.ParameterSets.Parameters | Where-Object Name -NotIn $common | Sort-Object Name -Unique + } + + + return $parameters + } + end { } +} + + ## When testing help, remember that help is cached at the beginning of each session. ## To test, restart session. @@ -120,7 +166,8 @@ foreach ($command in $commands) { $Common = 'Debug', 'ErrorAction', 'ErrorVariable', 'InformationAction', 'InformationVariable', 'OutBuffer', 'OutVariable', 'PipelineVariable', 'Verbose', 'WarningAction', 'WarningVariable' - $parameters = $command.ParameterSets.Parameters | Sort-Object -Property Name -Unique | Where-Object Name -notin $common + $parameters = Get-ParametersDefaultFirst -Command $command | Sort-Object -Property Name -Unique | Where-Object Name -notin $common + #$parameters = $command.ParameterSets.Parameters | Sort-Object -Property Name -Unique | Where-Object Name -notin $common $parameterNames = $parameters.Name $HelpParameterNames = $Help.Parameters.Parameter.Name | Sort-Object -Unique foreach ($parameter in $parameters) { From 4630855089aee9a2f29fde91768aa2825ec05aa8 Mon Sep 17 00:00:00 2001 From: = <=> Date: Wed, 13 Dec 2023 13:24:07 +0100 Subject: [PATCH 06/11] Explicitly declare parameters using namend parameters and pass these to Invoke-WebRequest --- private/functions/Invoke-TlsWebRequest.ps1 | 49 ++++++++++++++++++++-- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/private/functions/Invoke-TlsWebRequest.ps1 b/private/functions/Invoke-TlsWebRequest.ps1 index 88ddf96884..ab50c91be9 100644 --- a/private/functions/Invoke-TlsWebRequest.ps1 +++ b/private/functions/Invoke-TlsWebRequest.ps1 @@ -1,15 +1,56 @@ function Invoke-TlsWebRequest { - Param ( + param( [switch]$UseBasicParsing, - [switch]$ProxyUseDefaultCredentials + [Uri]$Uri, + [Version]$HttpVersion, + [Microsoft.PowerShell.Commands.WebRequestSession]$WebSession, + [String]$SessionVariable, + [switch]$AllowUnencryptedAuthentication, + [Microsoft.PowerShell.Commands.WebAuthenticationType]$Authentication, + [PSCredential]$Credential, + [switch]$UseDefaultCredentials, + [String]$CertificateThumbprint, + [X509Certificate]$Certificate, + [switch]$SkipCertificateCheck, + [Microsoft.PowerShell.Commands.WebSslProtocol]$SslProtocol, + [SecureString]$Token, + [String]$UserAgent, + [switch]$DisableKeepAlive, + [Int32]$ConnectionTimeoutSeconds, + [Int32]$OperationTimeoutSeconds, + [Collections.IDictionary]$Headers, + [switch]$SkipHeaderValidation, + [switch]$AllowInsecureRedirect, + [Int32]$MaximumRedirection, + [Int32]$MaximumRetryCount, + [switch]$PreserveAuthorizationOnRedirect, + [Int32]$RetryIntervalSec, + [String]$CustomMethod, + [Microsoft.PowerShell.Commands.WebRequestMethod]$Method, + [switch]$PreserveHttpMethodOnRedirect, + [System.Net.Sockets.UnixDomainSocketEndPoint]$UnixSocket, + [switch]$NoProxy, + [Uri]$Proxy, + [PSCredential]$ProxyCredential, + [switch]$ProxyUseDefaultCredentials, + [Object]$Body, + [Collections.IDictionary]$Form, + [String]$ContentType, + [String]$TransferEncoding, + [String]$InFile, + [String]$OutFile, + [switch]$PassThru, + [switch]$Resume, + [switch]$SkipHttpErrorCheck ) + <# Internal utility that mimics invoke-webrequest but enables all tls available version rather than the default, which on a lot of standard installations is just TLS 1.0 - #> + $currentVersionTls = [Net.ServicePointManager]::SecurityProtocol $currentSupportableTls = [Math]::Max($currentVersionTls.value__, [Net.SecurityProtocolType]::Tls.value__) $availableTls = [enum]::GetValues('Net.SecurityProtocolType') | Where-Object { $_ -gt $currentSupportableTls } @@ -17,7 +58,7 @@ function Invoke-TlsWebRequest { [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor $_ } - Invoke-WebRequest @Args -UseBasicParsing:$UseBasicParsing -ProxyUseDefaultCredentials:$ProxyUseDefaultCredentials + Invoke-WebRequest @PSBoundParameters [Net.ServicePointManager]::SecurityProtocol = $currentVersionTls } \ No newline at end of file From ea833511b16d7d41e439e38c42ffcb6e3f00ab0f Mon Sep 17 00:00:00 2001 From: = <=> Date: Fri, 3 May 2024 14:06:12 +0200 Subject: [PATCH 07/11] Use dbatools config for proxy settings --- private/functions/Invoke-TlsWebRequest.ps1 | 48 +------------- public/Update-DbaBuildReference.ps1 | 74 +++++++--------------- 2 files changed, 24 insertions(+), 98 deletions(-) diff --git a/private/functions/Invoke-TlsWebRequest.ps1 b/private/functions/Invoke-TlsWebRequest.ps1 index ab50c91be9..9bde9da27f 100644 --- a/private/functions/Invoke-TlsWebRequest.ps1 +++ b/private/functions/Invoke-TlsWebRequest.ps1 @@ -1,56 +1,12 @@ function Invoke-TlsWebRequest { - param( - [switch]$UseBasicParsing, - [Uri]$Uri, - [Version]$HttpVersion, - [Microsoft.PowerShell.Commands.WebRequestSession]$WebSession, - [String]$SessionVariable, - [switch]$AllowUnencryptedAuthentication, - [Microsoft.PowerShell.Commands.WebAuthenticationType]$Authentication, - [PSCredential]$Credential, - [switch]$UseDefaultCredentials, - [String]$CertificateThumbprint, - [X509Certificate]$Certificate, - [switch]$SkipCertificateCheck, - [Microsoft.PowerShell.Commands.WebSslProtocol]$SslProtocol, - [SecureString]$Token, - [String]$UserAgent, - [switch]$DisableKeepAlive, - [Int32]$ConnectionTimeoutSeconds, - [Int32]$OperationTimeoutSeconds, - [Collections.IDictionary]$Headers, - [switch]$SkipHeaderValidation, - [switch]$AllowInsecureRedirect, - [Int32]$MaximumRedirection, - [Int32]$MaximumRetryCount, - [switch]$PreserveAuthorizationOnRedirect, - [Int32]$RetryIntervalSec, - [String]$CustomMethod, - [Microsoft.PowerShell.Commands.WebRequestMethod]$Method, - [switch]$PreserveHttpMethodOnRedirect, - [System.Net.Sockets.UnixDomainSocketEndPoint]$UnixSocket, - [switch]$NoProxy, - [Uri]$Proxy, - [PSCredential]$ProxyCredential, - [switch]$ProxyUseDefaultCredentials, - [Object]$Body, - [Collections.IDictionary]$Form, - [String]$ContentType, - [String]$TransferEncoding, - [String]$InFile, - [String]$OutFile, - [switch]$PassThru, - [switch]$Resume, - [switch]$SkipHttpErrorCheck - ) <# Internal utility that mimics invoke-webrequest but enables all tls available version rather than the default, which on a lot of standard installations is just TLS 1.0 - #> + #> $currentVersionTls = [Net.ServicePointManager]::SecurityProtocol $currentSupportableTls = [Math]::Max($currentVersionTls.value__, [Net.SecurityProtocolType]::Tls.value__) $availableTls = [enum]::GetValues('Net.SecurityProtocolType') | Where-Object { $_ -gt $currentSupportableTls } @@ -58,7 +14,7 @@ function Invoke-TlsWebRequest { [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor $_ } - Invoke-WebRequest @PSBoundParameters + Invoke-WebRequest @Args [Net.ServicePointManager]::SecurityProtocol = $currentVersionTls } \ No newline at end of file diff --git a/public/Update-DbaBuildReference.ps1 b/public/Update-DbaBuildReference.ps1 index 0f151dd3ff..9cc9ad62ff 100644 --- a/public/Update-DbaBuildReference.ps1 +++ b/public/Update-DbaBuildReference.ps1 @@ -11,17 +11,6 @@ function Update-DbaBuildReference { .PARAMETER LocalFile Specifies the path to a local file to install from instead of downloading from Github. - .PARAMETER Proxy - Specifies the URI for the proxy to be used. By default, a connection without a proxy is made. If it fails, a retry using default proxy - settings is made. - - .PARAMETER ProxyCredential - Specifies Credential for the proxy, when parameter "Proxy" is supplied. Only required if the proxy needs authentication. - - .PARAMETER ProxyUseDefaultCredentials - Uses the credentials of the current user to access the proxy server. Requires Proxy parameter to be supplied. - ProxyCredential and ProxyUseDefaultCredentials can't be used at the same time. - .PARAMETER EnableException By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message. This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting. @@ -29,7 +18,7 @@ function Update-DbaBuildReference { .NOTES Tags: Utility, SqlBuild - Author: Simone Bizzotto (@niphlod) | Friedrich Weinmann (@FredWeinmann) + Author: Simone Bizzotto (@niphold) | Friedrich Weinmann (@FredWeinmann) Website: https://dbatools.io Copyright: (c) 2018 by dbatools, licensed under MIT @@ -43,11 +32,6 @@ function Update-DbaBuildReference { Looks online if there is a newer version of the build reference - .EXAMPLE - PS C:\> Update-DbaBuildReference -Proxy $ProxyURI -ProxyCredential $ProxyCred - - Looks online if there is a newer version of the build reference using the proxy supplied. - .EXAMPLE PS C:\> Update-DbaBuildReference -LocalFile \\fileserver\Software\dbatools\dbatools-buildref-index.json @@ -58,15 +42,7 @@ function Update-DbaBuildReference { [CmdletBinding(DefaultParameterSetName = 'Build')] param ( [string]$LocalFile, - [switch]$EnableException, - [Parameter(ParameterSetName = 'Build')] - [Parameter(Mandatory, ParameterSetName = 'Proxy')] - [Parameter(ParameterSetName = 'ProxyDefaultCredential')] - [URI]$Proxy, - [Parameter(ParameterSetName = 'Proxy')] - [PSCredential]$ProxyCredential, - [Parameter(ParameterSetName = 'ProxyDefaultCredential')] - [switch]$ProxyUseDefaultCredentials + [switch]$EnableException ) begin { @@ -74,38 +50,31 @@ function Update-DbaBuildReference { [CmdletBinding()] param ( [bool] - $EnableException, - [URI] - $Proxy, - [PSCredential] - $ProxyCredential, - [switch] - $ProxyUseDefaultCredentials + $EnableException ) $url = Get-DbatoolsConfigValue -Name 'assets.sqlbuildreference' - $webRequestParams = @{ - Uri = $url - UseBasicParsing = $true - } - if ($Proxy) { - $webRequestParams['Proxy'] = $Proxy - } - if ($ProxyCredential) { - $webRequestParams['ProxyCredential'] = $ProxyCredential - } - if ($ProxyUseDefaultCredentials) { - $webRequestParams['ProxyUseDefaultCredentials'] = $ProxyUseDefaultCredentials + $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 @webRequestParams -ErrorAction Stop + $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 @@ -156,7 +125,7 @@ function Update-DbaBuildReference { Stop-Function -Message "Unable to read content from $LocalFile" } } else { - $newContent = Get-DbaBuildReferenceIndexOnline -Proxy:$Proxy -ProxyCredential:$ProxyCredential -ProxyUseDefaultCredentials:$ProxyUseDefaultCredentials -EnableException $EnableException + $newContent = Get-DbaBuildReferenceIndexOnline -EnableException $EnableException } # If new data was sucessful read, compare LastUpdated and copy if newer @@ -167,5 +136,6 @@ function Update-DbaBuildReference { $newContent | Out-File $writable_idxfile -Encoding utf8 -ErrorAction Stop } } + } } \ No newline at end of file From 0b205e440e4c21dcedf4531fcf04fae3b743c705 Mon Sep 17 00:00:00 2001 From: Chrissy LeMaire Date: Thu, 16 May 2024 17:28:16 +0200 Subject: [PATCH 08/11] put that back --- tests/Update-DbaBuildReference.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Update-DbaBuildReference.Tests.ps1 b/tests/Update-DbaBuildReference.Tests.ps1 index 66f0f24be6..ef2227e6b5 100644 --- a/tests/Update-DbaBuildReference.Tests.ps1 +++ b/tests/Update-DbaBuildReference.Tests.ps1 @@ -6,7 +6,7 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' { Context "Validate parameters" { It "Should only contain our specific parameters" { [array]$params = ([Management.Automation.CommandMetaData]$ExecutionContext.SessionState.InvokeCommand.GetCommand($CommandName, 'Function')).Parameters.Keys - [object[]]$knownParameters = 'LocalFile', 'Proxy', 'ProxyCredential', 'ProxyUseDefaultCredentials', 'EnableException' + [object[]]$knownParameters = 'LocalFile', 'EnableException' Compare-Object -ReferenceObject $knownParameters -DifferenceObject $params | Should -BeNullOrEmpty } } From d49a195161d743005b723aea4d61f7d6fe31754f Mon Sep 17 00:00:00 2001 From: Chrissy LeMaire Date: Thu, 16 May 2024 17:35:37 +0200 Subject: [PATCH 09/11] Update InModule.Help.Tests.ps1 --- tests/InModule.Help.Tests.ps1 | 51 ++--------------------------------- 1 file changed, 2 insertions(+), 49 deletions(-) diff --git a/tests/InModule.Help.Tests.ps1 b/tests/InModule.Help.Tests.ps1 index 8f52458ce9..8e99ca5c5d 100644 --- a/tests/InModule.Help.Tests.ps1 +++ b/tests/InModule.Help.Tests.ps1 @@ -37,52 +37,6 @@ if ($SkipHelpTest) { return } $includedNames = (Get-ChildItem "$PSScriptRoot\..\public" | Where-Object Name -like "*.ps1" ).BaseName $commands = Get-Command -Module (Get-Module dbatools) -CommandType Cmdlet, Function, Workflow | Where-Object Name -in $includedNames -function Get-ParametersDefaultFirst -{ - param - ( - [Parameter(Mandatory = $true, - ValueFromPipeline = $true)] - [System.Management.Automation.CommandInfo] - $Command - ) - - begin - { - $Common = 'Debug', 'ErrorAction', 'ErrorVariable', 'InformationAction', 'InformationVariable', 'OutBuffer', 'OutVariable', 'PipelineVariable', 'Verbose', 'WarningAction', 'WarningVariable' - $parameters = @() - } - process - { - if ($defaultPSetName = $Command.DefaultParameterSet) - { - $defaultParameters = ($Command.ParameterSets | Where-Object Name -eq $defaultPSetName).parameters | Where-Object Name -NotIn $common - $otherParameters = ($Command.ParameterSets | Where-Object Name -ne $defaultPSetName).parameters | Where-Object Name -NotIn $common - - $parameters += $defaultParameters - if ($parameters -and $otherParameters) - { - $otherParameters | ForEach-Object { - if ($_.Name -notin $parameters.Name) - { - $parameters += $_ - } - } - $parameters = $parameters | Sort-Object Name - } - } - else - { - $parameters = $Command.ParameterSets.Parameters | Where-Object Name -NotIn $common | Sort-Object Name -Unique - } - - - return $parameters - } - end { } -} - - ## When testing help, remember that help is cached at the beginning of each session. ## To test, restart session. @@ -148,7 +102,7 @@ foreach ($command in $commands) { if (-not ([string]::Equals($help.relatedLinks.NavigationLink.uri, "https://dbatools.io/$commandName"))) { # the link should point to the correct page It "The link for $commandName should be https://dbatools.io/$commandName" { - $help.relatedLinks[0].NavigationLink.uri | Should -MatchExactly "https://dbatools.io/$commandName" -Because "The web-page should be the one for the command!" + $help.relatedLinks[0].NavigationLink.uri | Should -MatchExactly "https://dbatools.io/$commandName" -Because "The web-page should be the one for the command!" } $testhelperrors += 1 } @@ -166,8 +120,7 @@ foreach ($command in $commands) { $Common = 'Debug', 'ErrorAction', 'ErrorVariable', 'InformationAction', 'InformationVariable', 'OutBuffer', 'OutVariable', 'PipelineVariable', 'Verbose', 'WarningAction', 'WarningVariable' - $parameters = Get-ParametersDefaultFirst -Command $command | Sort-Object -Property Name -Unique | Where-Object Name -notin $common - #$parameters = $command.ParameterSets.Parameters | Sort-Object -Property Name -Unique | Where-Object Name -notin $common + $parameters = $command.ParameterSets.Parameters | Sort-Object -Property Name -Unique | Where-Object Name -notin $common $parameterNames = $parameters.Name $HelpParameterNames = $Help.Parameters.Parameter.Name | Sort-Object -Unique foreach ($parameter in $parameters) { From fc8537bcd993196ddbacf2b39ef0c70c90995d26 Mon Sep 17 00:00:00 2001 From: Chrissy LeMaire Date: Thu, 16 May 2024 17:49:00 +0200 Subject: [PATCH 10/11] put it back --- tests/InModule.Help.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/InModule.Help.Tests.ps1 b/tests/InModule.Help.Tests.ps1 index 8e99ca5c5d..277981cc47 100644 --- a/tests/InModule.Help.Tests.ps1 +++ b/tests/InModule.Help.Tests.ps1 @@ -102,7 +102,7 @@ foreach ($command in $commands) { if (-not ([string]::Equals($help.relatedLinks.NavigationLink.uri, "https://dbatools.io/$commandName"))) { # the link should point to the correct page It "The link for $commandName should be https://dbatools.io/$commandName" { - $help.relatedLinks[0].NavigationLink.uri | Should -MatchExactly "https://dbatools.io/$commandName" -Because "The web-page should be the one for the command!" + $help.relatedLinks[0].NavigationLink.uri | Should -MatchExactly "https://dbatools.io/$commandName" -Because "The web-page should be the one for the command!" } $testhelperrors += 1 } From d0171d46faad4070e9f30634b3441dda908ffa92 Mon Sep 17 00:00:00 2001 From: Chrissy LeMaire Date: Thu, 16 May 2024 18:08:44 +0200 Subject: [PATCH 11/11] add proxy info --- private/configurations/settings/network.ps1 | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 private/configurations/settings/network.ps1 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."