Hey everyone. Today i wanted to share something I’ve been working on and off on for the last couple of weeks.
It is a utility written completely in PowerShell that allows a technican to enter in some basic information about a VM he wants to create and the script pulls the other required values and settings and takes over the whole process from there.
Below is an In Depth outline of what this tool achieves and what you would need to edit to make this tool work for your team.
-
The user selects a VCenter Server from the dropdown menu. We are a global organization and have 3 to choose from. You may only need one in your instance.
-
The user gets prompted for the required credentials for that connection. We require specific admin accounts for this access.
-
A connection is made to that server which persists for as long as the tool is open.
-
The user is then presented with a list of all datacenters or local offices that exist on that vcenter server. They choose from the dropdown the datacenter they want the VM placed in.
-
Some work is done in the background and the user is presented with the available ESX clusters and Hosts for that datacenter. Hosts are placed at the top of the list. They choose an ESX cluster or host from this dropdown.
-
More filtering is done in the background and they are presented a list of datastore clusters and datastores to choose from that are available for that ESX host or cluster. Again clusters are presented at the top of the list. They choose and item from this dropdown.
-
The user is then presented with a list of Templates to choose from.
-
The user is next asked to choose the customization they want applied to the VM.
-
Next the user needs to input the name of the VM they want to create.
-
Next the user will choose the desired number of cores from the available dropdown menu.
-
Then they must choose the amount of RAM to be assigned to the VM from the next combobox.
-
The user is then allowed to choose to add extra disks to the VM and is also prompted for drive letters but im not doing anything with those yet (i’ll get to it.)
-
Next the technician will be asked to provide an IP address to assign to the server.
-
Finally they will be asked to enter in values for some custom attributes we have in VSphere. These may or may not be applicable to your organization.
Once all that information has been entered they will hit the complete process button which starts the automation.
The complete process button kicks off the following chain of events:
-
The tech is prompted for the local admin creds for that image as we will need them later.
-
The VM is created using the Server, Resourcepool, Name, Datastore, Template, and OSCustomizationSpec parameters we have already gathered. They get a progress bar as this process does its thing.
-
Once the machine is created we set the Num of VCPU’s on the box.
-
Next we set the amount of RAM chosen previously.
-
We then add the extra disks if any were chosen by the user.
-
Once complete we validate the IP address that was entered is an actual IP address.
-
We then determine automatically the portgroup that this VM should be applied to. We do this heavily based off of naming convention so this portion will most likely not work for others and would need to be re-worked.
-
If only one possible portgroup matched we call the function to set that portgroup on the VM. Otherwise we prompt the user to choose from a dropdown menu of the available portgroups which matched our filters and then set the proper group on the VM.
-
Once the port group has been set we start the VM.
-
The tool then waits till it gets a notification that the customization process has begun on the machine, and once begun it waits again until it gets a completion event.
-
Once the customization is complete, we update VMware tools on the machine.
-
Once VMware tools have been updated we make the notes and annotations on this machine in VSPhere.
-
We then wait for the guest operating system to become available for remote use again.
-
Once the OS is ready, we set the IP, Mask and default gateway in the guest OS using Invoke-VMScript.
-
Once complete we set the DNS information on the VM. also using invoke-vmscript.
-
We then enable ps-remoting on the machine also using Invoke-VMScript. We also verify it worked by testing with a simple invoke-command.
-
Once PS remoting is enabled we join this machine to the domain which causes a reboot.
-
We monitor and wait for the reboot to complete.
-
Once completed we verify the Domain join worked as desired.
-
Once the machine is joined to the domain we run a final function to apply some desired customizations. We then log out the results of those customizations to the Rich text box for the user to copy/paste out.
Now i know that not all of you would want all of this code but i hope that the tool serves as a useful example on some of the stuff you can do inside the VMWARE cli. I just started learning vmware a month ago and its been a lot of fun.
I would love suggestions, feedback, good natured criticism or candy
The files are available at the following GITHUB address. Please note that if you have PSStudio you will be able to downloaf the PS project file (Which i reccomend as it will make the code look cleaner.
If you aren’t using PS Studio just grab “Server Build Tool.Export.ps1”
submitted by /u/hematic
[link] [comments]
The post Automated Server Build Utility (VMWARE) appeared first on How to Code .NET.