functions/Remove-DbaAgentJobStep.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
function Remove-DbaAgentJobStep {
    <#
.SYNOPSIS
Remove-DbaAgentJobStep removes a job step.
 
.DESCRIPTION
Remove-DbaAgentJobStep removes a job step 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
Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted. To use:
$scred = Get-Credential, then pass $scred object to the -SqlCredential parameter.
To connect as a different Windows user, run PowerShell as that user.
 
.PARAMETER Job
The name of the job. Can be null if the the job id is being used.
 
.PARAMETER StepName
The name of the step.
  
.PARAMETER KeepHistory
Specifies to keep the history for the job. By default is history is deleted.
 
.PARAMETER KeepUnusedSchedule
Specifies to keep the schedules attached to this job if they are not attached to any other job. By default the unused schedule is deleted.
 
.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 Silent
Use this switch to disable any kind of verbose messages
 
.NOTES
Original Author: Sander Stad (@sqlstad, sqlstad.nl)
Tags: Agent, Job, Job Step
  
Website: https://dbatools.io
Copyright: (C) Chrissy LeMaire, clemaire@gmail.com
License: GNU GPL v3 https://opensource.org/licenses/GPL-3.0
 
.LINK
https://dbatools.io/Remove-DbaAgentJobStep
 
.EXAMPLE
Remove-DbaAgentJobStep -SqlInstance sql1 -Job Job1 -StepName Step1
Remove the job step from the job
 
.EXAMPLE
Remove-DbaAgentJobStep -SqlInstance sql1 -Job Job1, Job2, Job3 -StepName Step1
Remove the job step from the job for multiple jobs
 
.EXAMPLE
Remove-DbaAgentJobStep -SqlInstance sql1, sql2, sql3 -Job Job1 -StepName Step1
Remove the job step from the job on multiple servers
 
 
.EXAMPLE
sql1, sql2, sql3 | Remove-DbaAgentJobStep -Job Job1 -StepName Step1
Remove the job step from the job on multiple servers using pipeline
 
#>
    
    
    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = "Low")]
    param (
        [parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [Alias("ServerInstance", "SqlServer")]
        [DbaInstanceParameter[]]$SqlInstance,
        [Parameter(Mandatory = $false)]
        [PSCredential]$SqlCredential,
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [object[]]$Job,
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$StepName,
        [Parameter(Mandatory = $false)]
        [switch]$Silent
    )
    
    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
            }
            
            foreach ($j in $Job) {
                
                # Check if the job exists
                if ($Server.JobServer.Jobs.Name -notcontains $j) {
                    Write-Message -Message "Job $j doesn't exists on $instance" -Level Warning
                }
                else {
                    # Check if the job step exists
                    if ($Server.JobServer.Jobs[$j].JobSteps.Name -notcontains $StepName) {
                        Write-Message -Message "Step $StepName doesn't exist for $job on $instance" -Level Warning
                    }
                    else {
                        # Get the job step
                        try {
                            $JobStep = $Server.JobServer.Jobs[$j].JobSteps[$StepName]
                        }
                        catch {
                            Stop-Function -Message "Something went wrong creating the job step. `n$($_.Exception.Message)" -Target $JobStep -Continue -InnerErrorRecord $_
                        }
                        
                        # Execute
                        if ($PSCmdlet.ShouldProcess($instance, "Removing the job step $StepName for job $j")) {
                            try {
                                Write-Message -Message "Removing the job step $StepName for job $j" -Level Output
                                
                                $JobStep.Drop()
                            }
                            catch {
                                Stop-Function -Message "Something went wrong removing the job step. `n$($_.Exception.Message)" -Target $JobStep -Continue -InnerErrorRecord $_
                            }
                        }
                    }
                }
                
            } # foreach object job
        } # foreach object instance
    } # process
    
    end {
        Write-Message -Message "Finished removing the jobs step(s)" -Level Output
    }
}