functions/Remove-DbaAgentSchedule.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#ValidationTags#Messaging,FlowControl,Pipeline,CodeStyle#
function Remove-DbaAgentSchedule {
    <#
.SYNOPSIS
Remove-DbaAgentJobSchedule removes a job schedule.
 
.DESCRIPTION
Remove-DbaAgentJobSchedule removes a a job in the SQL Server Agent.
 
.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 Schedule
The name of the job schedule.
 
.PARAMETER InputObject
A collection of schedule (such as returned by Get-DbaAgentSchedule), to be removed.
 
.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.
 
.PARAMETER Force
The force parameter will ignore some errors in the parameters and assume defaults.
It will also remove the any present schedules with the same name for the specific job.
 
.NOTES
Author: Sander Stad (@sqlstad, sqlstad.nl)
Tags: Agent, Job, Schedule
 
Website: https://dbatools.io
Copyright: (C) Chrissy LeMaire, clemaire@gmail.com
License: MIT https://opensource.org/licenses/MIT
 
.LINK
https://dbatools.io/Remove-DbaAgentJobSchedule
 
.EXAMPLE
Remove-DbaAgentSchedule -SqlInstance sql1 -Schedule weekly
Remove the schedule weekly
 
.EXAMPLE
Remove-DbaAgentSchedule -SqlInstance sql1 -Schedule weekly -Force
Remove the schedule weekly from the job even if the schedule is being used by another job.
 
.EXAMPLE
Remove-DbaAgentSchedule -SqlInstance sql1 -Schedule daily, weekly
Remove multiple schedule
 
.EXAMPLE
Remove-DbaAgentSchedule -SqlInstance sql1, sql2, sql3 -Schedule daily, weekly
Remove the schedule on multiple servers for multiple schedules
 
.EXAMPLE
sql1, sql2, sql3 | Remove-DbaAgentSchedule -Schedule daily, weekly
Remove the schedule on multiple servers using pipe line
 
.EXAMPLE
Get-DbaAgentSchedule -SqlInstance sql1 -Schedule sched1, sched2, sched3 | Remove-DbaAgentSchedule
 
Remove the schedules using a pipeline
 
#>


    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = "Low")]

    param (
        [parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = "instance")]
        [Alias("ServerInstance", "SqlServer")]
        [DbaInstanceParameter[]]$SqlInstance,
        [System.Management.Automation.PSCredential]
        $SqlCredential,
        [Parameter(Mandatory = $true, ParameterSetName = "instance")]
        [ValidateNotNullOrEmpty()]
        [Alias("Schedules")]
        [object[]]$Schedule,
        [Parameter(ValueFromPipeline, Mandatory, ParameterSetName = "schedules")]
        [Microsoft.SqlServer.Management.Smo.Agent.ScheduleBase[]]$InputObject,
        [Alias('Silent')]
        [switch]$EnableException,
        [switch]$Force
    )

    process {

        foreach ($instance in $sqlinstance) {
            # Try connecting to the instance
            Write-Message -Message "Attempting to connect to $instance" -Level Verbose
            try {
                $server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $SqlCredential
            }
            catch {
                Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
            }

            $InputObject += $server.JobServer.SharedSchedules | Where-Object { $_.Name -in $Schedule }

        } # foreach object instance

        foreach ($s in $InputObject) {

            if ($Server.JobServer.SharedSchedules.Name -contains $s.Name) {
                # Get job count
                $jobCount = $Server.JobServer.SharedSchedules[$s].JobCount

                # Check if the schedule is shared among other jobs
                if ($jobCount -ge 1 -and -not $Force) {
                    Stop-Function -Message "The schedule $s is shared connected to one or more jobs. If removal is neccesary use -Force." -Target $instance -Continue
                }

                # Remove the job schedule
                if ($PSCmdlet.ShouldProcess($instance, "Removing schedule $s on $instance")) {
                    # Loop through each of the schedules and drop them
                    Write-Message -Message "Removing schedule $s on $instance" -Level Verbose

                    #Check if jobs use the schedule
                    if ($jobCount -ge 1) {
                        # Get the job object
                        $smoSchedules = $server.JobServer.SharedSchedules | Where-Object {($_.Name -eq $s.Name)}

                        Write-Message -Message "Schedule $sched is used in one or more jobs. Removing it for each job." -Level Verbose

                        # Loop through each if the schedules
                        foreach ($smoSchedule in $smoSchedules) {

                            # Get the job ids
                            $jobGuids = $Server.JobServer.SharedSchedules[$smoSchedule].EnumJobReferences()

                            if (($jobCount -gt 1 -and $Force) -or $jobCount -eq 1) {

                                # Loop though each of the jobs
                                foreach ($guid in $jobGuids) {
                                    # Get the job object
                                    $smoJob = $Server.JobServer.GetJobByID($guid)

                                    # Get the job schedule
                                    $jobSchedules = $Server.JobServer.Jobs[$smoJob].JobSchedules | Where-Object {$_.Name -eq $smoSchedule}

                                    foreach ($jobSchedule in $jobSchedules) {
                                        try {
                                            Write-Message -Message "Removing the schedule $jobSchedule for job $smoJob" -Level Verbose

                                            $jobSchedule.Drop()
                                        }
                                        catch {
                                            Stop-Function -Message  "Something went wrong removing the job schedule" -Target $instance -ErrorRecord $_ -Continue
                                        }
                                    }
                                } # foreach guid
                            } # if jobcount

                        } # foreach smoschedule
                    } # if jobcount ge 1

                    Write-Message -Message "Removing schedules that are not being used by other jobs." -Level Verbose

                    # Get the schedules
                    $smoSchedules = $server.JobServer.SharedSchedules | Where-Object {($_.Name -eq $s.Name) -and ($_.JobCount -eq 0)}

                    # Remove the schedules that have no jobs
                    foreach ($smoSchedule in $smoSchedules) {
                        try {
                            $smoSchedule.Drop()
                        }
                        catch {
                            Stop-Function -Message  "Something went wrong removing the schedule" -Target $instance -ErrorRecord $_ -Continue
                        }
                    } # foreach schedule
                } # should process
            } # if contains schedule
            else {
                Stop-Function -Message "Schedule $s is not present on instance $instance" -Target $instance -Continue
            }
        } #foreach object schedule

    } # process

    end {
        Write-Message -Message "Finished removing jobs schedule(s)." -Level Verbose
    }
}