| 1 |
# Copyright (c) 2023 Ansible Project |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
|
| 4 |
# FOR INTERNAL COLLECTION USE ONLY |
| 5 |
# The interfaces in this file are meant for use within this collection |
| 6 |
# and may not remain stable to outside uses. Changes may be made in ANY release, even a bugfix release. |
| 7 |
# See also: https://github.com/ansible/community/issues/539#issuecomment-780839686 |
| 8 |
# Please open an issue if you have questions about this. |
| 9 |
|
| 10 |
#AnsibleRequires -CSharpUtil Ansible.Basic |
| 11 |
|
| 12 |
Function Compare-AnsibleADAttribute { |
| 13 |
<# |
| 14 |
.SYNOPSIS |
| 15 |
Compares AD attribute values. |
| 16 |
|
| 17 |
.PARAMETER Name |
| 18 |
The attribute name to compare. |
| 19 |
|
| 20 |
.PARAMETER ADObject |
| 21 |
The AD object to compare with. |
| 22 |
|
| 23 |
.PARAMETER Attribute |
| 24 |
The attribute value(s) to add/remove/set. |
| 25 |
|
| 26 |
.PARAMETER Action |
| 27 |
Set to Add to add the value(s), Remove to remove the value(s), and Set to replace the value(s). |
| 28 |
#> |
| 29 |
[CmdletBinding()] |
| 30 |
param ( |
| 31 |
[Parameter(Mandatory)] |
| 32 |
[string] |
| 33 |
$Name, |
| 34 |
|
| 35 |
[Parameter()] |
| 36 |
[AllowNull()] |
| 37 |
[Microsoft.ActiveDirectory.Management.ADObject] |
| 38 |
$ADObject, |
| 39 |
|
| 40 |
[Parameter()] |
| 41 |
[AllowEmptyCollection()] |
| 42 |
[object] |
| 43 |
$Attribute, |
| 44 |
|
| 45 |
[ValidateSet("Add", "Remove", "Set")] |
| 46 |
[string] |
| 47 |
$Action |
| 48 |
) |
| 49 |
|
| 50 |
<# Gets all the known types the AD module can return |
| 51 |
|
| 52 |
DateTime, Guid, SecurityIdentifier are all from readonly properties |
| 53 |
that the AD module alaises of the real LDAP attributes. |
| 54 |
|
| 55 |
Get-ADObject -LDAPFilter '(objectClass=*)' -Properties * | |
| 56 |
ForEach-Object { |
| 57 |
foreach ($name in $_.PSObject.Properties.Name) { |
| 58 |
if ($name -in @('AddedProperties', 'ModifiedProperties', 'RemovedProperties', 'PropertyNames')) { continue } |
| 59 |
|
| 60 |
$v = $_.$name |
| 61 |
if ($null -eq $v) { continue } |
| 62 |
if ($v -isnot [System.Collections.IList] -or $v -is [System.Byte[]]) { |
| 63 |
$v = @(, $v) |
| 64 |
} |
| 65 |
|
| 66 |
foreach ($value in $v) { |
| 67 |
$value.GetType() |
| 68 |
} |
| 69 |
} |
| 70 |
} | |
| 71 |
Sort-Object -Unique |
| 72 |
#> |
| 73 |
$getDiffValue = { |
| 74 |
if ($_ -is [System.Byte[]]) { |
| 75 |
[System.Convert]::ToBase64String($_) |
| 76 |
} |
| 77 |
elseif ($_ -is [System.DateTime]) { |
| 78 |
$_.ToUniversalTime().ToFileTimeUtc() |
| 79 |
} |
| 80 |
elseif ($_ -is [System.DirectoryServices.ActiveDirectorySecurity]) { |
| 81 |
$_.GetSecurityDescriptorSddlForm([System.Security.AccessControl.AccessControlSections]::All) |
| 82 |
} |
| 83 |
else { |
| 84 |
# Bool, Int32, Int64, String |
| 85 |
$_ |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
$existingAttributes = [System.Collections.Generic.List[object]]@() |
| 90 |
if ($ADObject -and $null -ne $ADObject.$Name) { |
| 91 |
$existingValues = $ADObject.$Name |
| 92 |
if ($null -ne $existingValues) { |
| 93 |
if ( |
| 94 |
$existingValues -is [System.Collections.IList] -and |
| 95 |
$existingValues -isnot [System.Byte[]] |
| 96 |
) { |
| 97 |
# Wrap with @() to help pwsh unroll the property value collection |
| 98 |
$existingAttributes.AddRange(@($existingValues)) |
| 99 |
|
| 100 |
} |
| 101 |
else { |
| 102 |
$existingAttributes.Add($existingValues) |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
$desiredAttributes = [System.Collections.Generic.List[object]]@() |
| 108 |
if ($null -ne $Attribute -and $Attribute -isnot [System.Collections.IList]) { |
| 109 |
$Attribute = @($Attribute) |
| 110 |
} |
| 111 |
foreach ($attr in $Attribute) { |
| 112 |
if ($attr -is [System.Collections.IDictionary]) { |
| 113 |
if ($attr.Keys.Count -gt 2) { |
| 114 |
$keyList = $attr.Keys -join "', '" |
| 115 |
throw "Attribute '$Name' entry should only contain the 'type' and 'value' keys, found: '$keyList'" |
| 116 |
} |
| 117 |
|
| 118 |
$type = $attr.type |
| 119 |
$value = $attr.value |
| 120 |
} |
| 121 |
else { |
| 122 |
$type = 'raw' |
| 123 |
$value = $attr |
| 124 |
} |
| 125 |
|
| 126 |
switch ($type) { |
| 127 |
bool { |
| 128 |
$desiredAttributes.Add([System.Boolean]$value) |
| 129 |
} |
| 130 |
bytes { |
| 131 |
$desiredAttributes.Add([System.Convert]::FromBase64String($value)) |
| 132 |
} |
| 133 |
date_time { |
| 134 |
$dtVal = [DateTimeOffset]::ParseExact( |
| 135 |
$value, |
| 136 |
[string[]]@("yyyy-MM-dd'T'HH:mm:ss.FFFFFFFK"), |
| 137 |
[System.Globalization.CultureInfo]::InvariantCulture, |
| 138 |
[System.Globalization.DateTimeStyles]::AssumeUniversal) |
| 139 |
$desiredAttributes.Add($dtVal.UtcDateTime) |
| 140 |
} |
| 141 |
int { |
| 142 |
$desiredAttributes.Add([Int64]$value) |
| 143 |
} |
| 144 |
security_descriptor { |
| 145 |
$sd = New-Object -TypeName System.DirectoryServices.ActiveDirectorySecurity |
| 146 |
$sd.SetSecurityDescriptorSddlForm($value) |
| 147 |
$desiredAttributes.Add($sd) |
| 148 |
} |
| 149 |
string { |
| 150 |
$desiredAttributes.Add($value.ToString()) |
| 151 |
} |
| 152 |
raw { |
| 153 |
# If the value is an Int32 we need it to be Int64 to ensure |
| 154 |
# the values are all the same type. |
| 155 |
if ($value -is [int]) { |
| 156 |
$value = [Int64]$value |
| 157 |
} |
| 158 |
$desiredAttributes.Add($value) |
| 159 |
} |
| 160 |
default { throw "Attribute type '$type' must be bytes, date_time, int, security_descriptor, or raw" } |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
$diffBefore = @($existingAttributes | ForEach-Object -Process $getDiffValue) |
| 165 |
$diffAfter = [System.Collections.Generic.List[object]]@() |
| 166 |
$value = [System.Collections.Generic.List[object]]@() |
| 167 |
$changed = $false |
| 168 |
|
| 169 |
# It's a lot easier to compare the string values |
| 170 |
$existing = [string[]]$diffBefore |
| 171 |
$desired = [string[]]@($desiredAttributes | ForEach-Object -Process $getDiffValue) |
| 172 |
|
| 173 |
if ($Action -eq 'Add') { |
| 174 |
$diffAfter.AddRange($existingAttributes) |
| 175 |
|
| 176 |
for ($i = 0; $i -lt $desired.Length; $i++) { |
| 177 |
if ($desired[$i] -cnotin $existing) { |
| 178 |
$value.Add($desiredAttributes[$i]) |
| 179 |
$diffAfter.Add($desiredAttributes[$i]) |
| 180 |
$changed = $true |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
elseif ($Action -eq 'Remove') { |
| 185 |
$diffAfter.AddRange($existingAttributes) |
| 186 |
|
| 187 |
for ($i = $desired.Length - 1; $i -ge 0; $i--) { |
| 188 |
if ($desired[$i] -cin $existing) { |
| 189 |
$value.Add($desiredAttributes[$i]) |
| 190 |
$diffAfter.RemoveAt($i) |
| 191 |
$changed = $true |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
else { |
| 196 |
$diffAfter.AddRange($desiredAttributes) |
| 197 |
|
| 198 |
$toAdd = [string[]][System.Linq.Enumerable]::Except($desired, $existing) |
| 199 |
$toRemove = [string[]][System.Linq.Enumerable]::Except($existing, $desired) |
| 200 |
if ($toAdd.Length -or $toRemove.Length) { |
| 201 |
$changed = $true |
| 202 |
} |
| 203 |
|
| 204 |
if ($changed) { |
| 205 |
$value.AddRange($desiredAttributes) |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
[PSCustomObject]@{ |
| 210 |
Name = $Name |
| 211 |
Value = $value.ToArray() # AD cmdlets expect an array here |
| 212 |
Changed = $changed |
| 213 |
DiffBefore = @($diffBefore | Sort-Object) |
| 214 |
DiffAfter = @($diffAfter | ForEach-Object -Process $getDiffValue | Sort-Object) |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
Function Update-AnsibleADSetADObjectParam { |
| 219 |
<# |
| 220 |
.SYNOPSIS |
| 221 |
Updates the Set-AD* parameter splat with the parameters needed to set the |
| 222 |
attributes requested. |
| 223 |
It will output a boolean that indicates whether a change is needed to |
| 224 |
update the attributes. |
| 225 |
|
| 226 |
.PARAMETER Splat |
| 227 |
The parameter splat to update. |
| 228 |
|
| 229 |
.PARAMETER Add |
| 230 |
The attributes to add. |
| 231 |
|
| 232 |
.PARAMETER Remove |
| 233 |
The attributes to remove. |
| 234 |
|
| 235 |
.PARAMETER Set |
| 236 |
The attributes to set. |
| 237 |
|
| 238 |
.PARAMETER Diff |
| 239 |
An optional dictionary that can be used to store the diff output value on |
| 240 |
what was changed. |
| 241 |
|
| 242 |
.PARAMETER ADObject |
| 243 |
The AD object to compare the requested attribute values with. |
| 244 |
|
| 245 |
.PARAMETER ForNew |
| 246 |
This Splat is used for New-AD* and will update the OtherAttributes |
| 247 |
parameter. |
| 248 |
#> |
| 249 |
[OutputType([bool])] |
| 250 |
[CmdletBinding()] |
| 251 |
param ( |
| 252 |
[Parameter(Mandatory)] |
| 253 |
[System.Collections.IDictionary] |
| 254 |
$Splat, |
| 255 |
|
| 256 |
[Parameter()] |
| 257 |
[AllowNull()] |
| 258 |
[System.Collections.IDictionary] |
| 259 |
$Add, |
| 260 |
|
| 261 |
[Parameter()] |
| 262 |
[AllowNull()] |
| 263 |
[System.Collections.IDictionary] |
| 264 |
$Remove, |
| 265 |
|
| 266 |
[Parameter()] |
| 267 |
[AllowNull()] |
| 268 |
[System.Collections.IDictionary] |
| 269 |
$Set, |
| 270 |
|
| 271 |
[Parameter()] |
| 272 |
[System.Collections.IDictionary] |
| 273 |
$Diff, |
| 274 |
|
| 275 |
[Parameter()] |
| 276 |
[AllowNull()] |
| 277 |
[Microsoft.ActiveDirectory.Management.ADObject] |
| 278 |
$ADObject, |
| 279 |
|
| 280 |
[Parameter()] |
| 281 |
[switch] |
| 282 |
$ForNew |
| 283 |
) |
| 284 |
|
| 285 |
$diffBefore = @{} |
| 286 |
$diffAfter = @{} |
| 287 |
|
| 288 |
$addAttributes = @{} |
| 289 |
$removeAttributes = @{} |
| 290 |
$replaceAttributes = @{} |
| 291 |
$clearAttributes = [System.Collections.Generic.List[String]]@() |
| 292 |
|
| 293 |
if ($Add.Count) { |
| 294 |
foreach ($kvp in $Add.GetEnumerator()) { |
| 295 |
$val = Compare-AnsibleADAttribute -Name $kvp.Key -ADObject $ADObject -Attribute $kvp.Value -Action Add |
| 296 |
if ($val.Changed -and $val.Value.Count) { |
| 297 |
$addAttributes[$kvp.Key] = $val.Value |
| 298 |
} |
| 299 |
$diffBefore[$kvp.Key] = $val.DiffBefore |
| 300 |
$diffAfter[$kvp.Key] = $val.DiffAfter |
| 301 |
} |
| 302 |
} |
| 303 |
# remove doesn't make sense when creating a new object |
| 304 |
if (-not $ForNew -and $Remove.Count) { |
| 305 |
foreach ($kvp in $Remove.GetEnumerator()) { |
| 306 |
$val = Compare-AnsibleADAttribute -Name $kvp.Key -ADObject $ADObject -Attribute $kvp.Value -Action Remove |
| 307 |
if ($val.Changed -and $val.Value.Count) { |
| 308 |
$removeAttributes[$kvp.Key] = $val.Value |
| 309 |
} |
| 310 |
$diffBefore[$kvp.Key] = $val.DiffBefore |
| 311 |
$diffAfter[$kvp.Key] = $val.DiffAfter |
| 312 |
} |
| 313 |
} |
| 314 |
if ($Set.Count) { |
| 315 |
foreach ($kvp in $Set.GetEnumerator()) { |
| 316 |
$val = Compare-AnsibleADAttribute -Name $kvp.Key -ADObject $ADObject -Attribute $kvp.Value -Action Set |
| 317 |
if ($val.Changed) { |
| 318 |
if ($val.Value.Count) { |
| 319 |
$replaceAttributes[$kvp.Key] = $val.Value |
| 320 |
} |
| 321 |
else { |
| 322 |
$clearAttributes.Add($kvp.Key) |
| 323 |
} |
| 324 |
} |
| 325 |
$diffBefore[$kvp.Key] = $val.DiffBefore |
| 326 |
$diffAfter[$kvp.Key] = $val.DiffAfter |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
$changed = $false |
| 331 |
if ($ForNew) { |
| 332 |
$diffBefore = $null |
| 333 |
$otherAttributes = @{} |
| 334 |
|
| 335 |
foreach ($kvp in $addAttributes.GetEnumerator()) { |
| 336 |
$otherAttributes[$kvp.Key] = $kvp.Value |
| 337 |
} |
| 338 |
foreach ($kvp in $replaceAttributes.GetEnumerator()) { |
| 339 |
$otherAttributes[$kvp.Key] = $kvp.Value |
| 340 |
} |
| 341 |
|
| 342 |
if ($otherAttributes.Count) { |
| 343 |
$changed = $true |
| 344 |
$Splat.OtherAttributes = $otherAttributes |
| 345 |
} |
| 346 |
} |
| 347 |
else { |
| 348 |
if ($addAttributes.Count) { |
| 349 |
$changed = $true |
| 350 |
$Splat.Add = $addAttributes |
| 351 |
} |
| 352 |
if ($removeAttributes.Count) { |
| 353 |
$changed = $true |
| 354 |
$Splat.Remove = $removeAttributes |
| 355 |
} |
| 356 |
if ($replaceAttributes.Count) { |
| 357 |
$changed = $true |
| 358 |
$Splat.Replace = $replaceAttributes |
| 359 |
} |
| 360 |
if ($clearAttributes.Count) { |
| 361 |
$changed = $true |
| 362 |
$Splat.Clear = $clearAttributes |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
if ($null -ne $Diff.Count) { |
| 367 |
$Diff.after = $diffAfter |
| 368 |
$Diff.before = $diffBefore |
| 369 |
} |
| 370 |
|
| 371 |
$changed |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
Function Compare-AnsibleADIdempotentList { |
| 376 |
<# |
| 377 |
.SYNOPSIS |
| 378 |
Common code to compare AD property values with an add/remove/set collection. |
| 379 |
|
| 380 |
.PARAMETER Existing |
| 381 |
The existing values for the property. |
| 382 |
|
| 383 |
.PARAMETER Add |
| 384 |
A list of values to add |
| 385 |
|
| 386 |
.PARAMETER Remove |
| 387 |
A list of values to remove |
| 388 |
|
| 389 |
.PARAMETER Set |
| 390 |
A list of files to set, will remove existing values if they are not in the |
| 391 |
list and add ones that are not in the existing values. |
| 392 |
|
| 393 |
.PARAMETER CaseInsensitive |
| 394 |
Whether to perform a case insensitive comparison check. |
| 395 |
#> |
| 396 |
[CmdletBinding()] |
| 397 |
param ( |
| 398 |
[Parameter(Mandatory)] |
| 399 |
[AllowEmptyCollection()] |
| 400 |
[AllowNull()] |
| 401 |
[object[]] |
| 402 |
$Existing, |
| 403 |
|
| 404 |
[Parameter()] |
| 405 |
[AllowNull()] |
| 406 |
[AllowEmptyCollection()] |
| 407 |
[object[]] |
| 408 |
$Add, |
| 409 |
|
| 410 |
[Parameter()] |
| 411 |
[AllowNull()] |
| 412 |
[AllowEmptyCollection()] |
| 413 |
[object[]] |
| 414 |
$Remove, |
| 415 |
|
| 416 |
[Parameter()] |
| 417 |
[AllowNull()] |
| 418 |
[AllowEmptyCollection()] |
| 419 |
[object[]] |
| 420 |
$Set, |
| 421 |
|
| 422 |
[Parameter()] |
| 423 |
[switch] |
| 424 |
$CaseInsensitive |
| 425 |
) |
| 426 |
|
| 427 |
# It's easier to compare with strings. |
| 428 |
$existingString = [string[]]@(if ($null -ne $Existing) { $Existing | ForEach-Object ToString }) |
| 429 |
$comparer = if ($CaseInsensitive) { |
| 430 |
[System.StringComparer]::OrdinalIgnoreCase |
| 431 |
} |
| 432 |
else { |
| 433 |
[System.StringComparer]::CurrentCulture |
| 434 |
} |
| 435 |
|
| 436 |
$value = [System.Collections.Generic.List[Object]]@() |
| 437 |
$toAdd = [System.Collections.Generic.List[Object]]@() |
| 438 |
$toRemove = [System.Collections.Generic.List[Object]]@() |
| 439 |
|
| 440 |
if ($null -ne $Set) { |
| 441 |
$setString = [string[]]@($Set | ForEach-Object ToString) |
| 442 |
$value.AddRange($Set) |
| 443 |
|
| 444 |
for ($i = 0; $i -lt $setString.Length; $i++) { |
| 445 |
$setElement = $setString[$i] |
| 446 |
if (-not [System.Linq.Enumerable]::Contains($existingString, $setElement, $comparer)) { |
| 447 |
$toAdd.Add($Set[$i]) |
| 448 |
} |
| 449 |
} |
| 450 |
for ($i = 0; $i -lt $existingString.Length; $i++) { |
| 451 |
$existingElement = $existingString[$i] |
| 452 |
if (-not [System.Linq.Enumerable]::Contains($setString, $existingElement, $comparer)) { |
| 453 |
$toRemove.Add($Existing[$i]) |
| 454 |
} |
| 455 |
} |
| 456 |
} |
| 457 |
else { |
| 458 |
if ($Remove) { |
| 459 |
$removeString = [string[]]@($Remove | ForEach-Object ToString) |
| 460 |
|
| 461 |
for ($i = 0; $i -lt $existingString.Length; $i++) { |
| 462 |
$existingElement = $existingString[$i] |
| 463 |
if ([System.Linq.Enumerable]::Contains($removeString, $existingElement, $comparer)) { |
| 464 |
$toRemove.Add($Existing[$i]) |
| 465 |
} |
| 466 |
else { |
| 467 |
$value.Add($Existing[$i]) |
| 468 |
} |
| 469 |
} |
| 470 |
} |
| 471 |
else { |
| 472 |
$value.AddRange($Existing) |
| 473 |
} |
| 474 |
|
| 475 |
if ($Add) { |
| 476 |
$addString = [string[]]@($Add | ForEach-Object ToString) |
| 477 |
|
| 478 |
for ($i = 0; $i -lt $addString.Length; $i++) { |
| 479 |
$addElement = $addString[$i] |
| 480 |
if (-not [System.Linq.Enumerable]::Contains($existingString, $addElement, $comparer)) { |
| 481 |
$toAdd.Add($Add[$i]) |
| 482 |
$value.Add($Add[$i]) |
| 483 |
} |
| 484 |
} |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
[PSCustomObject]@{ |
| 489 |
# $null is explicit here as the AD modules use it to unset a value |
| 490 |
Value = if ($value.Count) { $value.ToArray() } else { $null } |
| 491 |
# Also returned if the API doesn't support explicitly setting 1 value |
| 492 |
ToAdd = $toAdd.ToArray() |
| 493 |
ToRemove = $toRemove.ToArray() |
| 494 |
Changed = [bool]($toAdd.Count -or $toRemove.Count) |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
Function ConvertTo-AnsibleADDistinguishedName { |
| 499 |
<# |
| 500 |
.SYNOPSIS |
| 501 |
Converts the input list into DistinguishedNames for later comparison. |
| 502 |
|
| 503 |
.PARAMETER InputObject |
| 504 |
The identity parameter, this can either be a string or a hashtable. |
| 505 |
If a hashtable it should contain the name and optional server key to |
| 506 |
identity the object to search and set a specific server to search on. |
| 507 |
|
| 508 |
.PARAMETER Module |
| 509 |
The AnsibleModule object associated with the current module execution. |
| 510 |
|
| 511 |
.PARAMETER Context |
| 512 |
The context behind this conversion to add to the error message if there |
| 513 |
is one. |
| 514 |
|
| 515 |
.PARAMETER Server |
| 516 |
The default server to search. The Identity server key will override this |
| 517 |
value is present. |
| 518 |
|
| 519 |
.PARAMETER Credential |
| 520 |
The credential to search with. This is ignored if the Identity server key |
| 521 |
is present. |
| 522 |
|
| 523 |
.PARAMETER FailureAction |
| 524 |
The action to take if the lookup fails. Fail will cause the module to |
| 525 |
exit with an error, ignore will ignore the error, and warn will emit a |
| 526 |
warning on failure. |
| 527 |
#> |
| 528 |
[OutputType([string])] |
| 529 |
[CmdletBinding()] |
| 530 |
param ( |
| 531 |
[Parameter(Mandatory, ValueFromPipeline)] |
| 532 |
[object[]] |
| 533 |
$InputObject, |
| 534 |
|
| 535 |
[Parameter(Mandatory)] |
| 536 |
[object] |
| 537 |
$Module, |
| 538 |
|
| 539 |
[Parameter(Mandatory)] |
| 540 |
[string] |
| 541 |
$Context, |
| 542 |
|
| 543 |
[string] |
| 544 |
$Server, |
| 545 |
|
| 546 |
[PSCredential] |
| 547 |
$Credential, |
| 548 |
|
| 549 |
[ValidateSet('Fail', 'Ignore', 'Warn')] |
| 550 |
[string] |
| 551 |
$FailureAction = 'Fail' |
| 552 |
) |
| 553 |
|
| 554 |
begin { |
| 555 |
$allowedKeys = [string[]]@('name', 'server') |
| 556 |
$results = [System.Collections.Generic.List[string]]@() |
| 557 |
$getErrors = [System.Collections.Generic.List[string]]@() |
| 558 |
$invalidIdentities = [System.Collections.Generic.List[string]]@() |
| 559 |
} |
| 560 |
|
| 561 |
process { |
| 562 |
foreach ($obj in $InputObject) { |
| 563 |
$getParams = @{} |
| 564 |
if ($Server) { |
| 565 |
$getParams.Server = $Server |
| 566 |
} |
| 567 |
if ($Credential) { |
| 568 |
$getParams.Credential = $Credential |
| 569 |
} |
| 570 |
|
| 571 |
if ($obj -is [System.Collections.IDictionary]) { |
| 572 |
# When using a hashtable, the name and server key can be used |
| 573 |
# to specify the identity and server to use. If no server is |
| 574 |
# set then it defaults to the default server (if provided) and |
| 575 |
# it's credentials. |
| 576 |
$existingKeys = [string[]]$obj.Keys |
| 577 |
|
| 578 |
if ('name' -notin $existingKeys) { |
| 579 |
$getErrors.Add("Identity entry does not contain the required name key") |
| 580 |
continue |
| 581 |
} |
| 582 |
$name = [string]$obj.name |
| 583 |
|
| 584 |
[string[]]$extraKeys = [System.Linq.Enumerable]::Except($existingKeys, $allowedKeys) |
| 585 |
if ($extraKeys) { |
| 586 |
$extraKeys = $extraKeys | Sort-Object |
| 587 |
$getErrors.Add("Identity entry for '$name' contains extra keys: '$($extraKeys -join "', '")'") |
| 588 |
continue |
| 589 |
} |
| 590 |
$getParams.Identity = $name |
| 591 |
|
| 592 |
if ($obj.server) { |
| 593 |
# If a custom server is specified we use that and the |
| 594 |
# credential (if any) associated with that server. |
| 595 |
$getParams.Server = $obj.server |
| 596 |
|
| 597 |
if ($Module.ServerCredentials.ContainsKey($obj.server)) { |
| 598 |
$getParams.Credential = $Module.ServerCredentials[$obj.server] |
| 599 |
} |
| 600 |
elseif (-not $Module.DefaultCredentialSet) { |
| 601 |
$null = $getParams.Remove('Credential') |
| 602 |
} |
| 603 |
} |
| 604 |
} |
| 605 |
else { |
| 606 |
# Treat the value as just the identity as a string. |
| 607 |
$getParams.Identity = [string]$obj |
| 608 |
} |
| 609 |
|
| 610 |
if (-not $getParams.Identity) { |
| 611 |
continue |
| 612 |
} |
| 613 |
|
| 614 |
$adDN = Get-AnsibleADObject @getParams | |
| 615 |
Select-Object -ExpandProperty DistinguishedName |
| 616 |
if ($adDN) { |
| 617 |
$results.Add($adDN) |
| 618 |
} |
| 619 |
else { |
| 620 |
$invalidIdentities.Add($getParams.Identity) |
| 621 |
} |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
end { |
| 626 |
# This is a weird workaround as FailJson calls exit which means the |
| 627 |
# caller won't capture the output causing junk data in the output. By |
| 628 |
# only outputting the results if no errors occurred we can avoid that |
| 629 |
# problem. |
| 630 |
$errorPrefix = "Failed to find the AD object DNs for $Context" |
| 631 |
if ($getErrors) { |
| 632 |
$msg = "$errorPrefix. $($getErrors -join '. ')." |
| 633 |
$Module.FailJson($msg) |
| 634 |
} |
| 635 |
|
| 636 |
if ($invalidIdentities) { |
| 637 |
if ($FailureAction -ne 'Ignore') { |
| 638 |
$identityString = "'$($invalidIdentities -join "', '")'" |
| 639 |
if ($FailureAction -eq 'Fail') { |
| 640 |
$Module.FailJson("$errorPrefix. Invalid identities: $identityString") |
| 641 |
} |
| 642 |
else { |
| 643 |
$module.Warn("$errorPrefix. Ignoring invalid identities: $identityString") |
| 644 |
} |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
$results |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
Function Get-AnsibleADObject { |
| 653 |
<# |
| 654 |
.SYNOPSIS |
| 655 |
The -Identity params is limited to just objectGuid and distinguishedName |
| 656 |
on Get-ADObject. Try to preparse the value to support more common props |
| 657 |
like sAMAccountName, objectSid, userPrincipalName. |
| 658 |
|
| 659 |
.PARAMETER Identity |
| 660 |
The Identity to get. |
| 661 |
|
| 662 |
.PARAMETER Properties |
| 663 |
Extra properties to request on the object |
| 664 |
|
| 665 |
.PARAMETER Server |
| 666 |
The explicit domain controller to query. |
| 667 |
|
| 668 |
.PARAMETER Credential |
| 669 |
Custom queries to authenticate with. |
| 670 |
|
| 671 |
.PARAMETER GetCommand |
| 672 |
The Get-AD* cmdlet to use to get the AD object. Defaults to Get-ADObject. |
| 673 |
#> |
| 674 |
[OutputType([Microsoft.ActiveDirectory.Management.ADObject])] |
| 675 |
[CmdletBinding()] |
| 676 |
param ( |
| 677 |
[Parameter(Mandatory)] |
| 678 |
[string] |
| 679 |
$Identity, |
| 680 |
|
| 681 |
[Parameter()] |
| 682 |
[AllowEmptyCollection()] |
| 683 |
[string[]] |
| 684 |
$Properties, |
| 685 |
|
| 686 |
[string] |
| 687 |
$Server, |
| 688 |
|
| 689 |
[PSCredential] |
| 690 |
$Credential, |
| 691 |
|
| 692 |
[Parameter()] |
| 693 |
[System.Management.Automation.CommandInfo] |
| 694 |
$GetCommand = $null |
| 695 |
) |
| 696 |
|
| 697 |
$getParams = @{} |
| 698 |
if ($Properties.Count) { |
| 699 |
$getParams.Properties = $Properties |
| 700 |
} |
| 701 |
if ($Server) { |
| 702 |
$getParams.Server = $Server |
| 703 |
} |
| 704 |
if ($Credential) { |
| 705 |
$getParams.Credential = $Credential |
| 706 |
} |
| 707 |
|
| 708 |
# The -Identity parameter is used where possible as LDAPFilter is limited |
| 709 |
# to just the defaultNamingContext as defined by -SearchBase. |
| 710 |
$objectGuid = [Guid]::Empty |
| 711 |
$tryDollarFallback = $false |
| 712 |
if ([System.Guid]::TryParse($Identity, [ref]$objectGuid)) { |
| 713 |
$getParams.Identity = $objectGuid |
| 714 |
} |
| 715 |
elseif ($Identity -match '^.*\@.*\..*$') { |
| 716 |
$getParams.LDAPFilter = "(userPrincipalName=$($Matches[0]))" |
| 717 |
} |
| 718 |
else { |
| 719 |
try { |
| 720 |
$sid = New-Object -TypeName System.Security.Principal.SecurityIdentifier -ArgumentList $Identity |
| 721 |
$sidBytes = New-Object -TypeName System.Byte[] -ArgumentList $sid.BinaryLength |
| 722 |
$sid.GetBinaryForm($sidBytes, 0) |
| 723 |
|
| 724 |
$value = @($sidBytes | ForEach-Object { |
| 725 |
'\' + [System.BitConverter]::ToString($_).ToLowerInvariant() |
| 726 |
}) -join '' |
| 727 |
$getParams.LDAPFilter = "(objectSid=$value)" |
| 728 |
} |
| 729 |
catch [System.ArgumentException] { |
| 730 |
if ($Identity -match '^(?:[^:*?""<>|\/\\]+\\)?(?<username>[^;:""<>|?,=\*\+\\\(\)]+)$') { |
| 731 |
$tryDollarFallback = -not $Matches.username.EndsWith('$') |
| 732 |
$getParams.LDAPFilter = "(sAMAccountName=$($Matches.username))" |
| 733 |
} |
| 734 |
else { |
| 735 |
# Finally fallback to DistinguishedName. |
| 736 |
$getParams.Identity = $Identity |
| 737 |
} |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
if ($GetCommand) { |
| 742 |
$null = $getParams.Remove('GetCommand') |
| 743 |
} |
| 744 |
else { |
| 745 |
$GetCommand = Get-Command -Name Get-ADObject -Module ActiveDirectory |
| 746 |
} |
| 747 |
try { |
| 748 |
$obj = & $GetCommand @getParams | Select-Object -First 1 |
| 749 |
} |
| 750 |
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] { |
| 751 |
# AD cmdlets only raise this error when -Identity was used. |
| 752 |
# This means the account wasn't found and we don't need the fallback |
| 753 |
# logic below. |
| 754 |
return |
| 755 |
} |
| 756 |
|
| 757 |
# If we are dealing with a known object type where the sAMAccountName |
| 758 |
# typically ends with $ we want to try again with the $ append to the |
| 759 |
# filter. |
| 760 |
# https://github.com/ansible-collections/microsoft.ad/issues/124 |
| 761 |
$dollarCommands = @( |
| 762 |
'Get-ADComputer' |
| 763 |
'Get-ADServiceAccount' |
| 764 |
) |
| 765 |
if (-not $obj -and $tryDollarFallback -and $GetCommand.Name -in $dollarCommands) { |
| 766 |
$getParams.LDAPFilter = $getParams.LDAPFilter.Substring(0, $getParams.LDAPFilter.Length - 1) + '$)' |
| 767 |
$obj = & $GetCommand @getParams | Select-Object -First 1 |
| 768 |
} |
| 769 |
|
| 770 |
$obj |
| 771 |
} |
| 772 |
|
| 773 |
Function Invoke-AnsibleADObject { |
| 774 |
<# |
| 775 |
.SYNOPSIS |
| 776 |
Runs the module code for managing an AD object. |
| 777 |
|
| 778 |
.PARAMETER PropertyInfo |
| 779 |
The properties to compare on the AD object and what the module supports. |
| 780 |
Each object in this array must have the following keys set |
| 781 |
Name - The module option name |
| 782 |
Option - Module options to define in the arg spec |
| 783 |
|
| 784 |
The following keys are optional: |
| 785 |
Attribute - The ldap attribute name to compare against |
| 786 |
CaseInsensitive - The values are case insensitive (defaults to $false) |
| 787 |
StateRequired - Set to 'present' or 'absent' if this needs to be defined for either state |
| 788 |
DNLookup - Whether each value needs to be looked up to get the DN |
| 789 |
IsRawAttribute - Whether the attribute is a raw LDAP attribute name and not a parameter name |
| 790 |
SupportsReplace - Whether the IsRawAttribute supports setting through -Replace or needs -Add/-Remove |
| 791 |
New - Called when the option is to be set on the New-AD* cmdlet splat |
| 792 |
Set - Called when the option is to be set on the Set-AD* cmdlet splat |
| 793 |
|
| 794 |
The 'type' key in 'Option' should be a valid Ansible.Basic type or |
| 795 |
'add_remove_set'. When 'add_remove_set' is used the option type becomes |
| 796 |
dict with the options subentry for add/remove/set being the Option value |
| 797 |
specified. This can be combined with DNLookup to set the value as raw that |
| 798 |
can lookup the DN value from the string or dict specified. |
| 799 |
|
| 800 |
If Attribute is set then requested value will be compared with the |
| 801 |
attribute specified. The current attribute value is added to the before |
| 802 |
diff state for the option it is on. If New is not specified then the |
| 803 |
value requested is added to the New-AD* splat based on the attribute name. |
| 804 |
If Set is not specified then the value requested is added to the Set-AD* |
| 805 |
splat based on the attribute name. |
| 806 |
|
| 807 |
If New is specified it is called with the current module, common AD |
| 808 |
parameters and a splat that is called with New-AD*. It is up to the |
| 809 |
scriptblock to set the required splat parameters or called whatever |
| 810 |
function is needed. |
| 811 |
|
| 812 |
If Set is specified it is called with the current module, common AD |
| 813 |
parameters, a splat that is called with Set-AD*, and the current AD object. |
| 814 |
It is up to the scriptblock to set the required splat parameters or call |
| 815 |
whatever function is needed. |
| 816 |
|
| 817 |
The DNLookup key is used to indicate that the add/remove/set values can |
| 818 |
either be a string or a dictionary containing the name/server to specify |
| 819 |
the name and server to lookup the object DN value. |
| 820 |
|
| 821 |
Both New and Set must set the $Module.Diff.after results accordingly and/or |
| 822 |
mark $Module.Result.changed if it is making a change outside of adjusting |
| 823 |
the splat hashtable passed in. |
| 824 |
|
| 825 |
.PARAMETER DefaultPath |
| 826 |
A scriptblock that retrieves the default path the object is created in. |
| 827 |
Defaults to the defaultNamingContext. This is invoked with a hashtable |
| 828 |
containing parameters used to connect to AD, such as the Server and/or |
| 829 |
Credential. |
| 830 |
|
| 831 |
.PARAMETER ModuleNoun |
| 832 |
The module cmdlet noun that is being managed. This is used to run the |
| 833 |
correct Get-AD*, Set-AD*, and New-AD* cmdlets when needed. |
| 834 |
|
| 835 |
.PARAMETER ExtraProperties |
| 836 |
Extra properties to request when getting the AD object. |
| 837 |
|
| 838 |
.PARAMETER PreAction |
| 839 |
A scriptblock that is called at the beginning to perform any tasks needed |
| 840 |
before the module util is run. This is called with the module object, |
| 841 |
common ad parameters, and the ad object if it was found based on the input |
| 842 |
options. |
| 843 |
|
| 844 |
.PARAMETER PostAction |
| 845 |
A scriptblock that is called at the end to perform any tasks once the |
| 846 |
object has been configured. This is called with the module object, common |
| 847 |
ad parameters, and the ad object (state=present) else $null (state=absent) |
| 848 |
#> |
| 849 |
[CmdletBinding()] |
| 850 |
param ( |
| 851 |
[Parameter(Mandatory)] |
| 852 |
[object[]] |
| 853 |
$PropertyInfo, |
| 854 |
|
| 855 |
[Parameter()] |
| 856 |
[ScriptBlock] |
| 857 |
$DefaultPath = { param ($Module, $Params) (Get-ADRootDSE @Params -Properties defaultNamingContext).defaultNamingContext }, |
| 858 |
|
| 859 |
[Parameter()] |
| 860 |
[string] |
| 861 |
$ModuleNoun = 'ADObject', |
| 862 |
|
| 863 |
[Parameter()] |
| 864 |
[string[]] |
| 865 |
$ExtraProperties, |
| 866 |
|
| 867 |
[Parameter()] |
| 868 |
[ScriptBlock] |
| 869 |
$PreAction, |
| 870 |
|
| 871 |
[Parameter()] |
| 872 |
[ScriptBlock] |
| 873 |
$PostAction |
| 874 |
) |
| 875 |
|
| 876 |
$defaultPathSentinel = 'microsoft.ad.default_path' |
| 877 |
|
| 878 |
$spec = @{ |
| 879 |
options = @{ |
| 880 |
attributes = @{ |
| 881 |
default = @{} |
| 882 |
type = 'dict' |
| 883 |
options = @{ |
| 884 |
add = @{ |
| 885 |
default = @{} |
| 886 |
type = 'dict' |
| 887 |
} |
| 888 |
remove = @{ |
| 889 |
default = @{} |
| 890 |
type = 'dict' |
| 891 |
} |
| 892 |
set = @{ |
| 893 |
default = @{} |
| 894 |
type = 'dict' |
| 895 |
} |
| 896 |
} |
| 897 |
} |
| 898 |
domain_credentials = @{ |
| 899 |
default = @() |
| 900 |
type = 'list' |
| 901 |
elements = 'dict' |
| 902 |
options = @{ |
| 903 |
name = @{ |
| 904 |
type = 'str' |
| 905 |
} |
| 906 |
username = @{ |
| 907 |
required = $true |
| 908 |
type = 'str' |
| 909 |
} |
| 910 |
password = @{ |
| 911 |
no_log = $true |
| 912 |
required = $true |
| 913 |
type = 'str' |
| 914 |
} |
| 915 |
} |
| 916 |
} |
| 917 |
domain_password = @{ |
| 918 |
no_log = $true |
| 919 |
type = 'str' |
| 920 |
} |
| 921 |
domain_server = @{ |
| 922 |
type = 'str' |
| 923 |
} |
| 924 |
domain_username = @{ |
| 925 |
type = 'str' |
| 926 |
} |
| 927 |
identity = @{ |
| 928 |
type = 'str' |
| 929 |
} |
| 930 |
name = @{ |
| 931 |
type = 'str' |
| 932 |
} |
| 933 |
path = @{ |
| 934 |
type = 'str' |
| 935 |
} |
| 936 |
state = @{ |
| 937 |
choices = 'absent', 'present' |
| 938 |
default = 'present' |
| 939 |
type = 'str' |
| 940 |
} |
| 941 |
} |
| 942 |
required_one_of = @( |
| 943 |
, @("identity", "name") |
| 944 |
) |
| 945 |
required_together = @(, @('domain_username', 'domain_password')) |
| 946 |
supports_check_mode = $true |
| 947 |
} |
| 948 |
|
| 949 |
$stateRequiredIf = @{ |
| 950 |
present = @() |
| 951 |
absent = @() |
| 952 |
} |
| 953 |
|
| 954 |
$PropertyInfo = @( |
| 955 |
$PropertyInfo |
| 956 |
|
| 957 |
# These 3 options are common to all AD objects. |
| 958 |
[PSCustomObject]@{ |
| 959 |
Name = 'description' |
| 960 |
Option = @{ type = 'str' } |
| 961 |
Attribute = 'description' |
| 962 |
} |
| 963 |
[PSCustomObject]@{ |
| 964 |
Name = 'display_name' |
| 965 |
Option = @{ type = 'str' } |
| 966 |
Attribute = 'displayName' |
| 967 |
} |
| 968 |
[PSCustomObject]@{ |
| 969 |
Name = 'protect_from_deletion' |
| 970 |
Option = @{ type = 'bool' } |
| 971 |
Attribute = 'ProtectedFromAccidentalDeletion' |
| 972 |
} |
| 973 |
) |
| 974 |
|
| 975 |
[string[]]$requestedAttributes = @( |
| 976 |
foreach ($propInfo in $PropertyInfo) { |
| 977 |
$ansibleOption = $propInfo.Name |
| 978 |
|
| 979 |
if ($propInfo.StateRequired) { |
| 980 |
$stateRequiredIf[$propInfo.StateRequired] += $ansibleOption |
| 981 |
} |
| 982 |
|
| 983 |
$option = $propInfo.Option |
| 984 |
if ($option.type -eq 'add_remove_set') { |
| 985 |
$option.type = 'dict' |
| 986 |
|
| 987 |
$optionElement = $option.Clone() |
| 988 |
$optionElement.type = 'list' |
| 989 |
|
| 990 |
$option = @{ |
| 991 |
type = 'dict' |
| 992 |
options = @{} |
| 993 |
} |
| 994 |
if ($optionElement.ContainsKey('no_log')) { |
| 995 |
$option.no_log = $optionElement.no_log |
| 996 |
} |
| 997 |
|
| 998 |
if ($propInfo.DNLookup) { |
| 999 |
$optionElement.elements = 'raw' |
| 1000 |
$option.options.lookup_failure_action = @{ |
| 1001 |
choices = @('fail', 'ignore', 'warn') |
| 1002 |
default = 'fail' |
| 1003 |
type = 'str' |
| 1004 |
} |
| 1005 |
} |
| 1006 |
elseif (-not $optionElement.ContainsKey('elements')) { |
| 1007 |
$optionElement.elements = 'str' |
| 1008 |
} |
| 1009 |
|
| 1010 |
if ($optionElement.ContainsKey('aliases')) { |
| 1011 |
$option.aliases = $optionElement.aliases |
| 1012 |
$null = $optionElement.Remove('aliases') |
| 1013 |
} |
| 1014 |
|
| 1015 |
$option.options.add = $optionElement |
| 1016 |
$option.options.remove = $optionElement |
| 1017 |
$option.options.set = $optionElement |
| 1018 |
} |
| 1019 |
elseif ($propInfo.DNLookup) { |
| 1020 |
$option.type = 'raw' |
| 1021 |
} |
| 1022 |
|
| 1023 |
if (-not $propInfo.PSObject.Properties.Match('SupportsReplace')) { |
| 1024 |
$propInfo.PSObject.Properties.Add( |
| 1025 |
[System.Management.Automation.PSNoteProperty]::new('SupportsReplace', $true)) |
| 1026 |
} |
| 1027 |
|
| 1028 |
$spec.options[$ansibleOption] = $option |
| 1029 |
|
| 1030 |
if ($propInfo.Attribute) { |
| 1031 |
$propInfo.Attribute |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
$ExtraProperties |
| 1036 |
) |
| 1037 |
|
| 1038 |
$spec.required_if = @( |
| 1039 |
foreach ($kvp in $stateRequiredIf.GetEnumerator()) { |
| 1040 |
if ($kvp.Value) { |
| 1041 |
, @("state", $kvp.Key, $kvp.Value) |
| 1042 |
} |
| 1043 |
} |
| 1044 |
) |
| 1045 |
|
| 1046 |
$module = [Ansible.Basic.AnsibleModule]::Create(@(), $spec) |
| 1047 |
$module.Result.distinguished_name = $null |
| 1048 |
$module.Result.object_guid = $null |
| 1049 |
|
| 1050 |
$adParams = @{} |
| 1051 |
$serverCredentials = @{} |
| 1052 |
foreach ($domainCred in $module.Params.domain_credentials) { |
| 1053 |
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @( |
| 1054 |
$domainCred.username, |
| 1055 |
(ConvertTo-SecureString -AsPlainText -Force -String $domainCred.password) |
| 1056 |
) |
| 1057 |
|
| 1058 |
if ($domainCred.name) { |
| 1059 |
$serverCredentials[$domainCred.name] = $cred |
| 1060 |
} |
| 1061 |
elseif ($adParams.Credential) { |
| 1062 |
$module.FailJson("Cannot specify default domain_credentials with domain_username and domain_password") |
| 1063 |
} |
| 1064 |
else { |
| 1065 |
$adParams.Credential = $cred |
| 1066 |
} |
| 1067 |
} |
| 1068 |
$module | Add-Member -MemberType NoteProperty -Name ServerCredentials -Value $serverCredentials |
| 1069 |
|
| 1070 |
if ($module.Params.domain_server) { |
| 1071 |
$adParams.Server = $module.Params.domain_server |
| 1072 |
} |
| 1073 |
|
| 1074 |
if ($module.Params.domain_username) { |
| 1075 |
if ($adParams.Credential) { |
| 1076 |
$msg = "Cannot specify domain_username/domain_password and domain_credentials with an entry that has no name." |
| 1077 |
$module.FailJson($msg) |
| 1078 |
} |
| 1079 |
$adParams.Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @( |
| 1080 |
$module.Params.domain_username, |
| 1081 |
(ConvertTo-SecureString -AsPlainText -Force -String $module.Params.domain_password) |
| 1082 |
) |
| 1083 |
$module | Add-Member -MemberType NoteProperty -Name DefaultCredentialSet -Value $true |
| 1084 |
} |
| 1085 |
|
| 1086 |
$defaultObjectPath = & $DefaultPath $module $adParams |
| 1087 |
$getCommand = Get-Command -Name "Get-$ModuleNoun" -Module ActiveDirectory |
| 1088 |
$newCommand = Get-Command -Name "New-$ModuleNoun" -Module ActiveDirectory |
| 1089 |
$setCommand = Get-Command -Name "Set-$ModuleNoun" -Module ActiveDirectory |
| 1090 |
|
| 1091 |
$requestedAttributes = [System.Collections.Generic.HashSet[string]]@( |
| 1092 |
$requestedAttributes |
| 1093 |
'name' |
| 1094 |
$module.Params.attributes.add.Keys |
| 1095 |
$module.Params.attributes.remove.Keys |
| 1096 |
$module.Params.attributes.set.Keys |
| 1097 |
) | Where-Object { $_ } |
| 1098 |
|
| 1099 |
$namePrefix = 'CN' |
| 1100 |
if ($ModuleNoun -eq 'ADOrganizationalUnit' -or $Module.Params.type -eq 'organizationalUnit') { |
| 1101 |
$namePrefix = 'OU' |
| 1102 |
} |
| 1103 |
|
| 1104 |
$identity = if ($module.Params.identity) { |
| 1105 |
$module.Params.identity |
| 1106 |
} |
| 1107 |
else { |
| 1108 |
$ouPath = $defaultObjectPath |
| 1109 |
if ($module.Params.path -and $module.Params.path -ne $defaultPathSentinel) { |
| 1110 |
$ouPath = $module.Params.path |
| 1111 |
} |
| 1112 |
"$namePrefix=$($Module.Params.name -replace ',', '\,'),$ouPath" |
| 1113 |
} |
| 1114 |
|
| 1115 |
$getParams = @{ |
| 1116 |
GetCommand = $getCommand |
| 1117 |
Identity = $identity |
| 1118 |
Properties = $requestedAttributes |
| 1119 |
} |
| 1120 |
$adObject = Get-AnsibleADObject @getParams @adParams |
| 1121 |
if ($adObject) { |
| 1122 |
$module.Result.object_guid = $adObject.ObjectGUID |
| 1123 |
$module.Result.distinguished_name = $adObject.DistinguishedName |
| 1124 |
|
| 1125 |
$module.Diff.before = @{ |
| 1126 |
attributes = $null |
| 1127 |
name = $adObject.Name |
| 1128 |
path = @($adObject.DistinguishedName -split '[^\\],', 2)[-1] |
| 1129 |
} |
| 1130 |
|
| 1131 |
foreach ($propInfo in $PropertyInfo) { |
| 1132 |
$propValue = $module.Params[$propInfo.Name] |
| 1133 |
if ($null -eq $propValue -or -not $propInfo.Attribute) { |
| 1134 |
continue |
| 1135 |
} |
| 1136 |
|
| 1137 |
$actualValue = $adObject[$propInfo.Attribute].Value |
| 1138 |
if ($module.Option.no_log) { |
| 1139 |
$actualValue = 'VALUE_SPECIFIED_IN_NO_LOG_PARAMETER' |
| 1140 |
} |
| 1141 |
if ($actualValue -is [System.Collections.IList]) { |
| 1142 |
$actualValue = @($actualValue | Sort-Object) |
| 1143 |
} |
| 1144 |
$module.Diff.before[$propInfo.Name] = $actualValue |
| 1145 |
} |
| 1146 |
} |
| 1147 |
else { |
| 1148 |
$module.Diff.before = $null |
| 1149 |
} |
| 1150 |
|
| 1151 |
if ($PreAction) { |
| 1152 |
$null = & $PreAction $module $adParams $adObject |
| 1153 |
} |
| 1154 |
|
| 1155 |
if ($module.Params.state -eq 'absent') { |
| 1156 |
if ($adObject) { |
| 1157 |
$removeParams = @{ |
| 1158 |
Confirm = $false |
| 1159 |
WhatIf = $module.CheckMode |
| 1160 |
} |
| 1161 |
|
| 1162 |
# Remove-ADObject -Recursive fails with access is denied, use this |
| 1163 |
# instead to remove the child objects manually |
| 1164 |
Get-ADObject -Filter * -Properties ProtectedFromAccidentalDeletion -Searchbase $adObject.DistinguishedName @adParams | |
| 1165 |
Sort-Object -Property { $_.DistinguishedName.Length } -Descending | |
| 1166 |
ForEach-Object -Process { |
| 1167 |
if ($_.ProtectedFromAccidentalDeletion) { |
| 1168 |
$_ | Set-ADObject -ProtectedFromAccidentalDeletion $false @removeParams @adParams |
| 1169 |
} |
| 1170 |
$_ | Remove-ADObject @removeParams @adParams |
| 1171 |
} |
| 1172 |
|
| 1173 |
$module.Result.changed = $true |
| 1174 |
} |
| 1175 |
|
| 1176 |
$module.Diff.after = $null |
| 1177 |
} |
| 1178 |
else { |
| 1179 |
$attributes = $module.Params.attributes |
| 1180 |
$objectDN = $null |
| 1181 |
$objectGuid = $null |
| 1182 |
|
| 1183 |
if (-not $adObject) { |
| 1184 |
$adName = if ($module.Params.name) { |
| 1185 |
$module.Params.name |
| 1186 |
} |
| 1187 |
else { |
| 1188 |
$module.Params.identity |
| 1189 |
} |
| 1190 |
$newParams = @{ |
| 1191 |
Confirm = $false |
| 1192 |
Name = $adName |
| 1193 |
WhatIf = $module.CheckMode |
| 1194 |
PassThru = $true |
| 1195 |
} |
| 1196 |
|
| 1197 |
$objectPath = $null |
| 1198 |
if ($module.Params.path -and $module.Params.path -ne $defaultPathSentinel) { |
| 1199 |
$newParams.Path = $objectPath = $module.Params.path |
| 1200 |
} |
| 1201 |
else { |
| 1202 |
$objectPath = $defaultObjectPath |
| 1203 |
} |
| 1204 |
|
| 1205 |
$diffAttributes = @{} |
| 1206 |
$null = Update-AnsibleADSetADObjectParam @attributes -Splat $newParams -Diff $diffAttributes -ForNew |
| 1207 |
|
| 1208 |
$module.Diff.after = @{ |
| 1209 |
attributes = $diffAttributes.after |
| 1210 |
name = $adName |
| 1211 |
path = $objectPath |
| 1212 |
} |
| 1213 |
|
| 1214 |
foreach ($propInfo in $PropertyInfo) { |
| 1215 |
$propValue = $module.Params[$propInfo.Name] |
| 1216 |
if ($propValue -is [System.Collections.IDictionary]) { |
| 1217 |
if ($propValue.Count -eq 0) { |
| 1218 |
continue |
| 1219 |
} |
| 1220 |
} |
| 1221 |
elseif ([string]::IsNullOrWhiteSpace($propValue)) { |
| 1222 |
continue |
| 1223 |
} |
| 1224 |
|
| 1225 |
if ($propInfo.New) { |
| 1226 |
$null = & $propInfo.New $module $adParams $newParams |
| 1227 |
} |
| 1228 |
elseif ($propInfo.Attribute) { |
| 1229 |
# If a dictionary (add/set/remove) and is not a DNLookup single value |
| 1230 |
if ($propValue -is [System.Collections.IDictionary] -and $propInfo.Option.type -ne 'raw') { |
| 1231 |
$propValue = if ($propInfo.DNLookup) { |
| 1232 |
foreach ($actionKvp in $propValue.GetEnumerator()) { |
| 1233 |
if ($null -eq $actionKvp.Value -or $actionKvp.Key -in @('lookup_failure_action', 'remove')) { |
| 1234 |
continue |
| 1235 |
} |
| 1236 |
|
| 1237 |
$convertParams = @{ |
| 1238 |
Module = $module |
| 1239 |
Context = "$($propInfo.Name).$($actionKvp.Key)" |
| 1240 |
FailureAction = $propValue.lookup_failure_action |
| 1241 |
} |
| 1242 |
$actionKvp.Value | ConvertTo-AnsibleADDistinguishedName @adParams @convertParams |
| 1243 |
} |
| 1244 |
} |
| 1245 |
else { |
| 1246 |
$propValue['add'] |
| 1247 |
$propValue['set'] |
| 1248 |
} |
| 1249 |
|
| 1250 |
$propValue = $propValue | Select-Object -Unique |
| 1251 |
} |
| 1252 |
elseif ($propInfo.DNLookup) { |
| 1253 |
$propValue = $propValue | ConvertTo-AnsibleADDistinguishedName @adParams -Module $module -Context $propInfo.Name |
| 1254 |
} |
| 1255 |
|
| 1256 |
# If the value is empty we don't want to explicitly set it |
| 1257 |
# as that will be the default and can fail if empty. |
| 1258 |
if ($propInfo.IsRawAttribute -and @($propValue).Count) { |
| 1259 |
if (-not $newParams.ContainsKey('OtherAttributes')) { |
| 1260 |
$newParams.OtherAttributes = @{} |
| 1261 |
} |
| 1262 |
|
| 1263 |
# The AD cmdlets don't like explicitly casted arrays, use |
| 1264 |
# ForEach-Object to get back a vanilla object[] to set. |
| 1265 |
$newParams.OtherAttributes[$propInfo.Attribute] = $propValue | ForEach-Object { "$_" } |
| 1266 |
} |
| 1267 |
elseif (-not $propInfo.IsRawAttribute) { |
| 1268 |
$newParams[$propInfo.Attribute] = $propValue |
| 1269 |
} |
| 1270 |
|
| 1271 |
if ($propInfo.Option.no_log) { |
| 1272 |
$propValue = 'VALUE_SPECIFIED_IN_NO_LOG_PARAMETER' |
| 1273 |
} |
| 1274 |
if ($propValue -is [System.Collections.IList]) { |
| 1275 |
$propValue = @($propValue | Sort-Object) |
| 1276 |
} |
| 1277 |
$module.Diff.after[$propInfo.Name] = $propValue |
| 1278 |
} |
| 1279 |
} |
| 1280 |
|
| 1281 |
# Only New-ADObject has the -ProtectedFromAccidentialDeletion while |
| 1282 |
# other cmdlets do not. Check for this and manually run with |
| 1283 |
# Set-ADObject later if protection is desired. |
| 1284 |
# https://github.com/ansible-collections/microsoft.ad/issues/47 |
| 1285 |
$protectFromDeletion = $false |
| 1286 |
if ( |
| 1287 |
$newParams.ContainsKey('ProtectedFromAccidentalDeletion') -and |
| 1288 |
-not $newCommand.Parameters.ContainsKey('ProtectedFromAccidentalDeletion') |
| 1289 |
) { |
| 1290 |
$protectFromDeletion = $newParams.ProtectedFromAccidentalDeletion |
| 1291 |
$newParams.Remove('ProtectedFromAccidentalDeletion') |
| 1292 |
} |
| 1293 |
|
| 1294 |
try { |
| 1295 |
$adObject = & $newCommand @newParams @adParams |
| 1296 |
} |
| 1297 |
catch { |
| 1298 |
# Using FailJson means other useful debugging information |
| 1299 |
# like the diff output is returned |
| 1300 |
$module.FailJson("New-$ModuleNoun failed: $_", $_) |
| 1301 |
} |
| 1302 |
$module.Result.changed = $true |
| 1303 |
|
| 1304 |
if ($module.CheckMode) { |
| 1305 |
$objectDN = "$namePrefix=$($adName -replace ',', '\,'),$objectPath" |
| 1306 |
$objectGuid = [Guid]::Empty # Dummy value for check mode |
| 1307 |
} |
| 1308 |
else { |
| 1309 |
$objectDN = $adObject.DistinguishedName |
| 1310 |
$objectGuid = $adObject.ObjectGUID |
| 1311 |
|
| 1312 |
if ($protectFromDeletion) { |
| 1313 |
$adObject | Set-ADObject @adParams -ProtectedFromAccidentalDeletion $true |
| 1314 |
} |
| 1315 |
} |
| 1316 |
} |
| 1317 |
else { |
| 1318 |
$objectDN = $adObject.DistinguishedName |
| 1319 |
$objectGuid = $adObject.ObjectGUID |
| 1320 |
$objectName = $adObject.Name |
| 1321 |
$objectPath = @($objectDN -split '[^\\],', 2)[-1] |
| 1322 |
|
| 1323 |
$commonParams = @{ |
| 1324 |
Confirm = $false |
| 1325 |
Identity = $adObject.ObjectGUID |
| 1326 |
PassThru = $true |
| 1327 |
WhatIf = $module.CheckMode |
| 1328 |
} |
| 1329 |
$setParams = @{} |
| 1330 |
|
| 1331 |
$diffAttributes = @{} |
| 1332 |
$null = Update-AnsibleADSetADObjectParam @attributes -Splat $setParams -Diff $diffAttributes -ADObject $adObject |
| 1333 |
|
| 1334 |
$module.Diff.before.attributes = $diffAttributes.before |
| 1335 |
$module.Diff.after = @{ |
| 1336 |
attributes = $diffAttributes.after |
| 1337 |
name = $objectName |
| 1338 |
path = $objectPath |
| 1339 |
} |
| 1340 |
|
| 1341 |
foreach ($propInfo in $PropertyInfo) { |
| 1342 |
$propValue = $module.Params[$propInfo.Name] |
| 1343 |
if ($null -eq $propValue) { |
| 1344 |
continue |
| 1345 |
} |
| 1346 |
|
| 1347 |
if ($propInfo.Set) { |
| 1348 |
$null = & $propInfo.Set $module $adParams $setParams $adObject |
| 1349 |
} |
| 1350 |
elseif ($propInfo.Attribute) { |
| 1351 |
$actualValue = $adObject[$propInfo.Attribute] |
| 1352 |
|
| 1353 |
$compareParams = @{ |
| 1354 |
Existing = $actualValue |
| 1355 |
CaseInsensitive = $propInfo.DNLookup -or $propInfo.CaseInsensitive |
| 1356 |
} |
| 1357 |
|
| 1358 |
# If a dictionary (add/set/remove) and is not a DNLookup single value |
| 1359 |
if ($propValue -is [System.Collections.IDictionary] -and $propInfo.Option.type -ne 'raw') { |
| 1360 |
if ($propInfo.DNLookup) { |
| 1361 |
foreach ($actionKvp in $propValue.GetEnumerator()) { |
| 1362 |
if ($null -eq $actionKvp.Value -or $actionKvp.Key -eq 'lookup_failure_action') { continue } |
| 1363 |
|
| 1364 |
$convertParams = @{ |
| 1365 |
Module = $module |
| 1366 |
Context = "$($propInfo.Name).$($actionKvp.Key)" |
| 1367 |
FailureAction = $propValue.lookup_failure_action |
| 1368 |
} |
| 1369 |
$dns = $actionKvp.Value | ConvertTo-AnsibleADDistinguishedName @adParams @convertParams |
| 1370 |
$compareParams[$actionKvp.Key] = @($dns) |
| 1371 |
} |
| 1372 |
} |
| 1373 |
else { |
| 1374 |
$compareParams.Add = $propValue['add'] |
| 1375 |
$compareParams.Remove = $propValue['remove'] |
| 1376 |
$compareParams.Set = $propValue['set'] |
| 1377 |
} |
| 1378 |
} |
| 1379 |
elseif ([string]::IsNullOrWhiteSpace($propValue)) { |
| 1380 |
$compareParams.Set = @() |
| 1381 |
} |
| 1382 |
elseif ($propInfo.DNLookup) { |
| 1383 |
$compareParams.Set = @($propValue | ConvertTo-AnsibleADDistinguishedName @adParams -Module $module -Context $propInfo.Name) |
| 1384 |
} |
| 1385 |
else { |
| 1386 |
$compareParams.Set = @($propValue) |
| 1387 |
} |
| 1388 |
|
| 1389 |
$res = Compare-AnsibleADIdempotentList @compareParams |
| 1390 |
$newValue = $res.Value |
| 1391 |
if ($res.Changed) { |
| 1392 |
if ($propInfo.IsRawAttribute) { |
| 1393 |
if ($newValue) { |
| 1394 |
$toSet = @( |
| 1395 |
if ($propInfo.SupportsReplace) { |
| 1396 |
@{ Prop = 'Replace'; Value = $newValue } |
| 1397 |
} |
| 1398 |
else { |
| 1399 |
@{ Prop = 'Add'; Value = $res.ToAdd } |
| 1400 |
@{ Prop = 'Remove'; Value = $res.ToRemove } |
| 1401 |
} |
| 1402 |
) |
| 1403 |
|
| 1404 |
foreach ($setInfo in $toSet) { |
| 1405 |
if (-not $setInfo.Value) { |
| 1406 |
continue |
| 1407 |
} |
| 1408 |
if (-not $setParams.ContainsKey($setInfo.Prop)) { |
| 1409 |
$setParams[$setInfo.Prop] = @{} |
| 1410 |
} |
| 1411 |
$setParams[$setInfo.Prop][$propInfo.Attribute] = $setInfo.Value |
| 1412 |
} |
| 1413 |
} |
| 1414 |
else { |
| 1415 |
if (-not $setParams.ContainsKey('Clear')) { |
| 1416 |
$setParams['Clear'] = [System.Collections.Generic.List[string]]@() |
| 1417 |
} |
| 1418 |
$setParams['Clear'].Add($propInfo.Attribute) |
| 1419 |
} |
| 1420 |
} |
| 1421 |
else { |
| 1422 |
$setParams[$propInfo.Attribute] = $newValue |
| 1423 |
} |
| 1424 |
} |
| 1425 |
|
| 1426 |
$noLog = $propInfo.Option.no_log |
| 1427 |
if ($newValue) { |
| 1428 |
if ($res.Changed -and $noLog) { |
| 1429 |
$newValue = 'VALUE_SPECIFIED_IN_NO_LOG_PARAMETER - changed' |
| 1430 |
} |
| 1431 |
elseif ($noLog) { |
| 1432 |
$newValue = 'VALUE_SPECIFIED_IN_NO_LOG_PARAMETER' |
| 1433 |
} |
| 1434 |
|
| 1435 |
if ($newValue -is [System.Collections.IList]) { |
| 1436 |
$newValue = @($newValue | Sort-Object) |
| 1437 |
} |
| 1438 |
} |
| 1439 |
|
| 1440 |
$module.Diff.after[$propInfo.Name] = $newValue |
| 1441 |
} |
| 1442 |
} |
| 1443 |
|
| 1444 |
$finalADObject = $null |
| 1445 |
$desiredName = $module.Params.name |
| 1446 |
if ($desiredName -and $desiredName -cne $objectName) { |
| 1447 |
$objectName = $desiredName |
| 1448 |
$module.Diff.after.name = $objectName |
| 1449 |
|
| 1450 |
$finalADObject = Rename-ADObject @commonParams @adParams -NewName $objectName |
| 1451 |
$module.Result.changed = $true |
| 1452 |
} |
| 1453 |
|
| 1454 |
$desiredPath = $module.Params.path |
| 1455 |
if ($desiredPath -eq $defaultPathSentinel) { |
| 1456 |
$desiredPath = $defaultObjectPath |
| 1457 |
} |
| 1458 |
if ($desiredPath -and $desiredPath -ne $objectPath) { |
| 1459 |
$objectPath = $desiredPath |
| 1460 |
$module.Diff.after.path = $objectPath |
| 1461 |
|
| 1462 |
$addProtection = $false |
| 1463 |
if ($adObject.ProtectedFromAccidentalDeletion) { |
| 1464 |
$addProtection = $true |
| 1465 |
$null = Set-ADObject -ProtectedFromAccidentalDeletion $false @commonParams @adParams |
| 1466 |
} |
| 1467 |
|
| 1468 |
try { |
| 1469 |
$finalADObject = Move-ADObject @commonParams @adParams -TargetPath $objectPath |
| 1470 |
} |
| 1471 |
finally { |
| 1472 |
if ($addProtection) { |
| 1473 |
$null = Set-ADObject -ProtectedFromAccidentalDeletion $true @commonParams @adParams |
| 1474 |
} |
| 1475 |
} |
| 1476 |
|
| 1477 |
$module.Result.changed = $true |
| 1478 |
} |
| 1479 |
|
| 1480 |
$protectFromDeletion = $null |
| 1481 |
if ( |
| 1482 |
$setParams.ContainsKey('ProtectedFromAccidentalDeletion') -and |
| 1483 |
-not $setCommand.Parameters.ContainsKey('ProtectedFromAccidentalDeletion') |
| 1484 |
) { |
| 1485 |
$protectFromDeletion = $setParams.ProtectedFromAccidentalDeletion |
| 1486 |
$setParams.Remove('ProtectedFromAccidentalDeletion') |
| 1487 |
} |
| 1488 |
|
| 1489 |
if ($setParams.Count) { |
| 1490 |
try { |
| 1491 |
$finalADObject = & $setCommand @commonParams @setParams @adParams |
| 1492 |
} |
| 1493 |
catch { |
| 1494 |
# Using FailJson means other useful debugging information |
| 1495 |
# like the diff output is returned |
| 1496 |
$module.FailJson("Set-$ModuleNoun failed: $_", $_) |
| 1497 |
} |
| 1498 |
$module.Result.changed = $true |
| 1499 |
} |
| 1500 |
|
| 1501 |
if ($null -ne $protectFromDeletion) { |
| 1502 |
$finalADObject = Set-ADObject -ProtectedFromAccidentalDeletion $protectFromDeletion @commonParams @adParams |
| 1503 |
$module.Result.changed = $true |
| 1504 |
} |
| 1505 |
|
| 1506 |
# Won't be set in check mode |
| 1507 |
if ($finalADObject) { |
| 1508 |
$objectDN = $finalADObject.DistinguishedName |
| 1509 |
} |
| 1510 |
else { |
| 1511 |
$objectDN = "$namePrefix=$($objectName -replace ',', '\,'),$objectPath" |
| 1512 |
} |
| 1513 |
} |
| 1514 |
|
| 1515 |
# Explicit vars are set when running in check mode as the adObject may not |
| 1516 |
# have the desired values set at runtime |
| 1517 |
$module.Result.distinguished_name = $objectDN |
| 1518 |
$module.Result.object_guid = $objectGuid.Guid |
| 1519 |
} |
| 1520 |
|
| 1521 |
if ($PostAction) { |
| 1522 |
$null = & $PostAction $Module $adParams $adObject |
| 1523 |
} |
| 1524 |
|
| 1525 |
$module.ExitJson() |
| 1526 |
} |
| 1527 |
|
| 1528 |
$exportMembers = @{ |
| 1529 |
Function = @( |
| 1530 |
"Compare-AnsibleADIdempotentList" |
| 1531 |
"ConvertTo-AnsibleADDistinguishedName" |
| 1532 |
"Get-AnsibleADObject" |
| 1533 |
"Invoke-AnsibleADObject" |
| 1534 |
) |
| 1535 |
} |
| 1536 |
Export-ModuleMember @exportMembers |