Skip to content

Commit b217e8e

Browse files
committed
Fix issues with SQL Server 2025
1 parent b32aa4d commit b217e8e

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

public/Copy-DbaLinkedServer.ps1

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ function Copy-DbaLinkedServer {
88
99
This is particularly useful during server migrations, disaster recovery scenarios, or when consolidating environments where maintaining external data connections is critical. The function handles various provider types and can optionally upgrade older SQL Client providers to current versions during migration.
1010
11+
When upgrading from older versions to SQL Server 2025+, MSOLEDBSQL is changed to MSOLEDBSQL19 and provider string for encrypt and trustservercertificate settings is added if not already included to ensure compatibility with the breaking changes in the new driver.
12+
1113
Credit: Password decryption techniques provided by Antti Rantasaari (NetSPI, 2014) - https://blog.netspi.com/decrypting-mssql-database-link-server-passwords/
1214
1315
.PARAMETER Source
@@ -179,8 +181,8 @@ function Copy-DbaLinkedServer {
179181
$linkedServerDataSource = $currentLinkedServer.DataSource
180182

181183
$copyLinkedServer = [PSCustomObject]@{
182-
SourceServer = $sourceServer.Name
183-
DestinationServer = $destServer.Name
184+
SourceServer = $sourceServer.DomainInstanceName
185+
DestinationServer = $destServer.DomainInstanceName
184186
Name = $linkedServerName
185187
ProductName = $linkedServerProductName
186188
DataSource = $linkedServerDataSource
@@ -192,7 +194,12 @@ function Copy-DbaLinkedServer {
192194

193195
# This does a check to warn of missing OleDbProviderSettings but should only be checked on SQL on Windows
194196
if ($destServer.Settings.OleDbProviderSettings.Name.Length -ne 0) {
195-
if (!$destServer.Settings.OleDbProviderSettings.Name -contains $provider -and !$provider.StartsWith("SQLN")) {
197+
if ($destServer.VersionMajor -ge 17 -and $provider -eq "MSOLEDBSQL") {
198+
# Starting with SQL Server 2025 (17.x), MSOLEDBSQL uses Microsoft OLE DB Driver version 19, which adds support for TDS 8.0. However, this driver introduces a breaking change. You must now specify the encrypt parameter.
199+
Write-Message -Level Verbose -Message "Upgrading provider from MSOLEDBSQL to MSOLEDBSQL19 to ensure compatibility with SQL Server 2025+."
200+
$provider = "MSOLEDBSQL19"
201+
}
202+
if (-not ($destServer.Settings.OleDbProviderSettings.Name -contains $provider) -and -not ($provider.StartsWith("SQLN"))) {
196203
if ($Pscmdlet.ShouldProcess($destinstance, "$($destServer.Name) does not support the $provider provider. Skipping $linkedServerName.")) {
197204
$copyLinkedServer.Status = "Skipped"
198205
$copyLinkedServer.Notes = "Missing provider"
@@ -246,7 +253,22 @@ function Copy-DbaLinkedServer {
246253
$sql = $sql -replace ("sqlncli[0-9]+", $newProvider)
247254
}
248255

249-
$destServer.Query($sql)
256+
if ($provider -eq "MSOLEDBSQL19") {
257+
# Starting with SQL Server 2025 (17.x), MSOLEDBSQL uses Microsoft OLE DB Driver version 19, which adds support for TDS 8.0. However, this driver introduces a breaking change. You must now specify the encrypt parameter.
258+
$providerString = $currentLinkedServer.ProviderString
259+
if ($providerString) {
260+
if ($providerString -notmatch "Encrypt\s*=\s*Optional" -and $providerString -notmatch "TrustServerCertificate\s*=\s*Yes") {
261+
Write-Message -Level Warning -Message "Provider string currently set to '$providerString', so will not change it. Please verify that it includes 'Encrypt=Optional;TrustServerCertificate=Yes' to ensure connectivity."
262+
} else {
263+
Write-Message -Level Verbose -Message "Provider string already includes encrypt and trustservercertificate settings, so not modifying it."
264+
}
265+
} else {
266+
Write-Message -Level Verbose -Message "Provider string is empty. Adding 'Encrypt=Optional;TrustServerCertificate=Yes' to provider string for MSOLEDBSQL19."
267+
$sql = $sql -replace "@provider=N'MSOLEDBSQL'", "@provider=N'MSOLEDBSQL19', @provstr=N'Encrypt=Optional;TrustServerCertificate=Yes'"
268+
}
269+
}
270+
271+
$null = $destServer.Query($sql)
250272

251273
if ($copyLinkedServer.ProductName -eq 'SQL Server' -and $copyLinkedServer.Name -ne $copyLinkedServer.DataSource) {
252274
$sql2 = "EXEC sp_setnetname '$($copyLinkedServer.Name)', '$($copyLinkedServer.DataSource)'; "

0 commit comments

Comments
 (0)