Book Image

Microsoft Hyper-V PowerShell Automation

By : Vinith Menon
Book Image

Microsoft Hyper-V PowerShell Automation

By: Vinith Menon

Overview of this book

Table of Contents (13 chapters)
Microsoft Hyper-V PowerShell Automation
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Live virtual hard disk resizing


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.

Expanding the virtual hard disk

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 57GB:

Resizing the virtual hard disk

Let's resize the earlier mentioned VHD to 57 GB using the Resize-Virtualdisk cmdlet:

  1. For this exercise, the primary requirement is to shrink the disk partition by logging in to the VM using disk management, as you can see in the following screenshot; we're shrinking the VHDX file by 7 GB:

  2. Next, click on Shrink. Once you complete this step, you will see that the unallocated space is 7 GB. You can also execute this step using the Resize-Partition PowerShell cmdlet:

    Get-Partition -DiskNumber 1 | Resize-Partition -Size 50GB
    

    The following screenshot shows the partition:

  3. Next, we will resize/shrink the VHD to 50 GB:

    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 50GB: