From e77426dad36426bf80784e99d1a0c1caee3ba0da Mon Sep 17 00:00:00 2001 From: sergioirium <37545023+sergioirium@users.noreply.github.com> Date: Wed, 13 Nov 2019 00:13:44 +0100 Subject: [PATCH] Corrected Get-adcomputer (#63093) * Corrected Get-adcomputer Corrected Get-adcomputer on "Remove-ConstructedState" and "Set-ConstructedState" functions. resolved error: Unable to contact the server. This may be because this server does not exist, it is currently down, or it does not have the Active Directory Web Services running. * Update lib/ansible/modules/windows/win_domain_computer.ps1 Co-Authored-By: Daniel-Sanchez-Fabregas <33929811+Daniel-Sanchez-Fabregas@users.noreply.github.com> * Update win_domain_computer.ps1 changed "-credential $credential" to "@extra_args" (Line 115 and 150) corrected exception message (Line 122) * Added changelog fragment, minor code tweak --- .../win_domain_computer-credential.yaml | 2 ++ .../modules/windows/win_domain_computer.ps1 | 16 ++++++++-------- 2 files changed, 10 insertions(+), 8 deletions(-) create mode 100644 changelogs/fragments/win_domain_computer-credential.yaml diff --git a/changelogs/fragments/win_domain_computer-credential.yaml b/changelogs/fragments/win_domain_computer-credential.yaml new file mode 100644 index 00000000000..ebcd884f941 --- /dev/null +++ b/changelogs/fragments/win_domain_computer-credential.yaml @@ -0,0 +1,2 @@ +bugfixes: +- win_domain_computer - Honour the explicit domain server and credentials when moving or removing a computer object - https://github.com/ansible/ansible/pull/63093 diff --git a/lib/ansible/modules/windows/win_domain_computer.ps1 b/lib/ansible/modules/windows/win_domain_computer.ps1 index f53f46b595a..86c380e8ff7 100644 --- a/lib/ansible/modules/windows/win_domain_computer.ps1 +++ b/lib/ansible/modules/windows/win_domain_computer.ps1 @@ -112,14 +112,14 @@ Function Set-ConstructedState($initial_state, $desired_state) { If ($initial_state.distinguished_name -cne $desired_state.distinguished_name) { # Move computer to OU Try { - Get-ADComputer -Identity $desired_state.name | + Get-ADComputer -Identity $desired_state.name @extra_args | Move-ADObject ` -TargetPath $desired_state.ou ` -Confirm:$False ` -WhatIf:$check_mode ` @extra_args } Catch { - Fail-Json -obj $result -message "Failed to move the AD object $($desired_state.name) to $($desired_state.ou) OU: $($_.Exception.Message)" + Fail-Json -obj $result -message "Failed to move the AD object $($initial_state.distinguished_name) to $($desired_state.distinguished_name): $($_.Exception.Message)" } } $result.changed = $true @@ -147,12 +147,12 @@ Function Add-ConstructedState($desired_state) { # ------------------------------------------------------------------------------ Function Remove-ConstructedState($initial_state) { Try { - Get-ADComputer $initial_state.name ` - | Remove-ADObject ` - -Recursive ` - -Confirm:$False ` - -WhatIf:$check_mode ` - @extra_args + Get-ADComputer -Identity $initial_state.name @extra_args | + Remove-ADObject ` + -Recursive ` + -Confirm:$False ` + -WhatIf:$check_mode ` + @extra_args } Catch { Fail-Json -obj $result -message "Failed to remove the AD object $($desired_state.name): $($_.Exception.Message)" }