Skip to content

Commit

Permalink
Fix usage of Stop-Function (do Remove-DbaDatabaseSafely)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasjordan committed Oct 3, 2023
1 parent 5d27f60 commit 0d20812
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions public/Remove-DbaDatabaseSafely.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,15 @@ function Remove-DbaDatabaseSafely {
if ($Force) { $ConfirmPreference = 'none' }

if (!$AllDatabases -and !$Database) {
Stop-Function -Message "You must specify at least one database. Use -Database or -AllDatabases." -ErrorRecord $_
Stop-Function -Message "You must specify at least one database. Use -Database or -AllDatabases."
return
}

try {
$sourceserver = Connect-DbaInstance -SqlInstance $SqlInstance -SqlCredential $SqlCredential
} catch {
Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $SqlInstance
return
}

if (-not $Destination) {
Expand All @@ -159,18 +160,18 @@ function Remove-DbaDatabaseSafely {
}

if ($SqlInstance -ne $Destination) {

try {
$destserver = Connect-DbaInstance -SqlInstance $Destination -SqlCredential $DestinationSqlCredential
} catch {
Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $Destination -Continue
Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $Destination
return
}

$sourcenb = $sourceserver.ComputerName
$destnb = $destserver.ComputerName

if ($BackupFolder.StartsWith("\\") -eq $false -and $sourcenb -ne $destnb) {
Stop-Function -Message "Backup folder must be a network share if the source and destination servers are not the same." -ErrorRecord $_ -Target $backupFolder
Stop-Function -Message "Backup folder must be a network share if the source and destination servers are not the same." -Target $backupFolder
return
}
} else {
Expand All @@ -190,15 +191,17 @@ function Remove-DbaDatabaseSafely {

if (!(Test-DbaPath -SqlInstance $destserver -Path $backupFolder)) {
$serviceAccount = $destserver.ServiceAccount
Stop-Function -Message "Can't access $backupFolder Please check if $serviceAccount has permissions." -ErrorRecord $_ -Target $backupFolder
Stop-Function -Message "Can't access $backupFolder Please check if $serviceAccount has permissions." -Target $backupFolder
return
}

#TODO: Test
$jobname = "Rationalised Final Database Restore for $dbName"
$jobStepName = "Restore the $dbName database from Final Backup"

if (!($destserver.Logins | Where-Object { $_.Name -eq $jobowner })) {
Stop-Function -Message "$destination does not contain the login $jobowner - Please fix and try again - Aborting." -ErrorRecord $_ -Target $jobowner
Stop-Function -Message "$destination does not contain the login $jobowner - Please fix and try again - Aborting." -Target $jobowner
return
}
}
process {
Expand All @@ -222,7 +225,8 @@ function Remove-DbaDatabaseSafely {
$agentService = Get-DbaService -ComputerName $destserver.ComputerName -InstanceName $destInstanceName -Type Agent

if ($agentService.State -ne 'Running') {
Stop-Function -Message "SQL Server Agent is not running. Please start the service." -ErrorAction $agentService.Name
Stop-Function -Message "SQL Server Agent is not running. Please start the service."
return
} else {
Write-Message -Level Verbose -Message "SQL Server Agent $($agentService.Name) is running."
}
Expand Down Expand Up @@ -261,8 +265,7 @@ function Remove-DbaDatabaseSafely {
$choice = $host.UI.PromptForChoice($Title, $Info, $Options, $Defaultchoice)
# Check the given option
if ($choice -eq 1) {
Stop-Function -Message "You have chosen skipping the database $dbName because of last known backup time ($lastFullBckDurationMin minutes)." -ErrorRecord $_ -Target $dbName -Continue
Continue
Stop-Function -Message "You have chosen skipping the database $dbName because of last known backup time ($lastFullBckDurationMin minutes)." -Target $dbName -Continue
}
}
} else {
Expand Down

0 comments on commit 0d20812

Please sign in to comment.