Auto Deploy Virtual Domain Controller on VMware using PowerCli.

Auto Deploy Virtual Domain Controller on VMware using PowerCli.

Auto Deploy Virtual Domain Controller

Hey, Guys! – PowerCli is awesome!!, and its really great when you find that it can integrate with your Microsoft infrastructure to deploy stuff!!, without any manual intervention,  a fully automated solution.

I deployed a couple of cool ” most necessary ” infrastructure VM’s via powercli and powershell, this is a series of my blogposts on deploying them and i thought it would benefit you too in your environment 🙂

Untitled

Here’s one of the script i wrote to Auto Deploy Virtual Domain Controller. We would be taking a use case of a windows 2008r2 domain controller as it needs some manual work for dcpromo etc as compared to win2k12 which now comes with cmdlets to perform those activities.

The entire script to automate this activity is pasted below and i have broken down this into sections to explain the script execution

Step 1) Provide the necessary parameters to the script, as you can see i provide here the sitename, the vcenter server username, the vcenter password, domain username (which would be used to join the domain controller as an additional dc in an existing domain), domain password, the local username and password which would be used to invoke unattended dcpromo, the vcenter server ip address, ip address of the virtual machine and the datastore for this virtual machine.

 

Once we have all the parameters ready we would add the powershell snapin for powercli to perform various vm management activities which includes vm creation and assigning the network, we would be also creating the credential objects for domain, localadmin and also the vcenter login.

 

Next we would make sure that there are no Non-Persistent OS customizations present on the vcenter, OS customizations are required to apply a certain customization to a VM while deployment or after deployment, the OS customization can apply an ip configuration settings to the VM, join it to a domain etc.., so yes you guessed it right –> No extra code for all these activities.

 

Next lets create the VM name, for this i would be using the substring() operator to trim out the first three characters from the sitename parameter passed to the script and create the $VMname variable, also i would change the window title to indicate the status that we are deploying a DC with $VMname

Untitled

Next comes the real fun part of customizing the VM as per requirements, here we would be assigning the IP details, in this script we assume that the DNS,Subnet Mask and Gateway are common so we hard-coded them in the script. For this environment we are using an existing OS customization spec for windows2008r2 vm and we create a new OS-Custom-Spec based on this and name it as “DCSpec”

 

Once we create a nonpersistent os customization spec based, next comes the fun part – creating a vm by using this OS customization spec. As you can see below in the script i clone a vm from a template, apply the non persistent os customization spec and i place it on a VMware cluster by selecting a random host. Also note that this task is executed asynchronously, Next to monitor the VM provisioning process i use the “Wait-Task” cmdlet to wait for the clone task to get completed, this would give me a nice progress bar to view the status of deployment, next we would assign the virtual machine network to the virtual machine and wait for the virtual machine to be powered on.

 

Next we would wait for the OS customization activity to start and complete for this VM, and viola 🙂 you would have a fully custom vm deployed , assigned its ip, joined  to a domain all within maximum 5 minutes !!

There is an awesome blogpost by the powercli team who authored this script, i tinkered around a bit and converted it to a function 🙂

Untitled

You can also monitor this in the events tab for the VM

Auto Deploy Virtual Domain Controller

Next lets wait for the VMtools to be running on the VM, just to make sure that the VM completes its boot up after the oscustomization process 🙂

Some times the boot from post sysprep process for windows is a bit unpredictable so if you want you can also give a wait for about 3 minutes ;), just to make sure that everything comes up correct.

Untitled

Next we would remove th non persistent OS customization, just to make sure that our environment remains clean.

 

For the current deployment we would be creating two drives P:\ ( For PageFile ) and D:\ ( To Place the NTDS and SYSLOG files )

 

Now comes the real power of powercli, “Invoke-VMScript”, this is an extraordinary cmdlet which allows you to execute scripts inside the VM guest using vmware tools, so no need for network connection to a VM, you can be 100% sure even if there is no network connectivity to the VM, the scripts will still execute, this cmdlet can be used for situations where in you cannot enable or use native powershell remoting to execute scripts  or cmdlets on remote machines. In the below Invoke-VMscript activity i am using the diskpart utility to format and make the disk online and also changing the CDROM drive letter to Z:\ and assigning the drive letter D:\ to it.

 

Similarly we would perform this activity for P:\ drive ( For Page File ), also perform the page file configuration for the VM.

 

So you guys just saw how cool and awesome the cmdlet “Invoke-VMscript” is !!, the possibilities are endless and all depends on your creativity.

Next lets create a new site in ADDS, this site would be used in our DCPROMO unattended installation, as you can see there are three steps to this,

1) ############## Creating New Site with subnet details #########

 

2) Next copy over the sitecreation.ps1 file created to an existing domain controller, as you can see we use the Copy-VMGuestFile cmdlet and it also uses vmware tools internally to copy over content without the use of network resources

 

3) Invoke the script to create a new site inside the domain controller, you can also see that i remove the script from the domain controller once the execution finishes

 

Next lets get onto the final step, which is to create the unattended file for DCPROMO, copy it to our additional domain controller and finally invoke the script inside the VM, now you can sit back and relax!.

The DCPROMO would get initiated inside the VM and you would have a new DC provisioned, all automated !! 🙂

Now just imagine, if you want to deploy over 100 additional DC’s :), create a CSV file with those details and pipe it to the script and use a For-EachObject 🙂 cool right!

I hope you enjoyed this blogpost on “Auto Deploy Virtual Domain Controller” and found this information useful 🙂

Please follow and like us:

Share this post

Post Comment