VMware Cloud Community
beelzhere
Enthusiast
Enthusiast
Jump to solution

Join VM to a Domain Using PowerCLI

Hey Guys,

I am fairly new to PowerCLI and all. I am currently developing a script that allows me to join hundreds of VMs to a specified domain, the VMs have Windows 2012 and 2016. However, the problem I am encountering is that nothing is happening because I am not sure if the code is correct. I have also no idea how to put in the credentials that it would ask me in the VM. The code works within the VM after I provide the credentials, but it does not work when I run the script outside the VM environment. Any idea ?

The Code:

        $addDomain = "{

  

    $secpasswd1 = ConvertTo-SecureString 'password' -AsPlainText -Force

    $mycreds1 = New-Object System.Management.Automation.PSCredential ('username', $secpasswd1)

    Add-Computer -DomainName domainName  -Credential $mycreds1 -Restart -Force

    }"

    Invoke-VMScript -VM $VMName -ScriptText $addDomain -GuestCredential $mycreds

Thanks in advance for your help!

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You would need an updated OSCustomizationSpec, which you can do with Set-OSCustomiztionSpec.

Ideally, you would use an existing spec, then clone it into a temporary one with New-OSCustomizationSpec.
And make some changes to that temporary spec with Set-OSCustomizationSpec.

Then you use the New-VM with the temporary spec.


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

View solution in original post

0 Kudos
11 Replies
LucD
Leadership
Leadership
Jump to solution

That is most probably caused by UAC.
You need an elevated prompt for that domain join command.

But how are you deploying those VMs?

From a template?

Then you could use the OSCustomizationSpec, which allows doing a domain join.


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

0 Kudos
beelzhere
Enthusiast
Enthusiast
Jump to solution

Sorry for the late reply. I am deploying the VMs using a template and OSCustomizationSpec.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Why don't you use the domain join from the OSCustomizationSpec?


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

0 Kudos
beelzhere
Enthusiast
Enthusiast
Jump to solution

What do you mean ?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The Domain parameter on the Set-OSCustomizationSpec cmdlet.


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

0 Kudos
beelzhere
Enthusiast
Enthusiast
Jump to solution

I did not even know about this because I was creating a VM using

New-VM -Name $VMName -Template $Template -ResourcePool $Cluster -Datastore $Datastore -OSCustomizationSpec $Custom

So, I would use Set-OScustomizationSpec after New-VM, correct ?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You would need an updated OSCustomizationSpec, which you can do with Set-OSCustomiztionSpec.

Ideally, you would use an existing spec, then clone it into a temporary one with New-OSCustomizationSpec.
And make some changes to that temporary spec with Set-OSCustomizationSpec.

Then you use the New-VM with the temporary spec.


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

0 Kudos
beelzhere
Enthusiast
Enthusiast
Jump to solution

Okay, let me try them and will update on the progress. Btw thank you for helping me, I really appreciate it.

0 Kudos
beelzhere
Enthusiast
Enthusiast
Jump to solution

This helped me. I used Set-OSCustomizationSpec before New-VM. However, I am unable to install an msi file using 

$installSoft = 'Start-Process -FilePath "C:\fileName.msi" -ArgumentList "/install" -Wait'

    Invoke-VMScript -VM $VMName -ScriptText $installSoft -GuestCredential $mycreds

Any help with this ? or should I open a new question ?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You will probably need to use the 'silent' switch for msi installation.


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

0 Kudos
beelzhere
Enthusiast
Enthusiast
Jump to solution

You are right, it needed an argument of /quiet. Thank you for your help, LucD.

0 Kudos