I am trying to use apply a host profile via powershell script that will add an esxi host to my active directory domain.
$vCenter = Read-Host "Enter vCenter"
$esxhost = Read-Host "Enter FQDN of the ESXi host"
$ADdomaincreds = $host.ui.PromptForCredential("Enter Credentials", "Please enter your Active Directory username and password.", "", "")
$hostprofile = Read-Host "Enter HostProfile to apply"
#CONVERT SECURESTRING
$CONVERT_AD_PASSWORD = http://System.Runtime.InteropServices.Marshal::SecureStringToBSTR($ADdomaincreds.Password)
$AD_PASSWORD = http://System.Runtime.InteropServices.Marshal::PtrToStringAuto($CONVERT_AD_PASSWORD)
Connect-VIServer $vCenter
$hostprof = Get-VMHostprofile -Name $hostprofile
$applyhost = Get-VMHost $esxhost
Set-VMHost -VMHost $applyhost -State "maintenance"
$additionalConfiguration = Apply-VMHostProfile -ApplyOnly -Profile $hostprof -Entity $applyhost -Confirm:$false
$additionalConfiguration = $ADdomaincreds.username
$additionalConfiguration= $AD_PASSWORD
$additionalConfiguration = Apply-VMHostProfile -Profile $hostprof -Entity $applyhost -Variable $additionalConfiguration -Confirm:$false
It runs with no errors but when I look at the esxi host it still says it is using Local Authentication. If I apply the host profile via the VI Client, it works with no issue. Is there anything else I need to set in the variable?
I have also tried using LucD's Set-VMHostADDomain function. That works, however, if I then apply my host profile to finish configuring the other components such as syslog, ntp, etc, the authentication gets reset to local authentication after a reboot.