Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Copy-DbaAgentAlert.ps1 #5121

Merged
merged 8 commits into from
Feb 27, 2019
Merged

Update Copy-DbaAgentAlert.ps1 #5121

merged 8 commits into from
Feb 27, 2019

Conversation

mikepetrak
Copy link
Contributor

Added check to make sure the operator exists at the destination before creating the alert. If not, skip the alert and update the output object.

Type of Change

  • Bug fix (non-breaking change, fixes Copy-DbaAgentAlert does not validate that operator exist #4920)
  • New feature (non-breaking change, adds functionality)
  • Breaking change (effects multiple commands or functionality)
  • Ran manual Pester test and has passed (`.\tests\manual.pester.ps1)
  • Adding code coverage to existing functionality
  • Pester test is included
  • If new file reference added for test, has is been added to github.com/sqlcollaborative/appveyor-lab ?
  • Nunit test is included
  • Documentation
  • Build system

Purpose

Approach

Commands to test

Screenshots

Learning

Added check to make sure the operator exists at the destination before creating the alert. If not, skip the alert and update the output object.
Removed trailing spaces per failed test: "176Describe : dbatools style
177Context : indentation
178Name : It Copy-DbaAgentAlert.ps1 has no trailing spaces (line(s) 154,156)
179Result : Failed
180Message : Expected 0, but got 2."
@mikepetrak
Copy link
Contributor Author

Committed to fix failed test:

176Describe : dbatools style
177Context : indentation
178Name : It Copy-DbaAgentAlert.ps1 has no trailing spaces (line(s) 154,156)
179Result : Failed
180Message : Expected 0, but got 2.

@@ -150,6 +150,28 @@ function Copy-DbaAgentAlert {
continue
}

$operatorDoesntExist = $false
$sourceAlertNotifications = Get-DbaAgentAlert -SqlInstance $sourceServer.Name | Where-Object Name -eq $serverAlert.name | Select-Object Notifications
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to pass in just the $sourceServer object and not the property value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @wsmelton . Fixed and committed.

}
}
}
if ($operatorDoesntExist)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be within your ShouldProcess block at line 161 so when user's user -WhatIf they do not get eronous warning messages in the output...we keep the output only to the WhatIf messages.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So something like

if ($operatorNotExists) {
    if ($PSCmdlet.ShouldProcess....) {
        $copyAgentAlertSTatus....
        ...
        Write-Message -Message "Operator []..." -Level Warning
        continue
    }
}

Stop-Function should be used in try/catch blocks and not with if statements. You can simply just output a warning that the alert is being skipped because the operator does not exist, and then continue on.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small note I'd rather use operatorNotExists than use contractions in variable names (just reads better).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @wsmelton . Fixed and committed along with incorporating the approach by @alevyinroc to get the list of operators (with a small correction found while testing). Thanks, @alevyinroc!

Incorporated approach by @alevyinroc at alevyinroc@8a8a8da . Fixed issues mentioned in review by @wsmelton - thank you!
Checked for $null $sourceAlertOperators so Compare-Object wouldn't fail with a $null -ReferenceObject.
Removed trailing spaces.
@@ -155,7 +155,7 @@ function Copy-DbaAgentAlert {
$sourceAlertNotifications = Get-DbaAgentAlert -SqlInstance $sourceServer | Where-Object Name -eq $alertName | Select-Object Notifications
$sourceAlertOperators = $sourceAlertNotifications.Notifications.OperatorName
if ($null -ne $sourceAlertOperators) {
$missingOperators = Compare-Object -ReferenceObject $sourceAlertOperators -DifferenceObject $destServerOperators | Where-Object {$_.sideIndicator -eq "<="} | Select-Object -expandproperty InputObject
$missingOperators = Compare-Object -ReferenceObject $sourceAlertOperators -DifferenceObject $destServerOperators | Where-Object {$_.sideIndicator -eq "<="} | Select-Object -expandproperty InputObject
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use of external commands (even with built-in) require special attention because they will mess with our messaging system. (cause sea of red).

This alone is going to fail because you can't compare a populated object with an empty object. If there are no operators to start with on the destination server, your Compare command fails automatically.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will get this result:
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll work up an example of an alternate method that should include less overhead (improve timing, that compare command has overhead associated with it).

@wsmelton
Copy link
Member

I've pushed an update on the test specific to this PR. So when the solution is added the test should pass with all green.

On use of Compare and an alternative, I would use the pattern like below...which I think we have used in other commands but can't remember right off. Something like this should work:

                if ($serverAlert.HasNotification) {
                    $alertOperators = $serverAlert.EnumNotifications()
                    if ($destServerOperators.Name -notin $alertOperators.OperatorName) {
                        $missingOperators = ($alertOperators | Where-Object OperatorName -NotIn $destServerOperators.Name).OperatorName
                        if ($missingOperators.Count -gt 0 -or $missingOperators.Length -gt 0) {
                            $operatorList = $missingOperators -join ','
                            if ($PSCmdlet.ShouldProcess($destinstance, "Missing operator(s) at destination.")) {
                                $copyAgentAlertStatus.Status = "Skipped"
                                $copyAgentAlertStatus.Notes = "Operator(s) [$operatorList] do not exist on destination"
                                $copyAgentAlertStatus | Select-DefaultView -Property DateTime, SourceServer, DestinationServer, Name, Type, Status, Notes -TypeName MigrationObject
                                Write-Message -Message "One or more operators alerted by [$alertName] is not present at the destination. Alert will not be copied. Use Copy-DbaAgentOperator to copy the operator(s) to the destination. Missing operator(s): $operatorList" -Level Warning
                                continue
                            }
                        }
                    }
                }

Un-did changes using external command Compare-Object which has poor performance and problems with errors and put in approach by @wsmelton. Thanks, Shawn!
@mikepetrak
Copy link
Contributor Author

Thanks, @wsmelton! I committed your suggestion.

@potatoqualitee
Copy link
Member

Yesssss! thanks everyone for your hard work. I'll be merging now and will publish asap.

@potatoqualitee potatoqualitee merged commit ef77ab4 into dataplat:development Feb 27, 2019
@mikepetrak mikepetrak deleted the MikePetrakIssue5044 branch March 29, 2019 01:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Copy-DbaAgentAlert does not validate that operator exist
3 participants