Expedite your VM provisioning process with PowerShell remoting

By using a golden image to clone new VMs and PowerShell remoting to configure them, administrators can quickly respond to increased service demands.

Rapidly deploying new virtual machines for an increase in service demand, virtual desktop deployments or to quickly build a lab environment has gotten a whole lot easier in Hyper-V 3 with PowerShell. In the first part of this two-part series, I will take you through the process of building a golden image that you can use to rapidly deploy and configure new VMs.

Before you start

Before we get started, there are a few things you will want to check in your environment -- or lab environment -- for this to work. The magic in this process is using PowerShell remoting to configure the servers -- or clients -- after you have deployed them. Windows Server 2012 enables PowerShell remoting by default, but you will have to enable it manually in earlier versions. If you're not sure how to do that, you can learn from the free e-book, Secrets of PowerShell Remoting.

Because the newly deployed servers or clients will not be members of the domain until after configuration, you will need to change a setting on your management computer so that PowerShell remoting will connect to nondomain computers. On your computer, open PowerShell and type the following:

Set-Item -Path WSMan:\localhost\Client\TrustedHosts -Value *

Of course, you will also need at least one Windows Server 2012 running Hyper-V 3, a domain controller and Dynamic Host Configuration Protocol (DHCP) or Bootstrap Protocol (BOOTP) -- and I'm sure you already do; I'm just adding this as a reminder, in case you want to build a lab environment for testing.

Now all you need is a golden image that will act as the base image for the deployments.

More on Windows PowerShell

Understanding new PowerShell cmdlets

What you need to know about Windows PowerShell

Top Hyper-V cmdlets for basic tasks

Making the golden image

The golden image is the heart of the process and quite simple to create. The golden image is a VM that contains the operating system and has been prepared for cloning using SysPrep.exe. I keep my server golden images pretty simple, not much more than a basic install and administrative account. You can add roles -- or any software -- later, using PowerShell. You will only need the virtual disk -- a .vhd or .vhdx file -- when you're finished, so you can delete the VM from Hyper-V once it's built. Here are the main considerations I use when building the image:

  • VM settings.  Don't worry about memory or other basic settings for the VM, because each of these important settings will be handled after the deployment.
  • Computer name.  The name you choose for the computer doesn't matter, because it will be changed by the SysPrep process. As you will see, after the deployment you will use PowerShell to change it to a specific name.
  • Administrative account.  You need a password for the Administrator account so that you can connect to the new server using PowerShell remoting.
  • IP address settings.  The network adapter should obtain the IP address dynamically from DHCP or BOOTP. If you need a server to have a specific IP address, you will be able to set that after the deployment using PowerShell.

When you're finished configuring the server, make sure to run SysPrep.exe to keep computer names and security identifiers unique. Here's an example:

c:\windows\system32\syspre\sysprep.exe /generalize /oobe /shutdown

The golden image is now ready to be deployed and configured for additional servers. This begins with using the golden image to make new virtual disks for the new servers.

Creating the virtual disks

I start my deployments by creating virtual disks for the new servers. This is normally the most time-consuming part of the entire process. You can copy the entire virtual disk to create a standalone disk for the new VM or create a differencing disk based on the golden image. I like to use the PowerShell Copy-Item cmdlet to make full-size copies of a disk. You can set the destination parameter to a local folder or a universal naming convention path.

Copy-Item -Path C:\hyperv\GoldImageCore.vhdx -Destination c:\hyperv\FullDisk.vhdx

If you want to create differencing disks, use the Hyper-V 3 cmdlet New-VHD, as in the example below:

New-VHD -ParentPath C:\hyperv\GoldImageCore.vhdx -Path c:\hyperv\diffDisk.vhdx -Differencing

After the virtual disk has been copied, you can create a new VM in Hyper-V using the Hyper-V cmdlets.

Creating the new virtual machines

The cmdlet New-VM will create a VM, attach a virtual disk and assign the VM to a virtual switch. Here is an example and an explanation of the parameters I chose:

New-VM -Name Server1 -VHDPath C:\hyperv\diffDisk.vhdx-SwitchName (Get-VMSwitch -name inside* | Select-Object -ExpandProperty name)

  • -Name. This is the name of the VM
  • -VHDPath. This is the path to the virtual disk for the VM
  • -SwitchName. This is the virtual switch for the VM. In a previous article, I demonstrated how to create and manage a virtual switch using PowerShell. In the above command, I'm using a parenthetical and Get-VMSwitch to get the name of the virtual switch I want assigned to the VM.

After the VM is added to Hyper-V, you can configure options for the VM, such as memory and number of CPUs. Below, I configure the Server1 VM to use dynamic memory with a startup memory of 1 GB. I also set the minimum and maximum memory settings.

Set-VMMemory -VMName Server1 -DynamicMemoryEnabled $True -StartupBytes 1GB -MinimumBytes 512mb -MaximumBytes 2GB

Then start the VM!

Start-VM -Name Server1

Post-deployment

Remember that the new VM is not a member of the domain and has a generated computer name. Now is when you can use PowerShell remoting to connect to the VM and change those settings.

First, get the IP address of the new VM and store it to a variable. The example below grabs the IPV4 address:

$ip=(Get-VMNetworkAdapter -VMName server1 | Select -ExpandProperty ipaddresses)[0]

You will need to supply credentials to the new VM to use PowerShell remoting. Because I will need these often, I store the credentials to a variable:

$cred=Get-Credential

Now use the Add-Computer cmdlet through PowerShell remoting to join the server to the domain and set its computer name:

Invoke-Command -ComputerName $ip -Credential $cred {Add-Computer -NewName Server2-DomainName company.loc -Credential company\administrator -Restart -Force}

Once the computer is added to the domain, you can continue to use PowerShell remoting to install roles -- or any additional software -- and make configuration changes. Here's an example of installing the Web-Server role to the new VM.

Invoke-Command -ComputerName server1 {Install-WindowsFeature Web-Server}

In part two of this series, I will demonstrate how to automate (script) this entire process to feed computer names from a text or comma-separated values file and build as many VMs as you need!

This was first published in January 2013

Dig deeper on Virtual machine provisioning and configuration

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchVMware

SearchWindowsServer

SearchCloudComputing

SearchVirtualDesktop

SearchDataCenter

Close