Skip to content

Commit

Permalink
Added EnableException to internal calls
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasjordan committed Oct 6, 2023
1 parent 73ff74e commit b1e6e1a
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions public/Remove-DbaDatabaseSafely.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ function Remove-DbaDatabaseSafely {
if ($destInstanceName -eq '') {
$destInstanceName = "MSSQLSERVER"
}
$agentService = Get-DbaService -ComputerName $destserver.ComputerName -InstanceName $destInstanceName -Type Agent
$agentService = Get-DbaService -ComputerName $destserver.ComputerName -InstanceName $destInstanceName -Type Agent -EnableException

if ($agentService.State -ne 'Running') {
Stop-Function -Message "SQL Server Agent is not running. Please start the service."
Expand Down Expand Up @@ -321,20 +321,22 @@ function Remove-DbaDatabaseSafely {

$DefaultCompression = $sourceserver.Configuration.DefaultBackupCompression.ConfigValue
$backupWithCompressionParams = @{
SqlInstance = $SqlInstance
SqlCredential = $SqlCredential
Database = $dbName
BackupFileName = $filename
CompressBackup = $true
Checksum = $true
SqlInstance = $SqlInstance
SqlCredential = $SqlCredential
Database = $dbName
BackupFileName = $filename
CompressBackup = $true
Checksum = $true
EnableException = $true
}

$backupWithoutCompressionParams = @{
SqlInstance = $SqlInstance
SqlCredential = $SqlCredential
Database = $dbName
BackupFileName = $filename
Checksum = $true
SqlInstance = $SqlInstance
SqlCredential = $SqlCredential
Database = $dbName
BackupFileName = $filename
Checksum = $true
EnableException = $true
}
if ($BackupCompression -eq "Default") {
if ($DefaultCompression -eq 1) {
Expand Down Expand Up @@ -362,18 +364,19 @@ function Remove-DbaDatabaseSafely {

## Create a Job Category
if (!(Get-DbaAgentJobCategory -SqlInstance $destination -SqlCredential $DestinationSqlCredential -Category $categoryname)) {
New-DbaAgentJobCategory -SqlInstance $destination -SqlCredential $DestinationSqlCredential -Category $categoryname
New-DbaAgentJobCategory -SqlInstance $destination -SqlCredential $DestinationSqlCredential -Category $categoryname -EnableException
}

try {
if ($Pscmdlet.ShouldProcess($destination, "Creating Agent Job $jobname on $destination")) {
$jobParams = @{
SqlInstance = $destination
SqlCredential = $DestinationSqlCredential
Job = $jobname
Category = $categoryname
Description = "This job will restore the $dbName database using the final backup located at $filename."
Owner = $jobowner
SqlInstance = $destination
SqlCredential = $DestinationSqlCredential
Job = $jobname
Category = $categoryname
Description = "This job will restore the $dbName database using the final backup located at $filename."
Owner = $jobowner
EnableException = $true
}
$job = New-DbaAgentJob @jobParams

Expand All @@ -388,7 +391,7 @@ function Remove-DbaDatabaseSafely {
## Suggestion check for disk space before restore
## Create Restore Script
try {
$jobStepCommand = Restore-DbaDatabase -SqlInstance $destserver -Path $filename -OutputScriptOnly -WithReplace
$jobStepCommand = Restore-DbaDatabase -SqlInstance $destserver -Path $filename -OutputScriptOnly -WithReplace -EnableException

$jobStepParams = @{
SqlInstance = $destination
Expand All @@ -401,6 +404,7 @@ function Remove-DbaDatabaseSafely {
OnSuccessAction = 'QuitWithSuccess'
OnFailAction = 'QuitWithFailure'
StepId = 1
EnableException = $true
}
if ($Pscmdlet.ShouldProcess($destination, "Creating Agent JobStep on $destination")) {
$jobStep = New-DbaAgentJobStep @jobStepParams
Expand All @@ -422,7 +426,7 @@ function Remove-DbaDatabaseSafely {
if ($Pscmdlet.ShouldProcess($destination, "Dropping Database $dbName on $sourceserver")) {
## Drop the database
try {
$null = Remove-DbaDatabase -SqlInstance $sourceserver -Database $dbName -Confirm:$false
$null = Remove-DbaDatabase -SqlInstance $sourceserver -Database $dbName -Confirm:$false -EnableException
Write-Message -Level Verbose -Message "Dropped $dbName Database on $source prior to running the Agent Job"
} catch {
Stop-Function -Message "FAILED : To Drop database $dbName on $server - aborting routine for $dbName. Exception: $_" -Continue
Expand Down Expand Up @@ -491,7 +495,7 @@ function Remove-DbaDatabaseSafely {
if ($Pscmdlet.ShouldProcess($dbName, "Dropping Database $dbName on $destination")) {
## Drop the database
try {
$null = Remove-DbaDatabase -SqlInstance $destserver -Database $dbName -Confirm:$false
$null = Remove-DbaDatabase -SqlInstance $destserver -Database $dbName -Confirm:$false -EnableException
Write-Message -Level Verbose -Message "Dropped $dbName database on $destination."
} catch {
Stop-Function -Message "FAILED : To Drop database $dbName on $destination - Aborting. Exception: $_" -Target $dbName -ErrorRecord $_ -Continue
Expand Down

0 comments on commit b1e6e1a

Please sign in to comment.