| 1 |
#!powershell |
| 2 |
|
| 3 |
# Copyright: (c) 2023, Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
#AnsibleRequires -CSharpUtil Ansible.Basic |
| 7 |
#AnsibleRequires -PowerShell ..module_utils._ADObject |
| 8 |
|
| 9 |
$setParams = @{ |
| 10 |
PropertyInfo = @( |
| 11 |
[PSCustomObject]@{ |
| 12 |
Name = 'delegates' |
| 13 |
Option = @{ |
| 14 |
aliases = 'principals_allowed_to_delegate' |
| 15 |
type = 'add_remove_set' |
| 16 |
} |
| 17 |
Attribute = 'PrincipalsAllowedToDelegateToAccount' |
| 18 |
DNLookup = $true |
| 19 |
} |
| 20 |
[PSCustomObject]@{ |
| 21 |
Name = 'dns_hostname' |
| 22 |
Option = @{ type = 'str' } |
| 23 |
Attribute = 'DNSHostName' |
| 24 |
} |
| 25 |
[PSCustomObject]@{ |
| 26 |
Name = 'do_not_append_dollar_to_sam' |
| 27 |
Option = @{ |
| 28 |
default = $false |
| 29 |
type = 'bool' |
| 30 |
} |
| 31 |
} |
| 32 |
[PSCustomObject]@{ |
| 33 |
Name = 'enabled' |
| 34 |
Option = @{ type = 'bool' } |
| 35 |
Attribute = 'Enabled' |
| 36 |
} |
| 37 |
[PSCustomObject]@{ |
| 38 |
Name = 'kerberos_encryption_types' |
| 39 |
Option = @{ |
| 40 |
type = 'add_remove_set' |
| 41 |
choices = 'aes128', 'aes256', 'des', 'rc4' |
| 42 |
} |
| 43 |
Attribute = 'KerberosEncryptionType' |
| 44 |
CaseInsensitive = $true |
| 45 |
|
| 46 |
New = { |
| 47 |
param($Module, $ADParams, $NewParams) |
| 48 |
|
| 49 |
$encTypes = @( |
| 50 |
$Module.Params.kerberos_encryption_types.add |
| 51 |
$Module.Params.kerberos_encryption_types.set |
| 52 |
) | Select-Object -Unique |
| 53 |
|
| 54 |
$NewParams.KerberosEncryptionType = $encTypes |
| 55 |
$Module.Diff.after.kerberos_encryption_types = $MencTypes |
| 56 |
} |
| 57 |
Set = { |
| 58 |
param($Module, $ADParams, $SetParams, $ADObject) |
| 59 |
|
| 60 |
# This is an enum value and needs custom handling for things like |
| 61 |
# unsetting the values with none. |
| 62 |
$rawValue = $ADObject.KerberosEncryptionType.Value |
| 63 |
|
| 64 |
$existing = foreach ($v in [System.Enum]::GetValues($rawValue.GetType())) { |
| 65 |
if ($rawValue -band $v) { $v.ToString() } |
| 66 |
} |
| 67 |
if ($existing -eq 'None') { |
| 68 |
$existing = @() |
| 69 |
} |
| 70 |
$module.Diff.before.kerberos_encryption_types = $existing |
| 71 |
|
| 72 |
$desired = $Module.Params.kerberos_encryption_types |
| 73 |
$compareParams = @{ |
| 74 |
Existing = $existing |
| 75 |
CaseInsensitive = $true |
| 76 |
} |
| 77 |
$res = Compare-AnsibleADIdempotentList @compareParams @desired |
| 78 |
if ($res.Changed) { |
| 79 |
if ($res.Value) { |
| 80 |
$SetParams.KerberosEncryptionType = $res.Value -join ', ' |
| 81 |
} |
| 82 |
else { |
| 83 |
$SetParams.KerberosEncryptionType = 'None' |
| 84 |
} |
| 85 |
} |
| 86 |
$module.Diff.after.kerberos_encryption_types = $res.Value |
| 87 |
} |
| 88 |
} |
| 89 |
[PSCustomObject]@{ |
| 90 |
Name = 'location' |
| 91 |
Option = @{ type = 'str' } |
| 92 |
Attribute = 'Location' |
| 93 |
} |
| 94 |
[PSCustomObject]@{ |
| 95 |
Name = 'managed_by' |
| 96 |
Option = @{ type = 'raw' } |
| 97 |
Attribute = 'ManagedBy' |
| 98 |
DNLookup = $true |
| 99 |
} |
| 100 |
[PSCustomObject]@{ |
| 101 |
Name = 'sam_account_name' |
| 102 |
Option = @{ type = 'str' } |
| 103 |
Attribute = 'sAMAccountName' |
| 104 |
# New handling is done in PostAction as New-ADComputer cannot |
| 105 |
# set a SAM without the $ suffix. |
| 106 |
Set = { |
| 107 |
param($Module, $ADParams, $SetParams, $ADObject) |
| 108 |
|
| 109 |
# Using the -SAMAccountName parameter will automatically append |
| 110 |
# '$' to the value. We want to set the provided user value |
| 111 |
# which may not have the suffix so we use the raw attribute |
| 112 |
# replacement method. |
| 113 |
$sam = $Module.Params.sam_account_name |
| 114 |
if ($sam -ne $ADObject.SAMAccountName) { |
| 115 |
if (-not $SetParams.ContainsKey('Replace')) { |
| 116 |
$SetParams['Replace'] = @{} |
| 117 |
} |
| 118 |
$SetParams.Replace['sAMAccountName'] = $sam |
| 119 |
} |
| 120 |
|
| 121 |
$module.Diff.after.sam_account_name = $sam |
| 122 |
} |
| 123 |
} |
| 124 |
[PSCustomObject]@{ |
| 125 |
Name = 'spn' |
| 126 |
Option = @{ |
| 127 |
aliases = 'spns' |
| 128 |
type = 'add_remove_set' |
| 129 |
} |
| 130 |
Attribute = 'servicePrincipalName' |
| 131 |
CaseInsensitive = $true |
| 132 |
IsRawAttribute = $true |
| 133 |
} |
| 134 |
[PSCustomObject]@{ |
| 135 |
Name = 'trusted_for_delegation' |
| 136 |
Option = @{ type = 'bool' } |
| 137 |
Attribute = 'TrustedForDelegation' |
| 138 |
} |
| 139 |
[PSCustomObject]@{ |
| 140 |
Name = 'upn' |
| 141 |
Option = @{ type = 'str' } |
| 142 |
Attribute = 'userPrincipalName' |
| 143 |
} |
| 144 |
) |
| 145 |
ModuleNoun = 'ADComputer' |
| 146 |
DefaultPath = { |
| 147 |
param($Module, $ADParams) |
| 148 |
|
| 149 |
$GUID_COMPUTERS_CONTAINER_W = 'AA312825768811D1ADED00C04FD8D5CD' |
| 150 |
$defaultNamingContext = (Get-ADRootDSE @ADParams -Properties defaultNamingContext).defaultNamingContext |
| 151 |
|
| 152 |
Get-ADObject @ADParams -Identity $defaultNamingContext -Properties wellKnownObjects | |
| 153 |
Select-Object -ExpandProperty wellKnownObjects | |
| 154 |
Where-Object { $_.StartsWith("B:32:$($GUID_COMPUTERS_CONTAINER_W):") } | |
| 155 |
ForEach-Object Substring 38 |
| 156 |
} |
| 157 |
PreAction = { |
| 158 |
param ($Module, $ADParams, $ADObject) |
| 159 |
|
| 160 |
if ( |
| 161 |
$Module.Params.sam_account_name -and |
| 162 |
-not $Module.Params.sam_account_name.EndsWith('$') -and |
| 163 |
-not $Module.Params.do_not_append_dollar_to_sam |
| 164 |
) { |
| 165 |
$Module.Params.sam_account_name = "$($Module.Params.sam_account_name)$" |
| 166 |
} |
| 167 |
} |
| 168 |
PostAction = { |
| 169 |
param($Module, $ADParams, $ADObject) |
| 170 |
|
| 171 |
if ($ADObject) { |
| 172 |
$Module.Result.sid = $ADObject.SID.Value |
| 173 |
|
| 174 |
# This should only happen when the computer account was created. |
| 175 |
# The code will set sam_account_name to the desired value without |
| 176 |
# the '$' suffix. |
| 177 |
if ( |
| 178 |
$Module.Params.state -eq 'present' -and |
| 179 |
$Module.Params.sam_account_name -and |
| 180 |
$Module.Params.do_not_append_dollar_to_sam -and |
| 181 |
$Module.Params.sam_account_name -ne $ADObject.SAMAccountName |
| 182 |
) { |
| 183 |
$ADObject | Set-ADComputer -Replace @{ |
| 184 |
sAMAccountName = $module.Params.sam_account_name |
| 185 |
} -WhatIf:$Module.CheckMode @ADParams |
| 186 |
} |
| 187 |
} |
| 188 |
elseif ($Module.Params.state -eq 'present') { |
| 189 |
# Use dummy value for check mode when creating a new user |
| 190 |
$Module.Result.sid = 'S-1-5-0000' |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
Invoke-AnsibleADObject @setParams |