diff --git a/private/functions/Invoke-ManagedComputerCommand.ps1 b/private/functions/Invoke-ManagedComputerCommand.ps1 index c383687e33..c0f73ad23e 100644 --- a/private/functions/Invoke-ManagedComputerCommand.ps1 +++ b/private/functions/Invoke-ManagedComputerCommand.ps1 @@ -90,68 +90,37 @@ function Invoke-ManagedComputerCommand { Credential = $Credential ErrorAction = 'Stop' } + try { + # Attempt to execute the command directly Invoke-Command2 @parms } catch { + # Log the failure and prepare to connect remotely Write-Message -Level Verbose -Message "Local connection attempt to $computer failed | $PSItem. Connecting remotely." $hostname = $resolved.FullComputerName - # For surely resolve stuff, and going by default with kerberos, this needs to match FullComputerName + # For securely resolving and using Kerberos by default, the ComputerName should match FullComputerName + # Now, we will attempt to connect remotely with different versions + + # Set the maximum and minimum versions for the loop + $MaxVersion = 16 + $MinVersion = 8 - try { - Invoke-Command2 @parms -ComputerName $hostname - } catch { + # Iterate through versions from maximum to minimum + foreach ($version in ($MaxVersion..$MinVersion)) { try { - $ArgumentList[$ArgumentList.GetUpperBound(0)].version = 16 + # Set the desired version in the ArgumentList + $ArgumentList[$ArgumentList.GetUpperBound(0)].version = $version $parms.ArgumentList = $ArgumentList + + # Attempt to execute the command remotely Invoke-Command2 @parms -ComputerName $hostname + + # Operation succeeded, exit the loop + break } catch { - # lol I'm not sure how to catch the last error so... - try { - $ArgumentList[$ArgumentList.GetUpperBound(0)].version = 15 - $parms.ArgumentList = $ArgumentList - Invoke-Command2 @parms -ComputerName $hostname - } catch { - try { - $ArgumentList[$ArgumentList.GetUpperBound(0)].version = 14 - $parms.ArgumentList = $ArgumentList - Invoke-Command2 @parms -ComputerName $hostname - } catch { - try { - $ArgumentList[$ArgumentList.GetUpperBound(0)].version = 13 - $parms.ArgumentList = $ArgumentList - Invoke-Command2 @parms -ComputerName $hostname - } catch { - try { - $ArgumentList[$ArgumentList.GetUpperBound(0)].version = 12 - $parms.ArgumentList = $ArgumentList - Invoke-Command2 @parms -ComputerName $hostname - } catch { - try { - $ArgumentList[$ArgumentList.GetUpperBound(0)].version = 11 - $parms.ArgumentList = $ArgumentList - Invoke-Command2 @parms -ComputerName $hostname - } catch { - try { - $ArgumentList[$ArgumentList.GetUpperBound(0)].version = 10 - $parms.ArgumentList = $ArgumentList - Invoke-Command2 @parms -ComputerName $hostname - } catch { - try { - $ArgumentList[$ArgumentList.GetUpperBound(0)].version = 9 - $parms.ArgumentList = $ArgumentList - Invoke-Command2 @parms -ComputerName $hostname - } catch { - $ArgumentList[$ArgumentList.GetUpperBound(0)].version = 8 - $parms.ArgumentList = $ArgumentList - Invoke-Command2 @parms -ComputerName $hostname - } - } - } - } - } - } - } + # Log the failure and proceed to the next version + Write-Message -Level Verbose -Message "Connecting remotely to: '$computer' using version: '$version' failed. | $PSItem" } } } diff --git a/public/Set-DbaNetworkCertificate.ps1 b/public/Set-DbaNetworkCertificate.ps1 index 4aa35f82c8..9ee3ed15e3 100644 --- a/public/Set-DbaNetworkCertificate.ps1 +++ b/public/Set-DbaNetworkCertificate.ps1 @@ -66,7 +66,7 @@ function Set-DbaNetworkCertificate { param ( [Parameter(ValueFromPipelineByPropertyName)] [Alias("ComputerName")] - [DbaInstanceParameter[]]$SqlInstance, + [DbaInstanceParameter[]]$SqlInstance = $env:COMPUTERNAME, [Parameter(ValueFromPipelineByPropertyName)] [PSCredential]$Credential, [parameter(Mandatory, ParameterSetName = "Certificate", ValueFromPipeline)] @@ -97,23 +97,13 @@ function Set-DbaNetworkCertificate { Write-Message -Level VeryVerbose -Message "Processing $instance" -Target $instance $null = Test-ElevationRequirement -ComputerName $instance -Continue - try { - Write-Message -Level Verbose -Message "Resolving hostname." - $resolved = $null - $resolved = Resolve-DbaNetworkName -ComputerName $instance -Credential $Credential -EnableException - } catch { - $resolved = Resolve-DbaNetworkName -ComputerName $instance -Credential $Credential -Turbo - } - - if ($null -eq $resolved) { - Stop-Function -Message "Can't resolve $instance" -Target $instance -Continue -Category InvalidArgument - } $computerName = $instance.ComputerName $instanceName = $instance.instancename try { - $sqlwmi = Invoke-ManagedComputerCommand -ComputerName $resolved.FQDN -ScriptBlock { $wmi.Services } -Credential $Credential -ErrorAction Stop | Where-Object DisplayName -eq "SQL Server ($instanceName)" + # removed: Resolve-DbaNetworkName command as it is used in the Invoke-ManagedComputerCommand anyway + $sqlwmi = Invoke-ManagedComputerCommand -ComputerName $computerName -ScriptBlock { $wmi.Services } -Credential $Credential -ErrorAction Stop | Where-Object DisplayName -eq "SQL Server ($instanceName)" } catch { Stop-Function -Message "Failed to access $instance" -Target $instance -Continue -ErrorRecord $_ }