diff --git a/lib/ansible/modules/windows/win_domain_membership.ps1 b/lib/ansible/modules/windows/win_domain_membership.ps1 index ee280bb8015..e08e17c1585 100644 --- a/lib/ansible/modules/windows/win_domain_membership.ps1 +++ b/lib/ansible/modules/windows/win_domain_membership.ps1 @@ -45,6 +45,23 @@ Function Get-DomainMembershipMatch { return $domain_match } + catch [System.Security.Authentication.AuthenticationException] { + Write-DebugLog "Failed to get computer domain. Attempting a different method." + Add-Type -AssemblyName System.DirectoryServices.AccountManagement + $user_principal = [System.DirectoryServices.AccountManagement.UserPrincipal]::Current + If ($user_principal.ContextType -eq "Machine") { + $current_dns_domain = (Get-CimInstance -ClassName Win32_ComputerSystem -Property Domain).Domain + + $domain_match = $current_dns_domain -eq $dns_domain_name + + Write-DebugLog ("current domain {0} matches {1}: {2}" -f $current_dns_domain, $dns_domain_name, $domain_match) + + return $domain_match + } + Else { + Fail-Json -obj $result -message "Failed to authenticate with domain controller and cannot retrieve the existing domain name: $($_.Exception.Message)" + } + } Catch [System.DirectoryServices.ActiveDirectory.ActiveDirectoryObjectNotFoundException] { Write-DebugLog "not currently joined to a reachable domain" return $false