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
|
#!powershell
# -*- coding: utf-8 -*-
# (c) 2022, John McCall (@lowlydba)
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
#AnsibleRequires -CSharpUtil Ansible.Basic
#AnsibleRequires -PowerShell ansible_collections.lowlydba.sqlserver.plugins.module_utils._SqlServerUtils
#Requires -Modules @{ ModuleName="dbatools"; ModuleVersion="2.0.0" }
$ErrorActionPreference = "Stop"
$spec = @{
supports_check_mode = $true
options = @{
job = @{type = 'str'; required = $true }
step_id = @{type = 'int'; required = $false }
step_name = @{type = 'str'; required = $false }
database = @{type = 'str'; required = $false; default = 'master' }
subsystem = @{type = 'str'; required = $false; default = 'TransactSql'
choices = @('CmdExec', 'Distribution', 'LogReader', 'Merge', 'PowerShell', 'QueueReader', 'Snapshot', 'Ssis', 'TransactSql')
}
command = @{type = 'str'; required = $false }
on_success_action = @{type = 'str'; required = $false; default = 'QuitWithSuccess'
choices = @('QuitWithSuccess', 'QuitWithFailure', 'GoToNextStep', 'GoToStep')
}
on_success_step_id = @{type = 'int'; required = $false; default = 0 }
on_fail_action = @{type = 'str'; required = $false; default = 'QuitWithFailure'
choices = @('QuitWithSuccess', 'QuitWithFailure', 'GoToNextStep', 'GoToStep')
}
on_fail_step_id = @{type = 'int'; required = $false; default = 0 }
retry_attempts = @{type = 'int'; required = $false; default = 0 }
retry_interval = @{type = 'int'; required = $false; default = 0 }
state = @{type = 'str'; required = $false; default = 'present'; choices = @('present', 'absent') }
}
required_together = @(
, @('retry_attempts', 'retry_interval')
)
required_one_of = @(
, @('step_id', 'step_name')
)
}
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec, @(Get-LowlyDbaSqlServerAuthSpec))
$sqlInstance, $sqlCredential = Get-SqlCredential -Module $module
$job = $module.Params.job
$stepId = $module.Params.step_id
$stepName = $module.Params.step_name
$database = $module.Params.database
$subsystem = $module.Params.subsystem
$command = $module.Params.command
$onSuccessAction = $module.Params.on_success_action
[nullable[int]]$onSuccessStepId = $module.Params.on_success_step_id
$onFailAction = $module.Params.on_fail_action
[nullable[int]]$onFailStepId = $module.Params.on_fail_step_id
[int]$retryAttempts = $module.Params.retry_attempts
[nullable[int]]$retryInterval = $module.Params.retry_interval
$state = $module.Params.state
$checkMode = $module.CheckMode
$module.Result.changed = $false
$PSDefaultParameterValues = @{ "*:EnableException" = $true; "*:Confirm" = $false; "*:WhatIf" = $checkMode }
# Configure Agent job step
try {
$existingJobSteps = Get-DbaAgentJobStep -SqlInstance $SqlInstance -SqlCredential $sqlCredential -Job $job
$existingJobStep = $existingJobSteps | Where-Object Name -eq $stepName
if ($state -eq "absent") {
if ($null -eq $existingJobStep) {
# try fetching name by id if we only care about removing
$existingJobStep = $existingJobSteps | Where-Object Id -eq $stepId
$stepName = $existingJobStep.Name
}
if ($existingJobStep) {
$removeStepSplat = @{
SqlInstance = $sqlInstance
SqlCredential = $sqlCredential
Job = $job
StepName = $stepName
}
$output = Remove-DbaAgentJobStep @removeStepSplat
$module.Result.changed = $true
}
}
elseif ($state -eq "present") {
if (!($stepName) -or !($stepId)) {
$module.FailJson("Step name must be specified when state=present.")
}
$jobStepParams = @{
SqlInstance = $sqlInstance
SqlCredential = $sqlCredential
Job = $job
StepName = $stepName
Database = $database
SubSystem = $subsystem
OnSuccessAction = $onSuccessAction
OnSuccessStepId = $onSuccessStepId
OnFailAction = $onFailAction
OnFailStepId = $onFailStepId
RetryAttempts = $retryAttempts
RetryInterval = $retryInterval
WhatIf = $checkMode
}
if ($null -ne $command) {
$jobStepParams.Add("Command", $command)
}
# No existing job step
if ($null -eq $existingJobStep) {
$jobStepParams.Add("StepId", $stepId)
$output = New-DbaAgentJobStep @jobStepParams
$module.Result.changed = $true
}
# Update existing
else {
# Validate step name isn't taken already - must be unique within a job
if ($existingJobStep.Name -eq $StepName -and $existingJobStep.ID -ne $stepId) {
$module.FailJson("There is already a step named '$StepName' for this job with an ID of $($existingJobStep.ID).")
}
# Reference by old name in case new name differs for step id
$jobStepParams.StepName = $existingJobStep.Name
$jobStepParams.Add("NewName", $StepName)
# Need to serialize to prevent SMO auto refreshing
$old = ConvertTo-SerializableObject -InputObject $existingJobStep -UseDefaultProperty $false
$output = Set-DbaAgentJobStep @jobStepParams
if ($null -ne $output) {
$compareProperty = @(
"Name"
"DatabaseName"
"Command"
"Subsystem"
"OnFailAction"
"OnFailActionStep"
"OnSuccessAction"
"OnSuccessActionStep"
"RetryAttempts"
"RetryInterval"
)
$diff = Compare-Object -ReferenceObject $output -DifferenceObject $old -Property $compareProperty
}
if ($diff -or $checkMode) {
$module.Result.changed = $true
}
}
}
if ($null -ne $output) {
$resultData = ConvertTo-SerializableObject -InputObject $output
$module.Result.data = $resultData
}
$module.ExitJson()
}
catch {
$module.FailJson("Error configuring SQL Agent job step.", $_)
}
|