This article by Vinith Menon, the author of Microsoft Hyper-V PowerShell Automation, delves into the basics of Hyper-V, right from installing Hyper-V to resizing virtual hard disks.
The Hyper-V PowerShell module includes several significant features that extend its use, improve its usability, and allow you to control and manage your Hyper-V environment with more granular control.
Various organizations have moved on from Hyper-V (V2) to Hyper-V (V3). In Hyper-V (V2), the Hyper-V management shell was not built-in and the PowerShell module had to be manually installed. In Hyper-V (V3), Microsoft has provided an exhaustive set of cmdlets that can be used to manage and automate all configuration activities of the Hyper-V environment. The cmdlets are executed across the network using Windows Remote Management.
In this article, we will cover:
(For more resources related to this topic, see here.)
Here is a list of all the new features introduced in Hyper-V in Windows Server 2012 R2. We will be going in depth through the important changes that have come into the Hyper-V PowerShell module with the following features and functions:
Before you proceed with the installation and configuration of Hyper-V, there are some prerequisites that need to be taken care of:
Once the prerequisites have been taken care of, let's start with installing the Hyper-V role:
Install-WindowsFeature –Name Hyper-V -IncludeManagementTools - Restart
Get-WindowsFeature -Name hyper*
You will be able to see that the Hyper-V role, Hyper-V PowerShell management shell, and the GUI management tools are successfully installed:
In this section, we will look at some of the fundamental concepts of Hyper-V management with the Hyper-V management shell. Once you get the Hyper-V role installed as per the steps illustrated in the previous section, a PowerShell module to manage your Hyper-V environment will also get installed. Now, perform the following steps:
Get-Command -Module Hyper-V
Hyper-V in Windows Server 2012 R2 ships with about 178 cmdlets. These cmdlets allow a Hyper-V administrator to handle very simple, basic tasks to advanced ones such as setting up a Hyper-V replica for virtual machine disaster recovery.
Get-Command -Module Hyper-V | Measure-Object
The Hyper-V PowerShell cmdlets follow a very simple approach and are very user friendly. The cmdlet name itself indirectly communicates with the Hyper-V administrator about its functionality. The following screenshot shows the output of the Get command:
For example, in the following screenshot, the Remove-VMSwitch cmdlet itself says that it's used to delete a previously created virtual machine switch:
Get-Help <Hyper-V cmdlet> -Full
The following screenshot is an example of the Get-Help cmdlet:
This new and improved feature in Windows Server 2012 R2 allows an administrator to share a virtual hard disk file (the .vhdx file format) between multiple virtual machines. These .vhdx files can be used as shared storage for a failover cluster created between virtual machines (also known as guest clustering). A shared virtual hard disk allows you to create data disks and witness disks using .vhdx files with some advantages:
This new feature allows you to save on storage costs and use the .vhdx files for guest clustering, enabling easier deployment rather than using virtual Fibre Channel or Internet Small Computer System Interface (iSCSI), which are complicated and require storage configuration changes such as zoning and Logic Unit Number (LUN) masking.
In Windows Server 2012 R2, virtual iSCSI disks (both shared and unshared virtual hard disk files) show up as virtual SAS disks when you add an iSCSI hard disk to a virtual machine. Shared virtual hard disks (.vhdx) files can be placed on Cluster Shared Volumes (CSV) or a Scale-Out File Server cluster
Let's look at the ways you can automate and manage your shared .vhdx guest clustering configuration using PowerShell. In the following example, we will demonstrate how you can create a two-node file server cluster using the shared VHDX feature. After that, let's set up a testing environment within which we can start learning these new features. The steps are as follows:
D:vhdbase_1.vhdx and D:vhdbase_2.vhdx are already existing VHDX files with sysprepped image of Windows Server 2012 R2.
The following code is used to create two virtual machines:
New-VM –Name "Fileserver_VM1" –MemoryStartupBytes 4GB –
NewVHDPath d:vhdbase_1.vhdx -NewVHDSizeBytes 50GB
New-VM –Name "Fileserver_VM2" –MemoryStartupBytes 4GB –
NewVHDPath d:vhdbase_2.vhdx -NewVHDSizeBytes 50GB
You need to enable PowerShell remoting on both the file servers and also have them joined to a domain.
The following is the code:
Install-WindowsFeature -computername Fileserver_VM1 File- Services,
FS-FileServer, Failover-Clustering Install-WindowsFeature -computername Fileserver_VM1 RSAT- Clustering –IncludeAllSubFeature Install-WindowsFeature -computername Fileserver_VM2 File- Services, FS-FileServer,
Failover-Clustering Install-WindowsFeature -computername Fileserver_VM2 RSAT- Clustering -IncludeAllSubFeature
New-Cluster -Name Cluster1 -Node FileServer_VM1, FileServer_VM2 -StaticAddress 10.0.0.59
-NoStorage – Verbose
You will need to choose a name and IP address that fits your organization.
New-VHD -Path c:ClusterStorageVolume1sharedvhdx_data.VHDX -Fixed - SizeBytes 10GB New-VHD -Path c:ClusterStorageVolume1sharedvhdx_quorum.VHDX -Fixed - SizeBytes 1GB
Add-VMHardDiskDrive –VMName Fileserver_VM1 -Path
c:ClusterStorageVolume1sharedvhdx_data.VHDX – ShareVirtualDisk Add-VMHardDiskDrive –VMName Fileserver_VM2 -Path
c:ClusterStorageVolume1sharedvhdx_data.VHDX – ShareVirtualDisk
Get-ClusterAvailableDisk | Add-ClusterDisk
Once we have executed the preceding set of steps, we will have a highly available file server infrastructure using shared VHD files.
With Windows Server 2012 R2, a newly added feature in Hyper-V allows the administrators to expand or shrink the size of a virtual hard disk attached to the SCSI controller while the virtual machines are still running. Hyper-V administrators can now perform maintenance operations on a live VHD and avoid any downtime by not temporarily shutting down the virtual machine for these maintenance activities.
Prior to Windows Server 2012 R2, to resize a VHD attached to the virtual machine, it had to be turned off leading to costly downtime. Using the GUI controls, the VHD resize can be done by using only the Edit Virtual Hard Disk wizard. Also, note that the VHDs that were previously expanded can be shrunk.
The Windows PowerShell way of doing a VHD resize is by using the Resize-VirtualDisk cmdlet. Let's look at the ways you can automate a VHD resize using PowerShell. In the next example, we will demonstrate how you can expand and shrink a virtual hard disk connected to a VM's SCSI controller. We will continue using the virtual machine that we created for our previous example. We have a pre-created VHD of 50 GB that is connected to the virtual machine's SCSI controller.
Let's resize the aforementioned virtual hard disk to 57 GB using the Resize-Virtualdisk cmdlet:
Resize-VirtualDisk -Name "scsidisk" -Size (57GB)
Next, if we open the VM settings and perform an inspect disk operation, we'll be able to see that the VHDX file size has become 57 GB:
Also, one can verify this when he or she logs into the VM, opens disk management, and extends the unused partition. You can see that the disk size has increased to 57 GB:
Let's resize the earlier mentioned VHD to 57 GB using the Resize-Virtualdisk cmdlet:
Get-Partition -DiskNumber 1 | Resize-Partition -Size 50GB
The following screenshot shows the partition:
Resize-VirtualDisk -Name "scsidisk" -Size (50GB)
Once the previous steps have been executed successfully, run a re-scan disk using disk management and you will see that the disk size is 50 GB:
In this article, we went through the basics of setting up a Hyper-V environment using PowerShell. We also explored the fundamental concepts of Hyper-V management with Hyper-V management shell.
Further resources on this subject: