Afetr deploying Unable to join domain using PowerCLI


Using the below Script i am able to deploy the VM (OS - Win2k8R2) but its not oining the domain. Could someone assist me.

$Domainuser = ""
$LocalUser = Read-Host "Type Local Admin"
$LocalPass = Read-Host "Type local Admin Pass" -AsSecureString
#$getcredential = Get-Credential
$domainpassword = Read-Host "type domain password" -AsSecureString
$viserver = Read-Host "Enter VC Name"
Connect-VIServer $viserver

Import-Csv "C:\PS\newserverdeploy.csv" -UseCulture | %{
#Create AD Object
$Server = $_.ServerName

#Create DNS Entry

Remove-OSCustomizationSpec $_.customizationName -Confirm:$false
  $Randomhost = Get-Cluster $_.clustername | Get-VMHost | where {$_.state -notmatch "Maintenance"} | get-random
$portgroup = $Randomhost | Get-VirtualPortGroup | select name
$inputportgrp1 = $_.inputportgrp1
$portgroup | ForEach-Object {if ($_.Name -match $inputportgrp1) {
$Networkname1 = $
Write-Host $networkname1
$inputportgrp2 = $_.inputportgrp2
  $portgroup | ForEach-Object {if ($_.Name -match $inputportgrp2) {
$Networkname2 = $
Write-Host $networkname2

Get-OSCustomizationSpec $_.customizationname | Set-OSCustomizationSpec -FullName $localuser -AdminPassword $localpass -domain "" -DomainUsername $Domainuser -DomainPassword $domainpassword -AutoLogonCount 2 -Confirm:$false

Get-OSCustomizationSpec $_.customizationname | Get-OSCustomizationNicMapping | where { $_.Position -eq '1'} | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $_.ip1 -SubnetMask $_.subnet1 -DefaultGateway $_.Gateway1 -Dns $_.dns1,$_.dns2

New-vm -Name $Server -VMhost $randomhost -Template $_.Template -Datastore $_.Datastore -OSCustomizationspec $_.customization
Start-Sleep -Seconds 10
Set-VM $_.Servername -MemoryMB $_.Memory -Confirm:$false
Start-Sleep 10
Set-VM $_.Servername -NumCpu $_.CPU -Confirm:$false

Start-VM $_.servername
Start-Sleep -Seconds 60

Get-NetworkAdapter -VM $_.ServerName | where {$_.Name -match "1"} | Set-NetworkAdapter -NetworkName $Networkname1 -Connected:$true -StartConnected:$true -Confirm:$false
Get-NetworkAdapter -VM $_.ServerName | where {$_.Name -match "2"} | Set-NetworkAdapter -NetworkName $Networkname2 -Connected:$true -StartConnected:$true -Confirm:$false

Start-Sleep -Seconds 30


0 Kudos
2 Replies

How are you creating the AD object?  If by script, then please include AD object creation script.  Also please try this for your credentials:

#Embedded credentials to join computer to domain
$User = "$AdminAccount"
$Pass = ConvertTo-SecureString "$AdminPassword" -AsPlainText -Force
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User,$Pass
$domainuser = $credentials.GetNetworkCredential().UserName
$domainpassword = $credentials.GetNetworkCredential().Password

0 Kudos

Are you expecting that the server auto joins the domain? I see that you are using a static IP address on the VM. Unfortunately, due to the order in which the customization happens, the static IP address is only applied after a second reboot. To autojoin the domain, you would need to use a DHCP server to make sure the machine already has an IP address at the time the domain join procedure happens.

Good design comes from intelligence.
0 Kudos