Debian Codegraph @

ansible_collections/lowlydba/sqlserver/plugins/modules/agent_job_category.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 category = @{type = 'str'; required = $true }
17 category_type = @{type = 'str'; required = $false; choices = @('LocalJob', 'MultiServerJob', 'None') }
18 state = @{type = 'str'; required = $false; default = 'present'; choices = @('present', 'absent') }
19 }
20 }
21
22 $module = [Ansible.Basic.AnsibleModule]::Create($args, $spec, @(Get-LowlyDbaSqlServerAuthSpec))
23 $sqlInstance, $sqlCredential = Get-SqlCredential -Module $module
24 $category = $module.Params.category
25 $categoryType = $module.Params.category_type
26 $state = $module.Params.state
27 $checkMode = $module.CheckMode
28 $module.Result.changed = $false
29
30 try {
31 $agentJobCategorySplat = @{
32 SqlInstance = $sqlInstance
33 SqlCredential = $sqlCredential
34 Category = $category
35 EnableException = $true
36 }
37 if ($null -ne $categoryType) {
38 $agentJobCategorySplat.Add("CategoryType", $categoryType)
39 }
40 $existingCategory = Get-DbaAgentJobCategory @agentJobCategorySplat
41
42 if ($state -eq "present") {
43 # Create new job category
44 if ($null -eq $existingCategory) {
45 $output = New-DbaAgentJobCategory @agentJobCategorySplat -WhatIf:$checkMode
46 $module.Result.changed = $true
47 }
48 }
49 elseif ($state -eq "absent") {
50 if ($null -ne $existingCategory) {
51 $output = $existingCategory | Remove-DbaAgentJobCategory -WhatIf:$checkMode -EnableException -Confirm:$false
52 $module.Result.changed = $true
53 }
54 }
55
56 if ($null -ne $output) {
57 $resultData = ConvertTo-SerializableObject -InputObject $output
58 $module.Result.data = $resultData
59 }
60 $module.ExitJson()
61 }
62 catch {
63 $module.FailJson("Error configuring SQL Agent job category.", $_)
64 }