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!
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
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
Sorry for the late reply. I am deploying the VMs using a template and OSCustomizationSpec.
Why don't you use the domain join from the OSCustomizationSpec?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
What do you mean ?
The Domain parameter on the Set-OSCustomizationSpec cmdlet.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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 ?
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
Okay, let me try them and will update on the progress. Btw thank you for helping me, I really appreciate it.
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 ?
You will probably need to use the 'silent' switch for msi installation.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You are right, it needed an argument of /quiet. Thank you for your help, LucD.