Debian Codegraph @

ansible_collections/lowlydba/sqlserver/plugins/modules/sa.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 new_name = @{type = 'str'; required = $false; }
17 password = @{type = 'str'; required = $false; no_log = $true }
18 enabled = @{type = 'bool'; required = $false; default = $true }
19 password_must_change = @{type = 'bool'; required = $false }
20 password_policy_enforced = @{type = 'bool'; required = $false }
21 password_expiration_enabled = @{type = 'bool'; required = $false }
22 }
23 }
24 $module = [Ansible.Basic.AnsibleModule]::Create($args, $spec, @(Get-LowlyDbaSqlServerAuthSpec))
25 $sqlInstance, $sqlCredential = Get-SqlCredential -Module $module
26 $newName = $module.Params.new_name
27 if ($null -ne $module.Params.password) {
28 $secPassword = ConvertTo-SecureString -String $module.Params.password -AsPlainText -Force
29 }
30 $enabled = $module.Params.enabled
31 [nullable[bool]]$passwordMustChange = $module.Params.password_must_change
32 [nullable[bool]]$passwordExpirationEnabled = $module.Params.password_expiration_enabled
33 [nullable[bool]]$passwordPolicyEnforced = $module.Params.password_policy_enforced
34 $checkMode = $module.CheckMode
35 $module.Result.changed = $false
36
37 try {
38 $sa = Get-DbaLogin -SqlInstance $SqlInstance -SqlCredential $sqlCredential -EnableException | Where-Object ID -eq 1
39
40 $setLoginSplat = @{ }
41
42 if ($null -ne $newName) {
43 $setLoginSplat.Add("NewName", $newName)
44 if ($sa.Name -ne $newName) {
45 $changed = $true
46 }
47 }
48 if ($null -ne $passwordExpirationEnabled) {
49 if ($sa.PasswordExpirationEnabled -ne $passwordExpirationEnabled) {
50 $changed = $true
51 }
52 if ($passwordExpirationEnabled -eq $true) {
53 $setLoginSplat.add("PasswordExpirationEnabled", $true)
54 }
55 }
56 if ($null -ne $passwordPolicyEnforced) {
57 if ($sa.PasswordPolicyEnforced -ne $passwordPolicyEnforced) {
58 $changed = $true
59 }
60 if ($passwordPolicyEnforced -eq $true) {
61 $setLoginSplat.add("PasswordPolicyEnforced", $true)
62 }
63 }
64 if ($true -eq $passwordMustChange) {
65 if ($sa.PasswordMustChange -ne $passwordMustChange) {
66 $changed = $true
67 }
68 if ($passwordMustChange -eq $true) {
69 $setLoginSplat.add("PasswordMustChange", $true)
70 }
71 }
72 if ($null -ne $secPassword) {
73 $setLoginSplat.add("SecurePassword", $secPassword)
74 }
75 if ($enabled -eq $false) {
76 $disabled = $true
77 $setLoginSplat.add("Disable", $true)
78 }
79 else {
80 $disabled = $false
81 $setLoginSplat.add("Enable", $true)
82 }
83
84 # Check for changes
85 if (($changed -eq $true) -or ($disabled -ne $sa.IsDisabled) -or ($secPassword)) {
86 $output = $sa | Set-DbaLogin @setLoginSplat -WhatIf:$checkMode -EnableException
87 $module.Result.changed = $true
88 }
89 else {
90 $output = $sa
91 }
92
93 if ($null -ne $output) {
94 $resultData = ConvertTo-SerializableObject -InputObject $output
95 $module.Result.data = $resultData
96 }
97 $module.exitJson()
98 }
99 catch {
100 $module.FailJson("Configuring 'sa' login failed: $($_.Exception.Message)", $_)
101 }