Managing your customer's Hyper-V system state, virtual hard disks

Solution providers who want to maintain their customer’s Hyper-V environment will want to focus on managing system state and virtual hard disks (VHDs) by learning VHD management functions.

Solution provider’s takeaway: By ensuring your customer’s system state and virtual hard disks (VHDs) are properly managed, their environment becomes that much easier to handle. Check out VHD management functions such as creating multiple VHD sizes in this chapter excerpt.

Managing the virtual environment
Creating VMs and collecting configuration information about your environment are only the first steps. Managing your virtual environment is critical to realizing the benefits of virtualization. You should strive to use enterprise system management tools if at all possible. The Microsoft System Center family of products provides comprehensive tools to manage physical and virtual environments and is covered in Chapters 11 through 13.

System management can mean many different things, such as managing system configuration, provisioning, performance, security policies, hardware configuration, storage, and even the power state of a system. For our purposes, we’ll only discuss managing the system state (power state) for VMs and the management tasks for VHD files. The scripts shown earlier for provisioning VMs demonstrated how to alter the configuration of a VM (add/set resources). We’ll review additional VM configuration tasks later, in the “Maintaining Virtual Systems” section of this chapter.

Managing state
Chapter 9 included verbose examples of how to show and alter the system state of VMs. In this chapter, Figures 10.14 and 10.15 illustrate accessing and viewing the state of all VMs on a group of hosts to demonstrate information discovery. The system/power state of a VM is represented by an integer value, discussed and shown in Table 9.5 in Chapter 9. You may not need to care much about this table, because the Hyper-V library discussed in this chapter understands the friendly names of these states. The codes and decodes are contained in the $VMState global variable, found near the beginning of the library. Calling get-VMsummary (or get-VMstate, which is the same) returns the state information of all VMs on the local host (see Figure 10.26): 

get-vmsummary | ft Host, VMelementname,
CPUcount, EnabledState, Heartbeat, FQDN -auto

Figure 10.26
get-VMsummary output

To access the state of a single VM, use the get-VMsummary function and specify the friendly name of the VM (see Figure 10.27):

Get-VMSummary “New VM”

Figure 10.27
get-VMstate output for one VM

Note: In the beta version of the library, when you run get-VMsummary or get-VMstate, you’ll see an error if a paused VM is found. A fix for this and other enhancements are in the R2 Gold version of the library.

Changing the state of a VM is just as simple as retrieving it. Table 10.3 shows state management functions.

Table 10.3: Hyper-V Library State Management Functions

Function Name Description
get-VMstate or get-VMsummary Shows the state of VMs
start-VM Turns on/resumes VMs
stop-VM Turns off VMs
suspend-VM Suspends VMs (pause)
shutdown-VM Shuts down VMs via ICs
set-VMState Specifies the desired VM state


You use start-VM, stop-VM, suspend-VM, and shutdown-VM the same way. To call each of these functions, specify the name of the target VM:

Start-VM “New VM”

As with many functions in the library, you may also specify a remote physical host:

Start-VM “New VM” -server HyperNode1

It’s important to know the state a VM is in before you make a change request. For example, you can’t transition from a saved (suspended) state to a paused state. If a VM is already running, a request to start it will fail (see Figure 10.28).

Figure 10.28
Trying to start a running VM

You can change the state of all VMs on a host at once. For example, it could be valuable to put all VMs on a given host into a saved state for backup or while you perform maintenance on the physical system. Using an asterisk instead of the individual VM name with suspend-VM saves the state of all running or paused VMs (see Figure 10.29): 

suspend-vm *

Figure 10.29
Suspending all running VMs

Understanding the role of ICs is also important. They allow for a coordinated shutdown of a VM. You can facilitate an orderly power-down of a VM with installed ICs by using shutdown-VM (see Figure 10.30):

shutdown-VM “Windows XP”

Figure 10.30
Shutting down a VM with ICs

A VM without installed and running ICs can’t take advantage of shutdown integration. An unenlightened VM must be shut down from within the VM or via another means (perhaps simply turned off). Currently, only supported versions of Windows with installed ICs support integrated shutdown. 

You also can’t shut down VMs with ICs if the ICs aren’t available. For example, if VMs are in a saved (suspended) state, the ICs are unavailable (see Figure 10.32).

Figure 10.32
Failed shutdown -- all VMs aresuspended

Managing VHDs
The common container for storing a VM-accessible disk is the VHD file. You can create, change, test, and compact these disks while they aren’t in use by a VM. Table 10.4 lists common VHD management functions.

Table 10.4: Common storage management functions in the Hyper-V library

Function Name Description
new-VHD Creates a new VHD file
compress-VHD Compacts a VHD file
convert-VHD Changes to/from fixed or dynamic; creates new VHD
expand-VHD Increases the size of a VHD
get-VHDInfo Retrieves information about a VHD
merge-VHD Merges a child with a parent disk (untested at time of writing)
mount-VHD Mounts a VHD on a host for access
unmount-VHD Unmounts a VHD from a host
test-VHD Validates the integrity of a VHD file

You learned how to create a new VHD file using new-VHD as part of VM provisioning (see “Creating a Bare-Bones VM” earlier in this chapter). As you’re likely aware, you can create VHD files with either a static (fixed) or dynamic size; dynamic is the default. To create a new VHD file, you must supply a name and the desired size (see Figure 10.33) in the default directory for virtual hard disks. The following code creates a dynamic VHD named tiny with a size of 2 GB:

new-vhd tiny 2gb

Figure 10.33
Using the new-VHD function

You may notice that the call to new-VHD spawns a job that runs in the background. Some Hyper-V administrative tasks (like VHD creation) can take a long time. In the case of new-VHD, you can opt to have your script wait for the task to complete by using the -wait parameter: 

New-VHD “big” 20GB -fixed -wait

You can periodically check the status of the WMI job by using the included test-WMIjob function (see Figure 10.34):

new-vhd BIGDisk 200gb -Fixed
Test-WMIJob $Diskjob

Figure 10.34
Using the test- WMIjob function

get-VHDinfo can provide basic information about a VHD file, including the actual file size, the maximum internal size, the type, and whether it’s in use at a given time (see Figure 10.35):

get-vhdinfo ”Windows XP”

Just like new-VHD, get-VHDinfo defaults to the Hyper-V virtual hard disk directory. Monitoring storage used by VHDs can be a critical management function, particularly when you’re using dynamic VHDs. Unexpected VHD growth on a shared physical disk can lead to performance issues for all VMs homed there. You can create a tiny and useful VHD storage report like the one in Figure 10.36:

get-vhdinfo *| out-gridview

Figure 10.35
get-VHDinfo retrieves basic information.

Figure 10.36
VHD size report

If your VHDs reside somewhere other than in the default virtual hard disk directory, you can get a similar report by stringing together the path and additional formatting information:

dir “d:\VMs\*.vhd” | get-vhdinfo *| out-gridview

It’s useful to access the contents of a VHD file from the virtualization host as if it were a locally attached drive. Being able to add files to or remove files from a VHD without starting a VM can save you time and can facilitate offline maintenance. You can use mount-VHD and unmount-VHD to simplify the mounting of local VHD files:


The mount-VHD function doesn’t return much useful information in the PowerShell user interface, but you can see the VHD listed as a volume within Server Manager’s Disk Management (Figure 10.37).

Figure 10.37
Mounted VHD in Server Manager

This mounted VHD wasn’t formatted as part of the creation process (it hadn’t yet been exposed to an operating system installation process). After mounting, you can locate the VHD on the host either by using the Computer Management console or by using the diskpart command (see Figure 10.38). Once you identify the volume, you can then perform additional storage tasks from the host.

Figure 10.38
Using diskpart to see information about a mounted VHD

Note: The capability to mount a VHD within the parent partition is so useful that it was added to Server Manager in Windows Server 2008 R2. The functions dismount-VHD, compress-VHD, convert-VHD, expand-VHD, test-VHD, and merge-VHD all help you manage and alter your VHDs in ways consistent with their names. For more information about how to use them, you can review the examples contained in the Hyper-V library.

Maintaining virtual systems
Your virtual systems depend on you to keep them properly maintained. Automating required configuration changes, installing software updates, and sometimes rolling back changes are all tasks necessary to keep physical systems and VMs running efficiently.

Configuration changes
Business and technical pressures may require you to alter the configurations of existing VMs. Perhaps the applications on a given VM require more RAM or CPU resources. Maybe a physical NIC is experiencing a high network load, and some VM traffic must be offloaded to a new interface. Earlier in the chapter, we reviewed functions for automating configuration changes for VMs. Table 10.5 lists functions commonly used from the library to define or alter the configuration of a VM.

You can update the software components of a running VM the same way you do for a physical system. You can also patch VMs while they’re offline using the Offline Virtual Machine Servicing Tool, discussed briefly in Chapter 4, “Utilizing Virtualization Best Practices.” You can use mount-VHD to access the disk for offline VMs and prestage software for later installation.

Hyper-V can create point-in-time VM snapshots. You can use retained snapshot information to revert a VM to a known state in the past. Table 10.6 lists the snapshot-related functions found in the library.

Note: Snapshots aren't backups. We hate snapshots! They are very useful for development and testing purposes (to roll back changes), but they aren’t suitable for all situations and should never ever be used in production scenarios. 

Creating a snapshot is a straightforward process of calling new-VMsnaphot and specifying or passing the target VMs to be snapped (see Figure 10.39):

new-vmsnapshot “SCO UNIX 5.0.7” -wait

A more useful function for creating new snapshots may be update-VMsnapshot. This function creates a new snapshot and alters the displayed name to something of your choosing (see Figure 10.40 and Figure 10.41):

Update-VMSnapshot “SCO UNIX 5.0.7” “Before Scary Update”

Figure 10.39
New snapshot

Figure 10.40
New snapshot with specified name

Figure 10.41
New snapshot In Hyper-V GUI

Reverting and applying snapshots is also simplified with the library when you use select-VMsnapshot in conjunction with restore-VMsnapshot (see Figure 10.42):

select-vmsnapshot “SCO UNIX 5.0.7” | Restore-VMSnapshot

Figure 10.42
Restoring and applying snapshots

About the authors
John Kelbley is a senior technical product manager with Microsoft's Platform Tech Strategy Team. He is a periodic contributor to TechNet Magazine and his blog John Kelbley's real life enterprise interop and administration. 

Mike Sterling is a program manager in the Windows Server and Solutions Division at Microsoft, where he focuses on Hyper-V functionality in Windows Server 2008.

Allen Stewart is a principal program manager at Microsoft, focusing on Microsoft virtualization technologies. He also leads the Microsoft Virtualization Customer Advisory Council, which has a core set of customers who help drive next-generation virtualization scenarios.

Printed with permission from Wiley Publishing. Copyright 2009. Windows Server 2008 Hyper-V: Insiders Guide to Microsoft's Hypervisor by John Kelbley, Mike Sterling and Allen Stewart. For more information about this title and other similar books, please visit

Dig Deeper on Server virtualization technology and services