Skip to content

Commit

Permalink
Use ExecuteAs with restore to set correct owner (do Add-DbaAgDatabase)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasjordan committed Oct 29, 2023
1 parent 0f7bb63 commit 8ac7da5
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion public/Add-DbaAgDatabase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,25 @@ function Add-DbaAgDatabase {
if ($Pscmdlet.ShouldProcess($replicaServerSMO[$replicaName], "Restore database $($db.Name) to replica $replicaName")) {
try {
Write-Message -Level Verbose -Message "Restore database $($db.Name) to replica $replicaName."
$null = $backups | Restore-DbaDatabase -SqlInstance $replicaServerSMO[$replicaName] -NoRecovery -TrustDbBackupHistory -ReuseSourceFolderStructure -EnableException
$restoreParams = @{
SqlInstance = $replicaServerSMO[$replicaName]
NoRecovery = $true
TrustDbBackupHistory = $true
ReuseSourceFolderStructure = $true
EnableException = $true
}
$sourceOwner = $db.Owner
$replicaOwner = $replicaServerSMO[$replicaName].ConnectedAs
if ($sourceOwner -ne $replicaOwner) {
Write-Message -Level Verbose -Message "Source database owner is $sourceOwner, replica database owner would be $replicaOwner."
if ($replicaServerSMO[$replicaName].Logins[$db.Owner]) {
Write-Message -Level Verbose -Message "Source database owner is found on replica, so using ExecuteAs with Restore-DbaDatabase to set correct owner."
$restoreParams['ExecuteAs'] = $db.Owner
} else {
Write-Message -Level Verbose -Message "Source database owner is not found on replica, so there is nothing we can do."
}
}
$null = $backups | Restore-DbaDatabase @restoreParams
} catch {
$failure = $true
Stop-Function -Message "Failed to restore database $($db.Name) to replica $replicaName." -ErrorRecord $_ -Continue
Expand Down

0 comments on commit 8ac7da5

Please sign in to comment.