When I started deploying servers, the process involved racking the hardware, connecting it to the network, inserting a CD/DVD, installing the operating system and drivers, configuring network settings in the OS and then installing and configuring services such as Active Directory or Exchange. These tasks were done using a GUI. Needless to say, this process has become archaic.
vSphere made the process of building a server much simpler by leveraging virtual machines. But many users still rely on using the GUI for bringing up new systems with the Windows vSphere client. In vSphere, servers can be built quickly and easily using PowerCLI. Code is king when deploying servers and using a GUI lacks scalability. We hire people who want to make the best things in the world. -Steve Jobs
In PowerCLI, the New-VM cmdlet is used to create a new virtual machine. A few important things that can be set with New-VM are the following:
- VM name
- Size of hard disk
- Amount of memory
- VM template
- Datastore location of disks
- Network settings
Here is an example of creating a VM:
- Connect to my vCenter server named “vcenter” in PowerShell.
C:> Connect-VIServer –Server ‘vcenter’
- Run New-VM while specifying the VM name, VMHost, datastore, number of CPU’s, hard disk size and the network name.
C:> New-VM -Name ‘TestVM’ –VMHost ‘VMHost-1’ -Datastore ‘TestDatastore’ -DiskGB 40 -MemoryGB 8 -NumCpu 2 -NetworkName ‘Virtual Machine Network’
Virtual machine templates
This did technically create a VM in vSphere, but it is the equivalent of racking a new server with no OS installed. This is one area where VM templates are very useful. A VM template is a “golden image” that you can use to deploy a new virtual machine.
To create a template, create a new virtual machine, install the OS and configure any settings you would like to standardize with future virtual machines. Then you either clone or convert that virtual machine for a template.
Here is an example of cloning a new template called “Win7Template” from the VM “Win7VM”.
C:> New-Template -VM ‘Win2012VM’ -Name “Server2012R2Template” -Datastore ‘TestDatastore’ -Location ‘TestLocation’
OS customization specifications are used to automate certain settings and tasks such as setting a computer’s hostname and joining a computer to a domain. To create an OS specification we use the cmdlet New-OSCustomizationSpec in PowerCLI.
I created a new OS spec named “WindowsServer2012”. I included the local administrator password, domain name, domain joining credentials, OS type, time zone and product key to be used. Note by default the network settings are configured for DHCP. You can set a static IP with the cmdlet New-OSCustomizationNicMapping.
C:> New-OSCustomizationSpec -Name ‘WindowsServer2012’ -FullName ‘TestName’ -OrgName ‘TestOrg’ -OSType Windows -ChangeSid -AdminPassword (Read-Host -AsSecureString) -Domain ‘DOMAIN’ -TimeZone 035 -DomainCredentials (Get-Credential) -ProductKey ‘1111-1111-1111-1111’ -AutoLogonCount 1
Deploy a virtual machine from a template and customize settings
Now that I have a VM template and OS customization specs, I can build a new virtual machine that will have the OS installed, be joined to a domain, set the product key and have the time zone set.
First, I place my OS Customization specs in the variable $OSSpecs.
$OSSpecs = Get-OSCustomizationSpec -Name ‘WindowsServer2012’
Next, I place my VM template in a variable called $VMTemplate.
$VMTemplate = Get-Template -Name ‘Server2012R2Template’
Finally, I deploy my VM with the New-VM cmdlet using my template and OS specs. I place the VM on the “VMHost-1” ESXi host and store the VM on the “TestDatastore” datastore.
New-VM -Name ‘TestVM’ -Template $VMTemplate -OSCustomizationSpec $OSSpec -VMHost ‘VMHost-1’ -Datastore ‘TestDatastore’