|
| 1 | +function Remove-DbaAgentJobSchedule { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Detaches a schedule from a SQL Server Agent job without removing the schedule. |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + Detaches one or more schedules from a SQL Server Agent job without deleting the schedule itself. This is equivalent to executing sp_detach_schedule in T-SQL. |
| 8 | +
|
| 9 | + This is particularly useful when a schedule is shared between multiple jobs and you need to stop a specific job from running on that schedule without affecting other jobs that use the same schedule. The schedule remains in SQL Server Agent and can be reattached to the job or attached to other jobs at any time. |
| 10 | +
|
| 11 | + Use Set-DbaAgentJob with the -Schedule parameter to reattach a schedule to a job after detaching it. |
| 12 | +
|
| 13 | + .PARAMETER SqlInstance |
| 14 | + The target SQL Server instance or instances. This can be a collection and receive pipeline input to allow the function to be executed against multiple SQL Server instances. |
| 15 | +
|
| 16 | + .PARAMETER SqlCredential |
| 17 | + Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential). |
| 18 | +
|
| 19 | + Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported. |
| 20 | +
|
| 21 | + For MFA support, please use Connect-DbaInstance. |
| 22 | +
|
| 23 | + .PARAMETER Job |
| 24 | + The name of the SQL Agent job(s) from which to detach the schedule. Required when using -SqlInstance. |
| 25 | +
|
| 26 | + .PARAMETER Schedule |
| 27 | + The name of the schedule(s) to detach from the job. The schedule itself is not deleted; only the association between the job and schedule is removed. |
| 28 | +
|
| 29 | + .PARAMETER InputObject |
| 30 | + Accepts job objects from the pipeline, typically from Get-DbaAgentJob output. Use this when you want to filter or retrieve jobs first, then pipe the results for schedule detachment. |
| 31 | +
|
| 32 | + .PARAMETER WhatIf |
| 33 | + If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run. |
| 34 | +
|
| 35 | + .PARAMETER Confirm |
| 36 | + If this switch is enabled, you will be prompted for confirmation before executing any operations that change state. |
| 37 | +
|
| 38 | + .PARAMETER EnableException |
| 39 | + By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message. |
| 40 | + This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting. |
| 41 | + Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch. |
| 42 | +
|
| 43 | + .OUTPUTS |
| 44 | + PSCustomObject |
| 45 | +
|
| 46 | + Returns one object per detach operation, containing the result and details. |
| 47 | +
|
| 48 | + Properties: |
| 49 | + - ComputerName: The computer name of the SQL Server instance |
| 50 | + - InstanceName: The SQL Server instance name |
| 51 | + - SqlInstance: The full SQL Server instance name (computer\instance) |
| 52 | + - Job: The name of the job from which the schedule was detached |
| 53 | + - Schedule: The name of the schedule that was detached |
| 54 | + - ScheduleId: The numeric ID of the schedule |
| 55 | + - ScheduleUid: The unique GUID identifier of the schedule |
| 56 | + - Status: Result of the operation ("Detached" for success, or error message for failures) |
| 57 | + - IsDetached: Boolean indicating if the schedule was successfully detached |
| 58 | +
|
| 59 | + .NOTES |
| 60 | + Tags: Agent, Job, JobSchedule, Schedule |
| 61 | + Author: the dbatools team + Claude |
| 62 | +
|
| 63 | + Website: https://dbatools.io |
| 64 | + Copyright: (c) 2018 by dbatools, licensed under MIT |
| 65 | + License: MIT https://opensource.org/licenses/MIT |
| 66 | +
|
| 67 | + .LINK |
| 68 | + https://dbatools.io/Remove-DbaAgentJobSchedule |
| 69 | +
|
| 70 | + .EXAMPLE |
| 71 | + PS C:\> Remove-DbaAgentJobSchedule -SqlInstance sql1 -Job Job1 -Schedule SharedSchedule |
| 72 | +
|
| 73 | + Detaches the schedule named 'SharedSchedule' from job 'Job1' on sql1. The schedule itself is not deleted and remains available for other jobs. |
| 74 | +
|
| 75 | + .EXAMPLE |
| 76 | + PS C:\> Remove-DbaAgentJobSchedule -SqlInstance sql1 -Job Job1 -Schedule Schedule1, Schedule2 |
| 77 | +
|
| 78 | + Detaches multiple schedules from a single job on sql1. |
| 79 | +
|
| 80 | + .EXAMPLE |
| 81 | + PS C:\> Remove-DbaAgentJobSchedule -SqlInstance sql1, sql2 -Job Job1 -Schedule SharedSchedule |
| 82 | +
|
| 83 | + Detaches the schedule from job 'Job1' on multiple SQL Server instances. |
| 84 | +
|
| 85 | + .EXAMPLE |
| 86 | + PS C:\> Get-DbaAgentJob -SqlInstance sql1 -Job Job1 | Remove-DbaAgentJobSchedule -Schedule SharedSchedule |
| 87 | +
|
| 88 | + Detaches the schedule 'SharedSchedule' from job 'Job1' using pipeline input. |
| 89 | +
|
| 90 | + .EXAMPLE |
| 91 | + PS C:\> Get-DbaAgentJob -SqlInstance sql1 | Where-Object Name -like 'Maintenance*' | Remove-DbaAgentJobSchedule -Schedule SharedSchedule |
| 92 | +
|
| 93 | + Detaches 'SharedSchedule' from all jobs whose names start with 'Maintenance' on sql1. |
| 94 | +
|
| 95 | + #> |
| 96 | + [CmdletBinding(SupportsShouldProcess, ConfirmImpact = "High")] |
| 97 | + param ( |
| 98 | + [DbaInstanceParameter[]]$SqlInstance, |
| 99 | + [PSCredential]$SqlCredential, |
| 100 | + [string[]]$Job, |
| 101 | + [Parameter(Mandatory)] |
| 102 | + [ValidateNotNullOrEmpty()] |
| 103 | + [string[]]$Schedule, |
| 104 | + [Parameter(ValueFromPipeline)] |
| 105 | + [Microsoft.SqlServer.Management.Smo.Agent.Job[]]$InputObject, |
| 106 | + [switch]$EnableException |
| 107 | + ) |
| 108 | + begin { |
| 109 | + $jobs = @() |
| 110 | + } |
| 111 | + process { |
| 112 | + foreach ($instance in $SqlInstance) { |
| 113 | + if (-not (Test-Bound -ParameterName Job)) { |
| 114 | + Stop-Function -Message "Parameter -Job is required when using -SqlInstance" -Target $instance -Continue |
| 115 | + } |
| 116 | + |
| 117 | + try { |
| 118 | + $server = Connect-DbaInstance -SqlInstance $instance -SqlCredential $SqlCredential |
| 119 | + } catch { |
| 120 | + Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue |
| 121 | + } |
| 122 | + |
| 123 | + foreach ($jobName in $Job) { |
| 124 | + if ($server.JobServer.Jobs.Name -notcontains $jobName) { |
| 125 | + Stop-Function -Message "Job '$jobName' does not exist on $instance" -Target $instance -Continue |
| 126 | + } |
| 127 | + $jobs += $server.JobServer.Jobs[$jobName] |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + foreach ($jobObject in $InputObject) { |
| 132 | + $jobs += $jobObject |
| 133 | + } |
| 134 | + } |
| 135 | + end { |
| 136 | + # We process in the end block to prevent "Collection was modified; enumeration operation may not execute." |
| 137 | + # if job objects are directly piped from Get-DbaAgentJob. |
| 138 | + foreach ($jobObject in $jobs) { |
| 139 | + $server = $jobObject.Parent.Parent |
| 140 | + |
| 141 | + foreach ($scheduleName in $Schedule) { |
| 142 | + $jobSchedule = $jobObject.JobSchedules | Where-Object { $_.Name -eq $scheduleName } |
| 143 | + |
| 144 | + if (-not $jobSchedule) { |
| 145 | + Stop-Function -Message "Schedule '$scheduleName' is not attached to job '$($jobObject.Name)' on $($server.Name)" -Target $jobObject -Continue |
| 146 | + } |
| 147 | + |
| 148 | + $output = [PSCustomObject]@{ |
| 149 | + ComputerName = $server.ComputerName |
| 150 | + InstanceName = $server.ServiceName |
| 151 | + SqlInstance = $server.DomainInstanceName |
| 152 | + Job = $jobObject.Name |
| 153 | + Schedule = $scheduleName |
| 154 | + ScheduleId = $jobSchedule.Id |
| 155 | + ScheduleUid = $jobSchedule.ScheduleUid |
| 156 | + Status = $null |
| 157 | + IsDetached = $false |
| 158 | + } |
| 159 | + |
| 160 | + if ($PSCmdlet.ShouldProcess($server, "Detaching schedule '$scheduleName' from job '$($jobObject.Name)'")) { |
| 161 | + try { |
| 162 | + Write-Message -Level Verbose -Message "Detaching schedule '$scheduleName' from job '$($jobObject.Name)' on $($server.Name)" |
| 163 | + $jobSchedule.Drop($true) |
| 164 | + $output.Status = "Detached" |
| 165 | + $output.IsDetached = $true |
| 166 | + } catch { |
| 167 | + Stop-Function -Message "Failed to detach schedule '$scheduleName' from job '$($jobObject.Name)' on $($server.Name)" -ErrorRecord $_ -Target $jobObject -Continue |
| 168 | + $output.Status = (Get-ErrorMessage -Record $_) |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + $output |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments