Skip to content

Commit

Permalink
Set-DbaNetworkCertificate - Support localhost and use it as default f…
Browse files Browse the repository at this point in the history
…or SqlInstance (#9098)
  • Loading branch information
HCRitter authored Oct 10, 2023
1 parent f3798e2 commit 44fd060
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 64 deletions.
71 changes: 20 additions & 51 deletions private/functions/Invoke-ManagedComputerCommand.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Expand Down
16 changes: 3 additions & 13 deletions public/Set-DbaNetworkCertificate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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 $_
}
Expand Down

0 comments on commit 44fd060

Please sign in to comment.