functions/Set-DbaAgentAlert.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#ValidationTags#Messaging,FlowControl,Pipeline,CodeStyle#
function Set-DbaAgentAlert {
    <#
.SYNOPSIS
Set-DbaAgentAlert updates a the status of a SQL Agent Alert.
 
.DESCRIPTION
Set-DbaAgentAlert updates an alert in the SQL Server Agent with parameters supplied.
 
.PARAMETER SqlInstance
SQL Server instance. You must have sysadmin access and server version must be SQL Server version 2000 or greater.
 
.PARAMETER SqlCredential
Login to the target instance using alternative credentials. Windows and SQL Authentication supported. Accepts credential objects (Get-Credential)
 
.PARAMETER Alert
The name of the alert.
 
.PARAMETER NewName
The new name for the alert.
 
.PARAMETER Enabled
Enabled the alert.
 
.PARAMETER Disabled
Disabled the alert.
 
.PARAMETER Force
The force parameter will ignore some errors in the parameters and assume defaults.
 
.PARAMETER InputObject
Enables piping alert objects
 
.PARAMETER WhatIf
Shows what would happen if the command were to run. No actions are actually performed.
 
.PARAMETER Confirm
Prompts you for confirmation before executing any changing operations within the command.
 
.PARAMETER EnableException
        By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
        This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
        Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
 
.NOTES
Author: Garry Bargsley (@gbargsley, garrybargsley.com)
Tags: Agent, Alert
 
Website: https://dbatools.io
Copyright: (C) Chrissy LeMaire, clemaire@gmail.com
License: MIT https://opensource.org/licenses/MIT
 
.LINK
https://dbatools.io/Set-DbaAgentAlert
 
.EXAMPLE
Set-DbaAgentAlert -SqlInstance sql1 -Alert 'Severity 025: Fatal Error' -Disabled
Changes the alert to disabled.
 
.EXAMPLE
Set-DbaAgentAlert -SqlInstance sql1 -Alert 'Severity 025: Fatal Error', 'Error Number 825', 'Error Number 824' -Enabled
Changes multiple alerts to enabled.
 
.EXAMPLE
Set-DbaAgentAlert -SqlInstance sql1, sql2, sql3 -Alert 'Severity 025: Fatal Error', 'Error Number 825', 'Error Number 824' -Enabled
Changes multiple alerts to enabled on multiple servers.
 
.EXAMPLE
Set-DbaAgentAlert -SqlInstance sql1 -Alert 'Severity 025: Fatal Error' -Disabled -Whatif
Doesn't Change the alert but shows what would happen.
 
#>

    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = "Low")]
    param (
        [Alias("ServerInstance", "SqlServer")]
        [DbaInstanceParameter[]]$SqlInstance,
        [PSCredential]$SqlCredential,
        [object[]]$Alert,
        [string]$NewName,
        [switch]$Enabled,
        [switch]$Disabled,
        [switch]$Force,
        [parameter(ValueFromPipeline)]
        [Microsoft.SqlServer.Management.Smo.Agent.Alert[]]$InputObject,
        [switch][Alias('Silent')]
        $EnableException
    )

    begin {
}
        process {

            if (Test-FunctionInterrupt) { return }

            if ((-not $InputObject) -and (-not $Alert)) {
                Stop-Function -Message "You must specify an alert name or pipe in results from another command" -Target $sqlinstance
                return
            }

            foreach ($instance in $sqlinstance) {
                # Try connecting to the instance
                Write-Message -Message "Connecting to $instance" -Level Verbose
                try {
                    $server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $SqlCredential
                }
                catch {
                    Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
                }
            foreach ($a in $Alert) {
                    # Check if the alert exists
                    if ($server.JobServer.Alerts.Name -notcontains $a) {
                        Stop-Function -Message "Alert $a doesn't exists on $instance" -Target $instance
                    }
                    else {
                        # Get the alert
                        try {
                            $InputObject += $server.JobServer.Alerts[$a]

                            # Refresh the object
                            $InputObject.Refresh()
                        }
                        catch {
                            Stop-Function -Message "Something went wrong retrieving the alert" -Target $a -ErrorRecord $_ -Continue
                        }
                    }
                }
            }

            foreach ($currentalert in $InputObject) {
                $server = $currentalert.Parent.Parent

                #region alert options
                # Settings the options for the alert
                if ($NewName) {
                    Write-Message -Message "Setting alert name to $NewName" -Level Verbose
                    $currentalert.Rename($NewName)
                }

                if ($Enabled) {
                    Write-Message -Message "Setting alert to enabled" -Level Verbose
                    $currentalert.IsEnabled = $true
                }

                if ($Disabled) {
                    Write-Message -Message "Setting alert to disabled" -Level Verbose
                    $currentalert.IsEnabled = $false
                }

                #endregion alert options

                # Execute
                if ($PSCmdlet.ShouldProcess($SqlInstance, "Changing the alert $a")) {
                    try {
                        Write-Message -Message "Changing the alert" -Level Verbose

                        # Change the alert
                        $currentalert.Alter()
                    }
                    catch {
                        Stop-Function -Message "Something went wrong changing the alert" -ErrorRecord $_ -Target $instance -Continue
                    }
                    Get-DbaAgentAlert -SqlInstance $server | Where-Object Name -eq $currentalert.name
                }
            }
        }
}
        {
            Write-Message -Message "Finished changing alert(s)" -Level Verbose
        }