| 1 |
#!powershell |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# (c) 2022, John McCall (@lowlydba) |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
#AnsibleRequires -CSharpUtil Ansible.Basic |
| 8 |
#AnsibleRequires -PowerShell ansible_collections.lowlydba.sqlserver.plugins.module_utils._SqlServerUtils |
| 9 |
#Requires -Modules @{ ModuleName="dbatools"; ModuleVersion="2.0.0" } |
| 10 |
|
| 11 |
$ErrorActionPreference = "Stop" |
| 12 |
|
| 13 |
$spec = @{ |
| 14 |
supports_check_mode = $true |
| 15 |
options = @{ |
| 16 |
job = @{type = 'str'; required = $true } |
| 17 |
step_id = @{type = 'int'; required = $false } |
| 18 |
step_name = @{type = 'str'; required = $false } |
| 19 |
database = @{type = 'str'; required = $false; default = 'master' } |
| 20 |
subsystem = @{type = 'str'; required = $false; default = 'TransactSql' |
| 21 |
choices = @('CmdExec', 'Distribution', 'LogReader', 'Merge', 'PowerShell', 'QueueReader', 'Snapshot', 'Ssis', 'TransactSql') |
| 22 |
} |
| 23 |
command = @{type = 'str'; required = $false } |
| 24 |
on_success_action = @{type = 'str'; required = $false; default = 'QuitWithSuccess' |
| 25 |
choices = @('QuitWithSuccess', 'QuitWithFailure', 'GoToNextStep', 'GoToStep') |
| 26 |
} |
| 27 |
on_success_step_id = @{type = 'int'; required = $false; default = 0 } |
| 28 |
on_fail_action = @{type = 'str'; required = $false; default = 'QuitWithFailure' |
| 29 |
choices = @('QuitWithSuccess', 'QuitWithFailure', 'GoToNextStep', 'GoToStep') |
| 30 |
} |
| 31 |
on_fail_step_id = @{type = 'int'; required = $false; default = 0 } |
| 32 |
retry_attempts = @{type = 'int'; required = $false; default = 0 } |
| 33 |
retry_interval = @{type = 'int'; required = $false; default = 0 } |
| 34 |
output_file = @{type = 'str'; required = $false } |
| 35 |
state = @{type = 'str'; required = $false; default = 'present'; choices = @('present', 'absent') } |
| 36 |
} |
| 37 |
required_together = @( |
| 38 |
, @('retry_attempts', 'retry_interval') |
| 39 |
) |
| 40 |
required_one_of = @( |
| 41 |
, @('step_id', 'step_name') |
| 42 |
) |
| 43 |
} |
| 44 |
|
| 45 |
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec, @(Get-LowlyDbaSqlServerAuthSpec)) |
| 46 |
$sqlInstance, $sqlCredential = Get-SqlCredential -Module $module |
| 47 |
$job = $module.Params.job |
| 48 |
$stepId = $module.Params.step_id |
| 49 |
$stepName = $module.Params.step_name |
| 50 |
$database = $module.Params.database |
| 51 |
$subsystem = $module.Params.subsystem |
| 52 |
$command = $module.Params.command |
| 53 |
$onSuccessAction = $module.Params.on_success_action |
| 54 |
[nullable[int]]$onSuccessStepId = $module.Params.on_success_step_id |
| 55 |
$onFailAction = $module.Params.on_fail_action |
| 56 |
[nullable[int]]$onFailStepId = $module.Params.on_fail_step_id |
| 57 |
[int]$retryAttempts = $module.Params.retry_attempts |
| 58 |
[nullable[int]]$retryInterval = $module.Params.retry_interval |
| 59 |
$outputFile = $module.Params.output_file |
| 60 |
$state = $module.Params.state |
| 61 |
$checkMode = $module.CheckMode |
| 62 |
$module.Result.changed = $false |
| 63 |
$PSDefaultParameterValues = @{ "*:EnableException" = $true; "*:Confirm" = $false; "*:WhatIf" = $checkMode } |
| 64 |
|
| 65 |
# Configure Agent job step |
| 66 |
try { |
| 67 |
$existingJobSteps = Get-DbaAgentJobStep -SqlInstance $SqlInstance -SqlCredential $sqlCredential -Job $job |
| 68 |
$existingJobStep = $existingJobSteps | Where-Object Name -eq $stepName |
| 69 |
|
| 70 |
if ($state -eq "absent") { |
| 71 |
if ($null -eq $existingJobStep) { |
| 72 |
# try fetching name by id if we only care about removing |
| 73 |
$existingJobStep = $existingJobSteps | Where-Object Id -eq $stepId |
| 74 |
$stepName = $existingJobStep.Name |
| 75 |
} |
| 76 |
if ($existingJobStep) { |
| 77 |
$removeStepSplat = @{ |
| 78 |
SqlInstance = $sqlInstance |
| 79 |
SqlCredential = $sqlCredential |
| 80 |
Job = $job |
| 81 |
StepName = $stepName |
| 82 |
} |
| 83 |
$output = Remove-DbaAgentJobStep @removeStepSplat |
| 84 |
$module.Result.changed = $true |
| 85 |
} |
| 86 |
} |
| 87 |
elseif ($state -eq "present") { |
| 88 |
if (!($stepName) -or !($stepId)) { |
| 89 |
$module.FailJson("Step name and step_id must be specified when state=present.") |
| 90 |
} |
| 91 |
$jobStepParams = @{ |
| 92 |
SqlInstance = $sqlInstance |
| 93 |
SqlCredential = $sqlCredential |
| 94 |
Job = $job |
| 95 |
StepName = $stepName |
| 96 |
Database = $database |
| 97 |
SubSystem = $subsystem |
| 98 |
OnSuccessAction = $onSuccessAction |
| 99 |
OnSuccessStepId = $onSuccessStepId |
| 100 |
OnFailAction = $onFailAction |
| 101 |
OnFailStepId = $onFailStepId |
| 102 |
RetryAttempts = $retryAttempts |
| 103 |
RetryInterval = $retryInterval |
| 104 |
} |
| 105 |
if ($null -ne $command) { |
| 106 |
$jobStepParams.Add("Command", $command) |
| 107 |
} |
| 108 |
|
| 109 |
# No existing job step |
| 110 |
if ($null -eq $existingJobStep) { |
| 111 |
$jobStepParams.Add("StepId", $stepId) |
| 112 |
$output = New-DbaAgentJobStep @jobStepParams |
| 113 |
$module.Result.changed = $true |
| 114 |
|
| 115 |
# Set output file if specified |
| 116 |
if ($null -ne $outputFile -and $outputFile -ne "") { |
| 117 |
$setOutputFileSplat = @{ |
| 118 |
SqlInstance = $sqlInstance |
| 119 |
SqlCredential = $sqlCredential |
| 120 |
Job = $job |
| 121 |
Step = $stepName |
| 122 |
OutputFile = $outputFile |
| 123 |
} |
| 124 |
$outputFileResult = Set-DbaAgentJobOutputFile @setOutputFileSplat |
| 125 |
if ($null -ne $outputFileResult) { |
| 126 |
$module.Result.changed = $true |
| 127 |
# Add the OutputFileName property from the Set-DbaAgentJobOutputFile result |
| 128 |
if ($null -ne $outputFileResult.OutputFileName) { |
| 129 |
Add-Member -InputObject $output -MemberType NoteProperty -Name "OutputFileName" -Value $outputFileResult.OutputFileName -Force |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
# Ensure OutputFileName property is always available for consistency |
| 135 |
if ($null -ne $output -and -not ($output.PSObject.Properties.Name -contains "OutputFileName")) { |
| 136 |
Add-Member -InputObject $output -MemberType NoteProperty -Name "OutputFileName" -Value $output.OutputFileName -Force |
| 137 |
} |
| 138 |
} |
| 139 |
# Update existing |
| 140 |
else { |
| 141 |
# Validate step name isn't taken already - must be unique within a job |
| 142 |
if ($existingJobStep.Name -eq $StepName -and $existingJobStep.ID -ne $stepId) { |
| 143 |
$module.FailJson("There is already a step named '$StepName' for this job with an ID of $($existingJobStep.ID).") |
| 144 |
} |
| 145 |
|
| 146 |
# Reference by old name in case new name differs for step id |
| 147 |
$jobStepParams.StepName = $existingJobStep.Name |
| 148 |
$jobStepParams.Add("NewName", $StepName) |
| 149 |
|
| 150 |
# Need to serialize to prevent SMO auto refreshing |
| 151 |
$old = ConvertTo-SerializableObject -InputObject $existingJobStep -UseDefaultProperty $false |
| 152 |
$output = Set-DbaAgentJobStep @jobStepParams |
| 153 |
if ($null -ne $output) { |
| 154 |
$compareProperty = @( |
| 155 |
"Name" |
| 156 |
"DatabaseName" |
| 157 |
"Command" |
| 158 |
"Subsystem" |
| 159 |
"OnFailAction" |
| 160 |
"OnFailActionStep" |
| 161 |
"OnSuccessAction" |
| 162 |
"OnSuccessActionStep" |
| 163 |
"RetryAttempts" |
| 164 |
"RetryInterval" |
| 165 |
"OutputFileName" |
| 166 |
) |
| 167 |
$diff = Compare-Object -ReferenceObject $output -DifferenceObject $old -Property $compareProperty |
| 168 |
} |
| 169 |
|
| 170 |
# Set output file if specified and different from current |
| 171 |
if ($null -ne $outputFile -and $outputFile -ne "" -and $existingJobStep.OutputFileName -ne $outputFile) { |
| 172 |
$setOutputFileSplat = @{ |
| 173 |
SqlInstance = $sqlInstance |
| 174 |
SqlCredential = $sqlCredential |
| 175 |
Job = $job |
| 176 |
Step = $stepName # Use the new step name since Set-DbaAgentJobStep already renamed it |
| 177 |
OutputFile = $outputFile |
| 178 |
} |
| 179 |
$outputFileResult = Set-DbaAgentJobOutputFile @setOutputFileSplat |
| 180 |
if ($null -ne $outputFileResult) { |
| 181 |
$module.Result.changed = $true |
| 182 |
# Add the OutputFileName property from the Set-DbaAgentJobOutputFile result |
| 183 |
if ($null -ne $outputFileResult.OutputFileName) { |
| 184 |
Add-Member -InputObject $output -MemberType NoteProperty -Name "OutputFileName" -Value $outputFileResult.OutputFileName -Force |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
# Ensure OutputFileName property is always available |
| 190 |
if ($null -ne $output -and -not ($output.PSObject.Properties.Name -contains "OutputFileName")) { |
| 191 |
Add-Member -InputObject $output -MemberType NoteProperty -Name "OutputFileName" -Value $output.OutputFileName -Force |
| 192 |
} |
| 193 |
|
| 194 |
if ($diff -or $checkMode) { |
| 195 |
$module.Result.changed = $true |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
if ($null -ne $output) { |
| 201 |
$resultData = ConvertTo-SerializableObject -InputObject $output |
| 202 |
$module.Result.data = $resultData |
| 203 |
} |
| 204 |
$module.ExitJson() |
| 205 |
} |
| 206 |
catch { |
| 207 |
$module.FailJson("Error configuring SQL Agent job step.", $_) |
| 208 |
} |