Fixes authentication error when ansible_user is a local account and computer is already joined to domain (#44270)

* Fixes authentication error when ansible_user is a local account and computer is already joined to domain

* recommended fixes
This commit is contained in:
Mike Klebolt 2018-09-10 22:20:45 -05:00 committed by Jordan Borean
parent 3371a779b6
commit ee8a10b87d

View file

@ -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