VMware Cloud Community
Kiala
Contributor
Contributor

Adding a list of VMs to domain

Hello guys,

I am using following script to add the VM to domain and it works great.

function Join-DomainOrWorkGroup
{

$username = "administrator"
$Domain = "test.com"
$DomainPassword = "xxxxxxx"
$Domainusername = "administrator"
$FJoinOptions = "3"

$ComputerSystem = gwmi Win32_ComputerSystem -ComputerName 10.x.x.x -Credential $username

-Authentication 6 -Impersonation Impersonate

$ComputerSystem.JoinDomainOrWorkGroup($Domain,$DomainPassword,$Domain + "\" +

$Domainusername,$null,$FJoinOptions)


}
Join-DomainOrWorkGroup

But I have two more criterias which I need to fullfill,

1.  I need to restart each VM automatically after joining it to domain.  Which command I should use and where should I add in the script?

2. I  have a list of 50 VMs on the same host. How can I implement on all of  them in a single shot. Can the script take the input of IP addresses from an excel or  csv file or any other alternative?

Thanks for the help!

0 Kudos
3 Replies
LucD
Leadership
Leadership

The domain join will cause a reboot of the machine.

You can wait for the reboot to happen and then check, in a loop, if the machine is powered on again.

Another method, which I prefer, is to query the AD to check if the machine joined to domain and then wait till it is powered on.

Query AD like this

$dcName = "MyDC" 
$compName
= "MyVM"
$adDomain
= [adsi]("LDAP://" + $dcName + ":389/dc=my,dc=domain,dc=name") $adSearch = [adsisearcher]$adDomain
$adSearch.Filter = '(&(objectClass=Computer)(name=' + $compName + '))'
$result = $adSearch.FindOne() $comp = $result.GetDirectoryEntry()

You can do this in a loop till you get a result.

Once you get the result, create another loop and wait till the machine is powered on.


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

0 Kudos
Kiala
Contributor
Contributor

Thanks LucD.

The script I am running is successfully adding the VM to a domain, but for some reason not rebooting the VM. I tested on our test VMs multiple times.

So is there a reboot VM command which we can add at the end of the script? And also how can I get the input form the csv file for the list of IP addresses and where should I integrate in my script

0 Kudos
mcourtney
Contributor
Contributor

Hi Kiala,

Assuming you've got the list of computers in a CSV file with one of the columns named IP (similar to the attached example) try this:

function Join-DomainOrWorkGroup([string]$Computer) {

   $username = "administrator"

   $Domain = "test.com"
   $DomainPassword = "xxxxxxx"
   $Domainusername = "administrator"
   $FJoinOptions = "3"

   $ComputerSystem = gwmi Win32_ComputerSystem -ComputerName $Computer -Credential $username -Authentication 6 -Impersonation Impersonate

   $ComputerSystem.JoinDomainOrWorkGroup($Domain,$DomainPassword,$Domain + "\" + $Domainusername,$null,$FJoinOptions)

   $ComputerSystem.Win32Shutdown(6) 

}

Import-CSV | ForEach-Object {

Join-DomainOrWorkGroup $_.IP

}

Regards,

Matty

0 Kudos