Debian Codegraph @

ansible_collections/lowlydba/sqlserver/plugins/modules/agent_job.ps1

raw
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 description = @{type = 'str'; required = $false; }
18 category = @{type = 'str'; required = $false; }
19 enabled = @{type = 'bool'; required = $false; default = $true }
20 owner_login = @{type = 'str'; required = $false; }
21 start_step_id = @{type = 'int'; required = $false; }
22 schedule = @{type = 'str'; required = $false; }
23 force = @{type = 'bool'; required = $false; default = $false }
24 state = @{type = 'str'; required = $false; default = 'present'; choices = @('present', 'absent') }
25 }
26 }
27
28 $module = [Ansible.Basic.AnsibleModule]::Create($args, $spec, @(Get-LowlyDbaSqlServerAuthSpec))
29 $sqlInstance, $sqlCredential = Get-SqlCredential -Module $module
30 $job = $module.Params.job
31 $description = $module.Params.description
32 $enabled = $module.Params.enabled
33 $ownerLogin = $module.Params.owner_login
34 $category = $module.Params.category
35 $schedule = $module.Params.schedule
36 [nullable[int]]$startStepId = $module.Params.start_step_id
37 $force = $module.Params.force
38 $state = $module.Params.state
39 $checkMode = $module.CheckMode
40 $module.Result.changed = $false
41
42 # Configure Agent job
43 try {
44 $existingJob = Get-DbaAgentJob -SqlInstance $sqlInstance -SqlCredential $sqlCredential -Job $job -EnableException
45 $output = $existingJob
46
47 if ($state -eq "absent") {
48 if ($null -ne $existingJob) {
49 $output = $existingJob | Remove-DbaAgentJob -Confirm:$false -WhatIf:$checkMode -EnableException
50 $module.Result.changed = $true
51 }
52 }
53 elseif ($state -eq "present") {
54 $jobParams = @{
55 SqlInstance = $sqlInstance
56 SqlCredential = $sqlCredential
57 Job = $job
58 WhatIf = $checkMode
59 Force = $force
60 EnableException = $true
61 }
62
63 if ($enabled -eq $false) {
64 $jobParams.add("Disabled", $true)
65 }
66
67 if ($null -ne $ownerLogin) {
68 $jobParams.add("OwnerLogin", $ownerLogin)
69 }
70
71 if ($null -ne $schedule) {
72 $jobParams.add("Schedule", $schedule)
73 }
74
75 if ($null -ne $category) {
76 $jobParams.add("Category", $category)
77 }
78
79 if ($null -ne $description) {
80 $jobParams.add("Description", $description)
81 }
82
83 if ($null -ne $startStepID) {
84 $jobParams.add("StartStepId", $startStepID)
85 }
86
87 # Create new job
88 if ($null -eq $existingJob) {
89 try {
90 $null = New-DbaAgentJob @jobParams
91 # Explicitly fetch the new job to make sure results don't suffer from SMO / Agent stale data bugs
92 $output = Get-DbaAgentJob -SqlInstance $sqlInstance -SqlCredential $sqlCredential -Job $job -EnableException
93 }
94 catch {
95 $module.FailJson("Failed creating new agent job: $($_.Exception.Message)", $_)
96 }
97 $module.Result.changed = $true
98 }
99 # Job exists
100 else {
101 # Need to serialize to prevent SMO auto refreshing
102 $old = ConvertTo-SerializableObject -InputObject $existingJob -UseDefaultProperty $false
103 if ($enabled -eq $true) {
104 $jobParams.Add("Enabled", $true)
105 }
106 $output = Set-DbaAgentJob @jobParams
107 if ($null -ne $output) {
108 $compareProperty = @(
109 "Category"
110 "Enabled"
111 "Name"
112 "OwnerLoginName"
113 "HasSchedule"
114 "Description"
115 "StartStepId"
116 )
117 $diff = Compare-Object -ReferenceObject $output -DifferenceObject $old -Property $compareProperty
118 if ($diff -or $checkMode) {
119 $module.Result.changed = $true
120 }
121 }
122 }
123 }
124
125 if ($output) {
126 $resultData = ConvertTo-SerializableObject -InputObject $output
127 $module.Result.data = $resultData
128 }
129 $module.ExitJson()
130 }
131 catch {
132 $module.FailJson("Error configuring SQL Agent job: $($_.Exception.Message)", $_)
133 }