VMware Cloud Community
Hetfield84
Enthusiast
Enthusiast

Add server to domain via Invoke-VMScript

Hi,

I'm trying to join a new server build to the domain via Invoke-VMScript. I'm hoping to script the whole server build process, or a good majority of it, to save on mouse clicks.

The script starts off by connecting to our vCenter server, so it can then reach the remote server and run the script below. Everything works, except for the add-computer line. When it gets to that piece it just hangs. Even without the sleep 10 line.

If i modify this, remove the invoke-vmscript command and I run the Add-Computer command on the VM locally, it joins the domain and reboots just fine. Any idea what I'm doing wrong, and how i can have the server join the domain and reboot via Invoke-VMScript?

My test server for this script is a brand new build and VMTools are up to date.

$scriptCode = "

    ## Configure the IP address and gateway ##

    New-NetIpAddress -InterfaceAlias Ethernet0 -IPAddress $ip -PrefixLength 24 -DefaultGateway $gway

   

    ## Configure the DNS Server IP address ##

    Set-DnsClientServerAddress -InterfaceAlias Ethernet0 -ServerAddresses $dns

   

    ## wait for 10 seconds ##

    sleep 10

   

    ## add the server to the domain and reboot ##

    Add-Computer –domainname $domain -restart -credential $cred2

"

#use the invoke-vmscript command to send all of the code in the $scriptCode variable above to the remote server

Invoke-VMScript -VM $vm -ScriptText $scriptCode -GuestCredential $cred

0 Kudos
6 Replies
LucD
Leadership
Leadership

There can be a couple of reasons for the hang.

You might want to add a Start-TRanscript and Stop-Transcript to you script.
That might show why it hangs (I suspect there might be a prompt of some kind).

That prompt could also be caused by the UAC.

Is UAC enabled or disabled on the target guest OS?


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

0 Kudos
Hetfield84
Enthusiast
Enthusiast

Hey LucD,

Thanks, I disabled UAC and had the same issue. I tried Start-Transcript - Stop-Transcript but the output wasn't giving me anything obvious.

I've decided to take another approach, but appreciate your help as always!

0 Kudos
dwchan
Enthusiast
Enthusiast

I am running this on a win2016 VM and encountering the same issue.  What other alternatives approach did you end up doing

0 Kudos
dwchan
Enthusiast
Enthusiast

I also tried to disable UAC, not sure if this is required on win2016 as I am using my local admin account with invoke.  Outcome was the same ;(

0 Kudos
vespavbb
Enthusiast
Enthusiast

Hi,

 

maybe ScriptType is missing

Invoke-VMScript -VM $vm -ScriptType Powershell -ScriptText $scriptCode -GuestCredential $cred

 

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

I end up going old school with 'netdom' and it works

$JoinNewDomain = "netdom join /d:$JoinDomain $strVMName /OU:$OUPath /userd:$DomainUser /passwordd:$strDomainPassword"
Invoke-VMScript -ScriptText $JoinNewDomain -VM $strVMName -GuestCredential $DCLocalCredential -ScriptType Powershell

$JoinDomain - the name of the domain you want to join
$DomainUser - expecting the standard NT format 'domain\username'
$DomainPassword - domain user password in plain text.  SecureString is not support here ;(
$DCLocalCredential - secure string to the local admin right password for the VM

0 Kudos