VMware Cloud Community
n3o
Contributor
Contributor

Windows server cloning, how to automate joining domain

We have a test group that at anytime has about a hundred test servers. When they move to a new platform, they typically present a gold image of a Windows server, and request that the VMware admins turn it into a template and push out a hundred machines, which are then manually joined to the domain. I am trying to automate the process (especially of joining to the domain), and this is a new concept for me, any ideas/suggestions?

0 Kudos
31 Replies
LucD
Leadership
Leadership

Could you share the exact error message you are getting back?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vespavbb
Enthusiast
Enthusiast

It looks like something is wrong inside the VM

I´m using a script to set the ip inside the VM as well, but there is a problem, i have 2 networkadapter called Ethernet and the ip route is not set correctly. Let me fix that first, maybe thats the reason why the join is not working.

VCP4,VCP5,VCP6,VCP7,VCP8
0 Kudos
vespavbb
Enthusiast
Enthusiast

Hi  Luc,

I did a workaround with the the netdom command, this works better, I don know the thing with @´ is not working at all

first question. I need to do a password trick to get it back in plaintext, otherwise the invoke command has problems to get the password

$DomainAccountPWD = Get-Credential $userID | ConvertTo-SecureString -asPlainText -force is not working, $DomainAccountPWD will not be correct transferd in the invoke command. Is there a better way to do it? or i use the plaintext trick

second question

reboot and wait-tools is not working reliable, mostly the script is running over.. whats wrong?

thanks

#######################################

#

cls

$userid = whoami

#$DomainAccountPWD = Get-Credential $userID | ConvertTo-SecureString -asPlainText -force   ### not working

$domain = 'mydomain'

Param(

    $DomainAccountPWD = (Read-Host "Dein Domain Admin Password für den Join" -AsSecureString)

)

Write-Host "Encrypted Password: $(ConvertFrom-SecureString $DomainAccountPWD)"

$ADpass = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($DomainAccountPWD))

#VM Inventory names to match

$matchVMs = "VMTEST01"

#Guest Credentials - Must have required permissions to change IP address

$GuestUserName = "Administrator"

$GuestPassword = "localpassword!"

##############NO CHANGES BEYOND THIS POINT##############

#List of VMs (vCenter Inventory Names) to change

Write-Host "Getting list of VMs from Inventory where Inventory Name contains $matchVMs"

$VM = (get-vm | where {$_.Name -match $matchVMs -and $_.PowerState -eq "PoweredOn"}).Name

$netdom = "netdom join $vm /domain:$domain /userd:$userID /passwordd:$ADpass"

Invoke-VMScript -vm $vm -scriptType bat -ScriptText $netdom -GuestUser $GuestUserName -GuestPassword $GuestPassword

##reboot and wait until vm is back 

Restart-VMguest -VM $VM -Confirm:$false | out-null

sleep -Seconds 5

write-host $vm "VM is rebooting"

Wait-Tools -VM $vm

write-host $vm "VM is online again"

$VMs

write-host " VM Domain Join done"

VCP4,VCP5,VCP6,VCP7,VCP8
0 Kudos
LucD
Leadership
Leadership

1) You have to remember that the here-string uses variable substitution in a string.
So you can't use composite objects, only simple types like string, int...

So yes, you will have to use the plaintext,

2) Correct Wait-Tools is not reliable to know when you can launch an Invoke-VMScript.

That is why I'm using the GuestOperationsReady property.

Something like this

while (-not $vm.ExtensionData.Guest.GuestOperationsReady)

{

    Start-Sleep 2

    $vm.ExtensionData.UpdateViewData('Guest.GuestOperationsReady')

}

In fact, after the Restart-VMGuest, you first have to wait till the VM is powered off, and then use the above loop.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vespavbb
Enthusiast
Enthusiast

ok thanks

and how could i fetch this two stati? How can i do the -or ?

Write-Host “Waiting for VM Tools to Start on $VM”

do {

Set-Variable -name ToolsStatus -Value (Get-VM $VM).extensiondata.Guest.ToolsStatus

Write-Host $toolsStatus

sleep 3

}

until ($toolsStatus -eq ‘toolsOk’ -or ‘toolsOld’ )

VCP4,VCP5,VCP6,VCP7,VCP8
0 Kudos
LucD
Leadership
Leadership

Sorry, not following.
I was talking about the GuestOperationsReady property.

The restart and wait to launch Invoke-VMScript could look like this

Restart-VMGuest -VM $vm -Confirm:$false | Out-Null

while ($vm.PowerState -ne 'PoweredOff')

{

    Start-Sleep 1

    $vm = Get-VM -Name $vm.Name

}

while (-not $vm.ExtensionData.Guest.GuestOperationsReady)

{

    Start-Sleep 2

    $vm.ExtensionData.UpdateViewData('Guest.GuestOperationsReady')

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
frased
Enthusiast
Enthusiast

Running vCenter 6.5 U3.  Noticed guest customization specific settings is deprecated now?

Moved from the clunky 5.5 thick client vCenter.  Guest customization had specific network/machine name settings during deploy. 

Is running this script the only way now to add specific static IP settings to the machine during clone/template deploys? 

0 Kudos
LucD
Leadership
Leadership

Where did you see that deprecated message?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
frased
Enthusiast
Enthusiast

I only see add new or modify existing...  the thick client had an option to specify custom settings without creating a new customization. 

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership

That was only when you clone a VM or deploy from a Template, afaik.
And that is still there.

And you can assign an OSCustomizationSpec to an existing VM with Set-VM.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vespavbb
Enthusiast
Enthusiast

Hi luc,

if I run the loop, an it never end.... I was waiting about 10 min and nothing happend. VM was restartet less than a minute. Am I doing something wrong?

Restart-VMGuest -VM $vm -Confirm:$false | Out-Null

while ($vm.PowerState -ne 'PoweredOff')

{

    Start-Sleep 1

    $vm = Get-VM -Name $vm.Name

}

while (-not $vm.ExtensionData.Guest.GuestOperationsReady)

{

    Start-Sleep 2

    $vm.ExtensionData.UpdateViewData('Guest.GuestOperationsReady')

}

VCP4,VCP5,VCP6,VCP7,VCP8
0 Kudos
LucD
Leadership
Leadership

No, that looks ok.

Try to determine in which loop it gets stuck.

Preferably with a debugger, but you can also add some Write-Host lines in the code.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos