Professional Documents
Culture Documents
Hands On Guide Understanding Hyper V in Windows Server 2012 PDF
Hands On Guide Understanding Hyper V in Windows Server 2012 PDF
Understanding
Hyper-V
in Windows Server 2012
Brien Posey
Pete Zerger, Chris Henley
Understanding Hyper-V in Windows Server 2012
Contents
Chapter 1. An Introduction to Hyper-V 3.0 3
Chapter 2. Failover Clustering 30
Chapter 3. Migrations 84
Chapter 4. Managing Virtual Machine Failover 123
Chapter 5. PowerShell Management 142
Coming soon:
Chapter 6. Virtual Machine Management
Chapter 7. P2V conversions
Chapter 8. Replication
Chapter 9. Hyper-V Management
Chapter 10. Networking
Chapter 11. Automation
Chapter 12. Backup
2
Understanding Hyper-V in Windows Server 2012
Chapter 1
An Introduction
to Hyper-V 3.0
This chapter is designed to get you started quickly with
Hyper-V 3.0. It starts with a discussion of the hardware
requirements for Hyper-V 3.0 and then explains a basic
Hyper-V–deployment followed by an upgrade from
Hyper-V 2.0 to Hyper-V 3.0. The chapter concludes with
a demonstration of migrating virtual machines from
Hyper-V 2.0 to Hyper-V 3.0
3
Understanding Hyper-V in Windows Server 2012
Hardware requirements
According to Microsoft, the minimum hardware required for deploying
Windows Server 2012 includes:
• A 64-bit processor operating at 1.4 GHz or higher
• 512 MB of RAM
• 32 GB of hard disk space
• A DVD Drive
• A monitor and video card with a minimum display resolution of 800 x 600
• Keyboard and mouse (or other compatible pointing device)
• Internet access
In addition to the hardware requirements listed above, the CPU must support
hardware-level virtualization. It is worth noting that virtualization has been
disabled by default on many servers, so you may need to enable virtualization
through the system BIOS, as shown in Figure 1.1. If your server’s BIOS contains
a setting for Data Execution Prevention (DEP), you will need to enable that
setting as well.
4
Understanding Hyper-V in Windows Server 2012
Figure 1.1
5
Understanding Hyper-V in Windows Server 2012
3. Click Next.
4. Click Install Now (Figure 1.3).
Figure 1.3
Choose the edition of Windows Server 2012 that you want to install.
6
Understanding Hyper-V in Windows Server 2012
7
Understanding Hyper-V in Windows Server 2012
Figure 1.7
Select the drive or volume on which you want to install Windows Server 2012.
9. Setup will now begin the installation process (Figure 1.8). After
the Windows installation completes , you must work through a separate
process to install Hyper-V.
Figure 1.8
8
Understanding Hyper-V in Windows Server 2012
Figure 1.9
9
Understanding Hyper-V in Windows Server 2012
Figure 1.11
10
Understanding Hyper-V in Windows Server 2012
11. When the wizard displays the list of available features, click Next.
12. Click Next on the Hyper-V introductory screen.
13. Select the network adapters that you want to make available to your
virtual machines. Be sure to reserve at least one network adapter for host
management traffic (Figure 1.13).
Figure 1.13
11
Understanding Hyper-V in Windows Server 2012
Select Internet Protocol Version 4 (TCP/IPv4) and click the Properties button.
12
Understanding Hyper-V in Windows Server 2012
Figure 1.15
Provision your management NIC with a static IP address and click OK.
13
Understanding Hyper-V in Windows Server 2012
Figure 1.16
14
Understanding Hyper-V in Windows Server 2012
4. Enter a new name for the server and click OK (Figure 1.18).
Figure 1.18
You must restart the server before your changes will take effect.
15
Understanding Hyper-V in Windows Server 2012
16
Understanding Hyper-V in Windows Server 2012
Figure 1.20
You must shut down the virtual machines prior to beginning the upgrade.
2. With Windows Server 2008 R2 still running, insert your Windows Server
2012 installation media and run the Setup program.
3. When the Windows Server 2012 splash screen appears, click Install Now
(Figure 1.21).
Figure 1.21
17
Understanding Hyper-V in Windows Server 2012
Figure 1.22
Choose the edition of Windows Server 2012 that you want to install.
18
Understanding Hyper-V in Windows Server 2012
7. Click Next.
8. When prompted, accept the license agreement and click Next
(Figure 1.24).
Figure 1.24
19
Understanding Hyper-V in Windows Server 2012
Figure 1.26
20
Understanding Hyper-V in Windows Server 2012
Figure 1.27
21
Understanding Hyper-V in Windows Server 2012
Figure 1.29
You can monitor the export process through the Hyper-V Manager.
Right click-on your the server and select the Import Virtual Machine command from
the right‑click menu.
3. When the Import Virtual Machine wizard launches, click Next to bypass
the wizard’s Welcome screen.
4. Click the Browse button.
22
Understanding Hyper-V in Windows Server 2012
5. Navigate to the folder containing the virtual machine that you want to
import and click the Select Folder button.
6. Choose the virtual machine that you want to import (Figure 1.31).
Figure 1.31
23
Understanding Hyper-V in Windows Server 2012
Figure 1.32
24
Understanding Hyper-V in Windows Server 2012
Figure 1.33
25
Understanding Hyper-V in Windows Server 2012
Figure 1.34
26
Understanding Hyper-V in Windows Server 2012
process is complete, you can always install Windows Server 2012 onto your
existing cluster hardware, join those servers to the new cluster and then remove
the temporary, low-end servers from the cluster. Don’t worry too much about
exceeding the maximum cluster size during the migration process, because
Windows Server 2012 allows up to 63 cluster nodes.
As you probably know, Hyper-V 2.0 clusters depend on the use of Cluster Shared
Volumes. When you build the new Hyper-V 3.0 cluster, you must attach the
cluster nodes to the existing cluster shared volume (see Chapter 2 for details).
At this point, both the Hyper-V 2.0 cluster and the Hyper-V 3.0 cluster should be
tied into the same Cluster Shared Volume.
Once the Hyper-V 3.0 cluster is in place, verify that the Hyper-V 2.0 cluster is
still functional and that the virtual machines are still running (there is no reason
why they shouldn’t be, because the cluster has not been modified). Now, open
the Failover Cluster Manager on one of your Windows Server 2012 servers
and follow these steps:
1. Right-click on the cluster name and choose the More Actions | Migrate
Roles commands from the right-click menus (Figure 1.35).
Figure 1.35
27
Understanding Hyper-V in Windows Server 2012
Figure 1.36
28
Understanding Hyper-V in Windows Server 2012
6. The next screen provides an analysis of the migration. You can click
the View Report button to see the full Failover Cluster Pre-Migration
Report. It is worth noting that the report indicates that the cluster group
and the available storage cannot be migrated. This is perfectly normal
and acceptable.
7. Close the report and click Next.
8. Take a moment to verify the information displayed on the Confirmation
screen and click Next.
9. When the migration completes, click Finish.
Please keep in mind that Windows Server 2012 does not perform a live migration
of the virtual machines. When the migration completes, the virtual machines are
still running on the Hyper-V 2.0 cluster, so there are a couple of things that you
need to do to complete the process.
First, shut down the virtual machines on the Hyper-V 2.0 server. Second,
disconnect the Hyper-V 2.0 cluster nodes from the shared storage. If you fail to
do this, virtual machine corruption can occur. Finally, start the virtual machines
on your new cluster. Once the virtual machines are up and running, it is safe to
destroy your Hyper-V 2.0 cluster and re-provision the host servers for use in
the new cluster.
29
Understanding Hyper-V in Windows Server 2012
Chapter 2
Failover Clustering
Chapter 2 is designed to familiarize you with your options
for Hyper-V clustering. Hyper-V 3.0 allows you to build
a cluster with or without shared storage, and this chapter
walks you through both methods. In addition, you will
learn how to provision storage using a new Windows
Server 2012 feature called Windows Storage Spaces.
30
Understanding Hyper-V in Windows Server 2012
Maximum number of
virtual machines in 1000 4000
a cluster
Maximum number of
16 63
hosts per cluster
Another major improvement that Microsoft has made with regard to failover
clustering is that they have changed the storage requirements. Prior to Hyper-V
3.0, a Hyper-V cluster depended on the use of a Cluster Shared Volume.
A Cluster Shared Volume is a shared storage volume that physically contains all
of the virtual machine components. Because the storage is shared, it is physically
accessible to all of the cluster nodes. You can see an example of shared storage
architecture in Figure 2.1.
31
Understanding Hyper-V in Windows Server 2012
Figure 2.1
Previous versions of Hyper-V required failover clusters to make use of shared storage.
In Hyper-V 3.0, shared storage is no longer required for failover clustering.
A failover cluster can be built without the need for a Cluster Shared Volume.
In those types of clusters, the virtual machines can reside on local direct
attached storage or they can even reside on certain types of file servers.
The fact that you can create failover clusters without a Cluster Shared Volume is
good news for smaller organizations because the cost of shared storage often
puts clustered virtualization hosts financially out of reach. Even so, Microsoft
recommends that Hyper-V 3.0 failover clusters make use of failover clusters
whenever possible . This chapter will demonstrate the process of building
failover clusters both with and without shared storage.
Cluster planning
Before you begin constructing a failover cluster you will need to do some
planning. Obviously you will need to decide whether the cluster will
use shared storage, you’ll need to take into account a number of other
considerations, including:
• Node names — Just as the cluster requires a cluster name, each cluster
node requires a unique computer name. Although Windows Server 2012
assigns computer names automatically, it is highly recommended that you
assign computer names that are more descriptive. Doing so makes it easier
to figure out which node you are working on. The cluster nodes used in
the examples in this chapter will be named Lab1, Lab2 and Lab3.
32
Understanding Hyper-V in Windows Server 2012
• Number of nodes — You also need to decide how many nodes to use
in your cluster. In this chapter you will be building a Majority Node Set
Cluster. It is best to use an odd number of cluster nodes whenever possible
because a Majority Node Set Cluster requires half of the nodes plus one
to remain online during a failure in order for the cluster to retain quorum.
It is technically possible to build a failover cluster out of two cluster nodes
(plus a file share witness), but it's recommended that you always use at
least three cluster nodes. Your cluster can contain as many as 63 nodes.
Of course, most clusters use far fewer than 63 nodes, and you always have
the option of adding additional cluster nodes later until the maximum
number of nodes has been reached.
33
Understanding Hyper-V in Windows Server 2012
You must install the Failover Clustering feature onto each of the cluster nodes.
6. When the wizard displays the list of server roles, click Next.
7. Select Failover Clustering from the list of features (Figure 2.3). If the wizard
prompts you to install additional features, click the Add Features button.
34
Understanding Hyper-V in Windows Server 2012
Figure 2.3
35
Understanding Hyper-V in Windows Server 2012
Figure 2.4
You should specify all of the nodes that you want to include in the cluster.
36
Understanding Hyper-V in Windows Server 2012
6. Click Next.
7. You should now see a message indicating that the cluster has not yet been
validated. Choose the option to run the validation tests and click Next
(Figure 2.6). Windows will launch the Validate a Cluster Wizard.
Figure 2.6
You must validate the cluster before you can create it.
8. Click Next to bypass the wizard’s Welcome screen.
9. Choose the Run All Tests (Recommended) option and click Next.
10. Click Next to begin the validation tests.
11. When the validation tests complete, take a moment to view the report
and review any errors or warnings (Figure 2.7). Click Finish.
37
Understanding Hyper-V in Windows Server 2012
Figure 2.7
Review the validation report and check for any errors or warnings.
12. When prompted, enter a name for the cluster and assign an IP address
to the cluster (Figure 2.8). The name and IP address that you use should be
unique and will be used to identify the cluster as a whole.
Figure 2.8
You must assign a unique name and static IP address to the cluster.
38
Understanding Hyper-V in Windows Server 2012
Choose the server on which you want to deploy the Hyper-V role.
39
Understanding Hyper-V in Windows Server 2012
6. Select the Hyper-V option from the list of roles (Figure 2.10).
Figure 2.10
40
Understanding Hyper-V in Windows Server 2012
Figure 2.11
Select the network adapters that you want to connect to the Hyper-V virtual switch.
12. Click Next.
13. Select the checkbox for Allow this Server to Send and Receive Live
Migrations of Virtual Machines (Figure 2.12).
Figure 2.12
41
Understanding Hyper-V in Windows Server 2012
Navigate through the Failover Cluster Manager to Failover Cluster Manager | <your cluster
name> | Roles.
4. Click the Configure Role link, found in the Actions pane.
5. Windows will launch the High Availability Wizard. Click Next to bypass
the wizard’s Welcome screen.
6. The next screen lists a variety of server roles. Select the Virtual Machine
role from the list and click Next (Figure 2.14).
42
Understanding Hyper-V in Windows Server 2012
Figure 2.14
Select the virtual machines that you wish to make fault tolerant and click Next.
43
Understanding Hyper-V in Windows Server 2012
8. Verify the information displayed on the confirmation screen and click Next.
9. When the process completes you should see a message confirming that
high availability was successfully configured. Click the View Report button
to examine the report. Click Finish.
After the virtual machines become fault tolerant, you should see them listed
in the Failover Cluster Manager’s Roles container (Figure 2.16). Likewise,
the Hyper-V Manager should list the virtual machine as being clustered
(Figure 2.17).
Figure 2.16
The virtual machine should now be listed in the Failover Cluster Manager.
Figure 2.17
The Hyper-V Manager should list the virtual machine as being clustered.
44
Understanding Hyper-V in Windows Server 2012
45
Understanding Hyper-V in Windows Server 2012
Figure 2.18
46
Understanding Hyper-V in Windows Server 2012
Figure 2.19
Specify the names of the servers that will make up the cluster.
6. Click Next.
7. You should now see a message indicating that the cluster has not yet
been validated. Choose the option to run the validation tests and click
Next (Figure 2.20). This will cause Windows to launch the Validate
a Cluster Wizard.
Figure 2.20
47
Understanding Hyper-V in Windows Server 2012
48
Understanding Hyper-V in Windows Server 2012
Figure 2.22
49
Understanding Hyper-V in Windows Server 2012
Shared storage
In the past if you wanted to use failover clustering with Hyper-V, you had to
make use of shared storage in the form of a Cluster Shared Volume. Although
Hyper-V 3.0 does not require a Cluster Shared Volume for clustering, shared
storage is still the preferred method for building a failover cluster.
As has always been the case, you can create a Cluster Shared Volume on
virtually any iSCSI or Fibre Channel accessible storage device. This can include
a SAN, a physical NAS appliance or even a server that is configured to act as
a shared storage device. The actual method you use to provision the shared
storage varies depending on the physical hardware you are using.
For the sake of demonstration, this example will use a Windows Server to
host the shared storage and will connect to this server using iSCSI. This server
contains four physical hard disks. The first hard disk is a 250-GB disk that
contains the Windows Server 2012 operating system files. The remaining
three hard disks are each 500 GB in size and will be configured to act as
a RAID 5 array. Keep in mind that this configuration is only being used for
demonstration purposes. Real-world organizations typically use larger arrays
and those arrays are often configured as RAID 6 or as RAID 10, because
such RAID configurations are resistant to the failures of multiple drives.
For this demonstration, however, RAID 5 will work within the limitations of
the lab hardware.
50
Understanding Hyper-V in Windows Server 2012
There are two ways to create the necessary storage array. The legacy method,
which will be discussed first, is the one you will have to use this method if
your shared storage resides on an older version of Windows Server. The other
method is the preferred method of storage provisioning in Windows Server
2012, which will be discussed second.
The Disk Management Console lists all of the server’s physical disks.
51
Understanding Hyper-V in Windows Server 2012
2. Right-click on one of the empty disks and choose the New RAID 5 volume
command from the right-click menu (Figure 2.26). Windows will launch
the New RAID 5 Volume Wizard.
Figure 2.26
52
Understanding Hyper-V in Windows Server 2012
5. Click Next.
6. Select the drive letter that you wish to assign to the volume that you are
creating (Figure 2.28).
Figure 2.28
53
Understanding Hyper-V in Windows Server 2012
9. Click Next.
10. Click Finish.
11. A warning message tells you that the operation will convert the basic disks
to dynamic disks (Figure 2.30). Click Yes to continue.
Figure 2.30
54
Understanding Hyper-V in Windows Server 2012
55
Understanding Hyper-V in Windows Server 2012
Figure 2.33
The Primordial pool is created by default, but you will have to manually create all other
storage pools.
5. Choose the New Storage Pool option from the Task list.
6. When the New Storage Pool Wizard begins, click Next to bypass
the wizard’s Welcome screen.
7. Enter a name and an optional description of the storage pool that you
are creating.
8. Click Next.
9. Choose the disks that you wish to include within the storage pool
(Figure 2.34).
Figure 2.34
Choose the disks that you wish to include in the storage pool.
56
Understanding Hyper-V in Windows Server 2012
10. Use the Allocation drop down for each disk to control whether the disk
should be allocated as a data store, a hot spare, or a manual allocation
(Figure 2.35).
Figure 2.35
57
Understanding Hyper-V in Windows Server 2012
You should now see the storage pool listed in the console along with
the physical disks that make up the storage pool (Figure 2.37).
Figure 2.37
58
Understanding Hyper-V in Windows Server 2012
Figure 2.38
Choose the method Windows will use to protect your virtual disk.
59
Understanding Hyper-V in Windows Server 2012
9. Click Next.
10. Choose whether you want to thinly provision the Virtual Hard Disk or
create a disk of a fixed size (Figure 2.40). Fixed size provisioning delivers
better performance, but thin provisioning is more flexible and makes more
efficient use of storage space.
Figure 2.40
60
Understanding Hyper-V in Windows Server 2012
Figure 2.41
61
Understanding Hyper-V in Windows Server 2012
Figure 2.42
62
Understanding Hyper-V in Windows Server 2012
Figure 2.43
63
Understanding Hyper-V in Windows Server 2012
Figure 2.44
Make sure that the server that will host the iSCSI target is selected.
64
Understanding Hyper-V in Windows Server 2012
7. Click Next.
8. On the Select Server Roles screen, verify that the File and Storage
Services role is installed. This role should be installed by default.
9. Expand the File and Storage Services container.
10. Expand the File and iSCSI Services container (Figure 2.46).
11. Select the iSCSI Target Server option and the iSCSI Target Storage
Provider option. If you are prompted to add additional features to support
those services, click the Add Features button.
Figure 2.46
Select the iSCSI Target Software and the iSCSI Target Storage Provider components.
12. Click Next.
13. Click Next on the Select Features screen (it is not necessary to deploy
any additional features at this time).
14. When you reach the confirmation screen, click Install (Figure 2.47).
65
Understanding Hyper-V in Windows Server 2012
Figure 2.47
Click the To Create an iSCSI Virtual Disk, Start the New iSCSI Virtual Disk Wizard link.
66
Understanding Hyper-V in Windows Server 2012
5. When the New iSCSI Virtual Disk Wizard begins, select your storage pool
and the volume that you just created (Figure 2.49).
Figure 2.49
67
Understanding Hyper-V in Windows Server 2012
Figure 2.50
68
Understanding Hyper-V in Windows Server 2012
Figure 2.51
70
Understanding Hyper-V in Windows Server 2012
7. You should now be returned to the main initiator screen with the Targets
tab selected (Figure 2.54).
Figure 2.54
71
Understanding Hyper-V in Windows Server 2012
Figure 2.55
You must add the target to the list of favorite targets and enable multi-path.
10. Select the Enable Multi-Path check box. This is a critical step because
you won’t be able to create a Cluster Shared Volume unless this option
is selected.
11. Click the Advanced button.
12. When the Advanced Settings dialog box launches, select the Enable
CHAP Log On check box (Figure 2.56).
Figure 2.56
You must choose the Enable CHAP Log On option and enter your CHAP credentials.
72
Understanding Hyper-V in Windows Server 2012
13. Enter the name that you specified earlier. You should enter the CHAP
password into the Target Secret field.
14. Click OK.
15. Click OK.
16. When you are returned to the Targets tab, the iSCSI Target should be
listed as Connected.
17. Click OK to close the iSCSI Initiator Properties sheet.
18. Repeat this procedure on the remaining cluster nodes so that each cluster
node has iSCSI connectivity to the target.
73
Understanding Hyper-V in Windows Server 2012
Figure 2.58
Select the type of partition that you wish to use for your Cluster Shared Volume and click OK.
6. Right-click on the unallocated space and create a volume. Do not assign
a drive letter to the volume.
7. Close the Disk Management Console.
8. Open the Server Manager.
9. Choose the Failover Cluster Manager option from the Tools menu.
10. When the Failover Cluster Manager opens, navigate through the console
tree to Failover Cluster Manager | <your cluster name> | Storage | Disks
(Figure 2.59).
Figure 2.59
Navigate through the Failover Cluster Manager to Failover Cluster Manager | <your cluster
name> | Storage | Disks.
11. Click the Add Disk link, found in the Actions pane.
12. The Failover Cluster Manager should automatically recognize your iSCSI
Target as a disk that can be added to the cluster (Figure 2.60). Verify that
the disk is selected and click OK.
74
Understanding Hyper-V in Windows Server 2012
Figure 2.60
The Cluster Shared Volume should be listed within the Failover Cluster Manager.
14. With the cluster disk selected, click Add to Cluster Shared Volumes.
75
Understanding Hyper-V in Windows Server 2012
76
Understanding Hyper-V in Windows Server 2012
Figure 2.63
77
Understanding Hyper-V in Windows Server 2012
As you may recall, you created a volume on the iSCSI target, but did not
assign a drive letter to it. You can access the Cluster Shared Volume at C:\
ClusterStorage\<your volume name>. Anything that you save to this path will
reside on the Cluster Shared Volume (the iSCSI target) rather than on the local
C: drive.
Simply storing the virtual machines on the Cluster Shared Volume is not
enough (Figure 2.64). As you can see in the figure, three virtual machines have
been created, and each of these virtual machines resides on the Cluster Shared
Volume. If you look at the lower middle pane in the next figure, however, you
will notice that the Hyper-V Manager indicates that the virtual machine is not
clustered (Figure 2.65).
Figure 2.64
78
Understanding Hyper-V in Windows Server 2012
Figure 2.65
Even with the virtual machines residing on the Cluster Shared Volume, the virtual machines
are not considered to be fault tolerant.
To make the virtual machine fault tolerant you must designate it as such
through the Failover Cluster Manager. To do so, follow these steps:
1. Open the Server Manager.
2. Choose the Failover Cluster Manager from the list of tools.
3. When the Failover Cluster Manager opens, navigate through the
console tree to Failover Cluster Manager | <your cluster name> | Roles
(Figure 2.66).
Figure 2.66
Navigate through the Failover Cluster Manager to Failover Cluster Manager | <your cluster
name> | Roles.
79
Understanding Hyper-V in Windows Server 2012
80
Understanding Hyper-V in Windows Server 2012
Figure 2.68
Select the virtual machines that you wish to make fault tolerant.
8. Verify the information displayed on the confirmation screen and click
Next.
9. When the process completes you should see a message confirming that
high availability was successfully configured (Figure 2.69). Click the View
Report button to examine the report. Click Finish.
Figure 2.69
81
Understanding Hyper-V in Windows Server 2012
After the virtual machines become fault tolerant, you should see them listed
in the Failover Cluster Manager’s Roles container (Figure 2.70). Likewise,
the Hyper-V Manager should list the virtual machine as being clustered
(Figure 2.71).
Figure 2.70
82
Understanding Hyper-V in Windows Server 2012
Figure 2.71
83
Understanding Hyper-V in Windows Server 2012
Chapter 3
Migrations
In the previous chapter, you spent a lot of time building
failover clusters and making virtual machines fault tolerant.
However, building the cluster is really just the beginning.
It is equally important to know how to verify that
your failover cluster is working properly. As a Hyper-V
administrator, you will also need to know how to move
virtual machines around both within and outside of
the cluster. This chapter discusses all of these tasks.
84
Understanding Hyper-V in Windows Server 2012
85
Understanding Hyper-V in Windows Server 2012
Figure 3.1
You can select the destination node manually, or you can let Hyper-V pick the best
node automatically.
5. Choose the cluster node to which you want to live migrate the virtual
machine (Figure 3.2).
Figure 3.2
Select the cluster node to which you want to live migrate the virtual machine.
6. Click OK.
7. You can monitor the live migration process through the Failover Cluster
Manager (Figure 3.3).
86
Understanding Hyper-V in Windows Server 2012
Figure 3.3
The Failover Cluster Manager displays the progress of the live migration.
8. When the live migration completes, the Failover Cluster Manager should
report the virtual machine as running on the cluster node that you have
selected (as shown in the Owner Node column) (Figure 3.4).
Figure 3.4
The Owner Node column confirms that the virtual machine has been moved to a different
cluster node.
87
Understanding Hyper-V in Windows Server 2012
Figure 3.5
Right-click on the listing for the host server and select the Hyper-V Settings command from
the right-click menu.
3. When the Hyper-V Settings dialog box launches, select the Live
Migrations option (Figure 3.6).
Figure 3.6
88
Understanding Hyper-V in Windows Server 2012
4. Select the Enable Incoming and Outgoing Live Migrations check box
(Figure 3.7).
Figure 3.7
89
Understanding Hyper-V in Windows Server 2012
Figure 3.8
You will receive an error message if the CPUs on the source and destination hosts are
too dissimilar.
The solution to this problem is to enable CPU compatibility. You can accomplish
this by following these steps:
1. Open the Hyper-V Manager.
2. Shut down the virtual machine.
3. Right-click on the virtual machine and choose the Settings command from
the right-click menu.
4. Expand the Processor container in the resulting dialog box.
5. Select the Compatibility container.
6. Select the checkbox for Migrate to a Physical Computer with a Different
Processor Version (Figure 3.9).
7. Click OK.
8. Restart the virtual machine.
9. Attempt the live migration.
90
Understanding Hyper-V in Windows Server 2012
Figure 3.9
You can overcome some live migration issues by enabling the processor compatibility feature.
It is worth noting that enabling the CPU compatibility feature does not allow
you to move virtual machines between hosts with different CPU architectures
(e.g., moving from a host with an Intel processor to a host with an AMD
processor). Furthermore, enabling CPU compatibility might impact the virtual
machine’s performance because the feature works by disabling the virtual
machine’s access to some of the more advanced CPU features such as
Misaligned SSE or POPCNT.
91
Understanding Hyper-V in Windows Server 2012
92
Understanding Hyper-V in Windows Server 2012
Figure 3.10
Right-click on the virtual machine and select the Remove command from
the right‑click menu.
6. When you see the prompt asking if you want to remove the virtual
machine, click Yes (Figure 3.11).
Figure 3.11
Confirm that you want to remove the virtual machine from the cluster.
7. Close the Failover Cluster Manager.
8. Open the Hyper-V Manager.
9. Verify that the virtual machine is still running, and that it is not clustered
(Figure 3.12).
93
Understanding Hyper-V in Windows Server 2012
Figure 3.12
Make sure that the VM is still running and that it is no longer clustered.
10. Right-click on the virtual machine and choose the Move command from
the right-click menu.
11. When the Move Wizard starts, click Next to bypass the wizard’s
Welcome screen.
12. Choose the Move the Virtual Machine option and click Next (Figure 3.13).
94
Understanding Hyper-V in Windows Server 2012
Figure 3.13
95
Understanding Hyper-V in Windows Server 2012
Choose the option to move the virtual machine by selecting where to move each item.
16. Click Next.
17. Choose the option for moving the virtual machine’s components. It is
usually acceptable to choose the option to Move the Virtual Machine’s
Data Automatically (Figure 3.16). This option places the virtual machine
components in the same locations on the destination host as the locations
in which they resided on the source host.
96
Understanding Hyper-V in Windows Server 2012
Figure 3.16
97
Understanding Hyper-V in Windows Server 2012
3. When the Move Wizard opens, click Next to bypass the Welcome screen.
4. On the following screen, select the Move the Virtual Machine option and
click Next (Figure 3.17).
Figure 3.17
6. Click Next.
7. Choose the Move the Virtual Machine’s Data to a Single Location option
(Figure 3.19).
Figure 3.19
99
Understanding Hyper-V in Windows Server 2012
Figure 3.20
13. Verify that the destination folder has sufficient free disk space. The amount
of disk space required is listed in the Source Location portion of
the wizard’s current screen.
14. Click Next.
15. If the cluster node does not contain a virtual switch with a name matching
the virtual switch that is currently in use, you will be asked to specify
a virtual switch (Figure 3.22). Make your selection and click Next.
Figure 3.22
Specify the virtual switch that the virtual machine will use after the migration.
16. Verify the summary information and click Finish.
17. When the migration process completes, go to the destination cluster node
and verify that the virtual machine is running.
18. Switch to the destination cluster node and verify that the virtual machine is
present and running (Figure 3.23).
101
Understanding Hyper-V in Windows Server 2012
Figure 3.23
102
Understanding Hyper-V in Windows Server 2012
Figure 3.24
Select the Virtual Machine option from the list of high availability roles.
25. Select the recently migrated virtual machine from the list of virtual
machines and click Next (Figure 3.25).
Figure 3.25
Select the virtual machine that you want to make highly available.
103
Understanding Hyper-V in Windows Server 2012
26. Verify that the correct virtual machine is listed on the summary screen
and click Next.
27. Verify that the virtual machine was made highly available (Figure 3.26).
28. Click Finish.
Figure 3.26
You can use the report to verify the success of the operation.
104
Understanding Hyper-V in Windows Server 2012
Figure 3.27
Select the virtual machine that you want to move to another cluster.
5. Right-click on the virtual machine and select the Remove command from
the right-click menu.
6. When you see the prompt asking if you want to remove the virtual
machine, click Yes (Figure 3.28).
Figure 3.28
Make sure that the VM is still running and that it has been removed from the cluster.
105
Understanding Hyper-V in Windows Server 2012
10. Right-click on the virtual machine and choose the Move command from
the right-click menu.
11. When the Move Wizard launches, click Next to bypass the wizard’s
Welcome screen.
12. Choose the option to Move the Virtual Machine (Figure 3.30)
and click Next.
Figure 3.30
106
Understanding Hyper-V in Windows Server 2012
Figure 3.31
107
Understanding Hyper-V in Windows Server 2012
Figure 3.32
Enter the path where the virtual machine should be stored on the destination cluster.
22. Verify that the destination folder has sufficient free disk space. The amount
of disk space required is listed in the Source Location portion of
the wizard’s current screen.
23. Click Next.
24. If the cluster node does not contain a virtual switch with a name matching
the virtual switch that is currently in use, you will be asked to specify
a virtual switch. Make your selection and click Next.
25. Verify the summary information and click Finish.
26. When the migration process completes, go to the destination cluster node
and verify that the virtual machine is running.
27. Switch to the destination cluster node and verify that the virtual machine is
present and running.
28. Open the Server Manager.
29. Choose the Failover Cluster Manager option from the Tools menu.
30. Navigate through the Failover Cluster Manager to Failover Cluster
Manager | <your cluster> | Roles.
31. Click on the Configure Roles link, found in the Actions pane.
32. When the High Availability Wizard launches, click Next to bypass
the Welcome screen.
33. Select the Virtual Machine option and click Next (Figure 3.33).
108
Understanding Hyper-V in Windows Server 2012
Figure 3.33
Select the Virtual Machine option from the list of roles that can be configured for
high availability.
34. Select the recently migrated virtual machine from the list of virtual
machines and click Next.
35. Verify that the correct virtual machine is listed on the summary screen
and click Next.
36. Verify that the virtual machine was made highly available.
37. Click Finish.
38. The virtual machine should now be displayed as a clustered resource
within the Failover Cluster Manager (Figure 3.34).
Figure 3.34
The virtual machine should be displayed within the Failover Cluster Manager.
109
Understanding Hyper-V in Windows Server 2012
Right-click on a virtual machine and choose the Move | Virtual Machine Storage commands
from the right-click menu.
110
Understanding Hyper-V in Windows Server 2012
Right-click in the Cluster Storage section and choose the Add Share option.
6. When the Add a Network Share dialog box launches, enter the UNC share
name (\\servername\sharename) for the destination storage and click OK
(Figure 3.37).
Figure 3.37
111
Understanding Hyper-V in Windows Server 2012
Figure 3.38
You should see the virtual machine components that are about to be migrated.
112
Understanding Hyper-V in Windows Server 2012
The Failover Cluster Manager should confirm that virtual machine migration has begun.
Right-click on a VM and choose the Move command from the right-click menu.
113
Understanding Hyper-V in Windows Server 2012
3. When the Move wizard launches, click Next to bypass the wizard’s
Welcome screen.
4. Choose the option to move the virtual machine’s storage, and click Next
(Figure 3.42).
Figure 3.42
114
Understanding Hyper-V in Windows Server 2012
Figure 3.43
Choose the option to move all of the virtual machine’s data to a single location.
6. Click Next.
7. Specify the folder to which you want to move the virtual machine
(Figure 3.44). Pay attention to the Current Location section within
the wizard’s current screen. This section tells you how much physical disk
space the virtual machine is currently consuming. You must verify that
there is sufficient disk space in the new location.
115
Understanding Hyper-V in Windows Server 2012
Figure 3.44
Specify the path to which you want to move the virtual machine.
8. Click Next.
9. Verify the information shown on the Summary screen.
10. Click Finish.
116
Understanding Hyper-V in Windows Server 2012
Figure 3.45
Choose the option to move the virtual machine’s data to different locations.
6. Click Next.
7. Choose the virtual machine components that you want to move
(Figure 3.46) and click Next.
Figure 3.46
117
Understanding Hyper-V in Windows Server 2012
8. If you have chosen to move the Virtual Hard Disk, you will be prompted to
enter a new location for it (Figure 3.47). Enter the path to the Virtual Hard
Disk’s new location and click Next.
Figure 3.47
118
Understanding Hyper-V in Windows Server 2012
Figure 3.48
119
Understanding Hyper-V in Windows Server 2012
Figure 3.49
120
Understanding Hyper-V in Windows Server 2012
Figure 3.50
Choose the option to move only the virtual machine’s Virtual Hard Disks.
6. Click Next.
7. Select the Virtual Hard Disks that you want to move (Figure 3.51)
and click Next.
Figure 3.51
121
Understanding Hyper-V in Windows Server 2012
8. Specify the location to which you wish to move the Virtual Hard Disks
and click Next (Figure 3.52).
9. Verify the information on the summary screen and click Finish.
Figure 3.52
Enter the path where the virtual hard disk should be stored.
122
Understanding Hyper-V in Windows Server 2012
Chapter 4
Managing Virtual
Machine Failover
The previous chapters show you how to build clustered
Hyper-V deployments and how to migrate virtual
machines within those environments. However, facilitating
live migrations is only part of a Hyper-V cluster’s job.
A cluster also has to keep virtual machines running, even
if the server on which a virtual machine is hosted were
to fail.
This chapter discusses various aspects of failover planning.
In this chapter you will learn about failover testing,
anti‑affinity rules and virtual machine prioritization.
123
Understanding Hyper-V in Windows Server 2012
Failover testing
As you have no doubt seen in the previous chapters, there is a lot of work
that goes into building a Hyper-V cluster. However, none of the hard work
and expense means very much if your cluster fails to keep your virtual
machines running when a hardware failure occurs. Of course you probably
don’t want to wait for a disaster to happen in order to find out if your cluster
is working or not. It’s better to test the cluster ahead of time.
Failover cluster testing can be tricky in a production environment because
you don’t want to jeopardize your production virtual machines in the testing
process. And it’s never a good idea to test your clustering solution by walking
through the datacenter and randomly yanking power cords out of clustered
hosts. Even though this type of testing makes for an amusing story, it is hardly
a recommended method.
Microsoft gives you a few different methods of testing failover within your
cluster. However, all of these tests tend to focus on the cluster at a high level.
Microsoft does not provide a method for testing the failover of an individual
virtual machine.
Validation testing
The first type of testing that you can perform is a cluster validation test. If this
sounds familiar, it is probably because you performed a validation test when
you were preparing to build the failover cluster.
Even though you may think of a validation test as a tool to use in preparation
for building a cluster, you can perform these types of tests against a cluster
that has already been established. The reason why this might be helpful
is because cluster configurations tend to change over time and validation
testing will help you spot any configuration problems that might exist within
your cluster.
You can perform validation testing either through the Failover Cluster Manager
or through PowerShell. To perform validation testing using the Failover Cluster
Manager, follow these steps:
1. Open the Server Manager.
124
Understanding Hyper-V in Windows Server 2012
2. Select the Failover Cluster Manager command from the Tools menu.
3. When the Failover Cluster Manager opens, select the listing for your
cluster within the console tree.
4. Click on the Validate Cluster link, found in the Actions pane (Figure 4.1) .
Figure 4.1
Choose the option to run only the tests that you select.
7. Click Next.
8. On the following screen, select the tests that you want to run (Figure 4.3).
You can select entire categories of tests (such as Inventory, Network, or
System Configuration), or you can expand a category and select individual
tests within that category.
125
Understanding Hyper-V in Windows Server 2012
Figure 4.3
Do not select the cluster disk if your Cluster Shared Volume is in use!
126
Understanding Hyper-V in Windows Server 2012
127
Understanding Hyper-V in Windows Server 2012
Figure 4.6
The Test-Cluster cmdlet can be used for comprehensive cluster validation testing.
When you first enter the Test-Cluster cmdlet, PowerShell may appear to
lock up. After several seconds you should see the tests begin, as shown in
Figure 4.6.
When the validation testing completes, you will see a message that refers to
a validation report (Figure 4.7). The validation report file is located in the C:\
Windows\Cluster\Reports folder. You can open the validation reports using
Internet Explorer, as shown in Figure 4.8.
Figure 4.7
128
Understanding Hyper-V in Windows Server 2012
Keep in mind that the validation report shown in the figure above was based
on a comprehensive set of cluster tests. It is possible to exercise a high degree
of granular control over the testing process. Using PowerShell, you can run
specific validation tests, exclude certain validation tests and direct the testing
process to specific cluster nodes.
The trick to using PowerShell to control the validation tests is to retrieve a list
of the test names. Once you have a list of the test names (as referenced by
PowerShell) you can begin to include or exclude specific tests. To get a list of all
of the test names, enter the following command (Figure 4.9):
Test-Cluster – List
Figure 4.9
129
Understanding Hyper-V in Windows Server 2012
You can also use PowerShell to specify the cluster nodes to include in
the validation tests. For example, suppose that you want to run the validation
tests against nodes named Lab1, Lab2, and Lab3 (Figure 4.11). To do so, you
can use the following command:
Test-Cluster –Node Lab1,Lab2,Lab3
Figure 4.11
130
Understanding Hyper-V in Windows Server 2012
Figure 4.12
131
Understanding Hyper-V in Windows Server 2012
For the most part, cluster resource failure testing is limited to the items on
the list of cluster resources (Figure 4.12). The big exception, however, is that you
can also test cluster disks. Suppose for example that you want to test the failure
of a disk named Cluster Disk 1 (Figure 4.12). You can do so by entering the
following command:
Test-ClusterResourceFailure –Name “Cluster Disk 1”
Figure 4.15
You can use the Test-ClusterResourceFailure cmdlet to test cluster disk failures.
Failover testing
The third type of testing you can do with regard to your cluster is failover
testing. Failover testing simulates the failure of an active cluster node and tests
the cluster’s ability to failover to another node in the cluster.
Unfortunately, Microsoft does not provide a mechanism for testing the failover
of an individual virtual machine. Instead, failover testing occurs at the cluster
level. To perform a failover test, follow these steps:
1. Open the Server Manager.
2. Choose the Failover Cluster Manager command from the Tools menu.
3. When the Failover Cluster Manager appears, navigate through the console
tree to Failover Cluster Manager | <your cluster> (Figure 4.16) .
132
Understanding Hyper-V in Windows Server 2012
Figure 4.16
133
Understanding Hyper-V in Windows Server 2012
You can watch the failover occur in the Cluster Core Resources pane.
Anti-affinity rules
In multi-tenant environments, it is sometimes necessary to configure certain
virtual machines so that they never reside on the same host server. For
example, if your organization were hosting virtual machines for both Coke
and Pepsi, you would not want the two companies’ virtual machines to reside
on a common host server.
It is relatively easy to keep virtual machines separate from one another when
you initially create the virtual machines. However, virtual machines are anything
but static and the chances of a virtual machine remaining on the same Hyper-V
host for an indefinite period of time are slim.
If you have virtual machines that need to be kept separate, you can reduce
the chances of those virtual machines ever ending up on a common host by
using anti-affinity rules. Anti-affinity rules prevent virtual machines that must
remain separate from failing over to a common host.
Unfortunately, anti-affinity rules are not exactly easy to work with. These
rules can only be established through PowerShell, and the process is not
very intuitive.
The key to understanding how the process works is to understand that
for every clustered virtual machine, there is a corresponding cluster group.
Each cluster group uses the same name as the virtual machine for which
it was created. Anti-affinity rules are applied to cluster groups, not to
virtual machines.
Like other types of objects in PowerShell, cluster groups have a number of
different properties (Figure 4.19). To see a list of the cluster group properties,
enter the following command:
Get-ClusterGroup <group name> | Select-Object *
134
Understanding Hyper-V in Windows Server 2012
Figure 4.19
135
Understanding Hyper-V in Windows Server 2012
Once you have extracted the zip file’s contents, you’ll need to import
the module into PowerShell. Note that importing a module is not a permanent
operation. The module only remains imported for as long as PowerShell is
open. If you need to use the module again at a later time, you will have to
import it again.
Before you can import the module, you will have to configure your server’s
execution policy to allow PowerShell scripts to be run. The easiest way to do
this is to enter the following command:
Set-ExecutionPolicy Unrestricted
This command allows PowerShell to run any PowerShell script, regardless
of where it came from (Figure 4.20). Obviously, there are some security
implications associated with allowing unrestricted access to scripts, as
explained in the text shown below (Figure 4.20). If you are concerned about
security, you can set the execution policy back to Restricted when you finish
working with the AntiAffinityClassNames by using the Set-ExecutionPolicy
Restricted cmdlet.
Figure 4.20
You must configure the execution policy to allow PowerShell scripts to be run.
With that said, you can use the following command to import the module
(assuming that the module resides in C:\modules):
Import-Module C:\Modules\AntiAffinityClassNames
When you execute the command listed above, you will see several prompts
asking if you want to allow the script to run (Figure 4.21). You must run each of
these scripts in order to successfully import the module.
Figure 4.21
You must import the AntiAffinityClassNames module before you can use it.
As previously mentioned, a PowerShell module adds cmdlets to the built-in
cmdlet set. If you would like to see a list of the new cmdlets that were added,
enter the following command (Figure 4.22):
Get-Command –Module AntiAffinityClassNames
136
Understanding Hyper-V in Windows Server 2012
Figure 4.22
137
Understanding Hyper-V in Windows Server 2012
Figure 4.23
138
Understanding Hyper-V in Windows Server 2012
By prioritizing your virtual machines, you can make sure that the most
important virtual machines continue to function in a failover situation.
Windows will start the highest priority virtual machines first and lower priority
virtual machines in sequence until the server runs out of memory or other
resources.
Setting a virtual machine’s priority is easy. To do so, follow these steps:
1. Open the Server Manager.
2. Choose the Failover Cluster Manager command from the Tools menu.
3. When the Failover Cluster Manager starts, select the Roles container.
4. Right-click on the virtual machine that you want to prioritize and choose
the Change Startup Priority command from the right-click menu
(Figure 4.25).
5. Specify the virtual machine’s priority.
Figure 4.25
139
Understanding Hyper-V in Windows Server 2012
Hyper-V allows you to specify a preferred host server for any clustered virtual machine.
Although Microsoft makes it easy to specify a preferred host, there are two
things that you should know about host server prioritization. First, you can
specify multiple preferred servers by selecting the checkboxes for each
preferred cluster node. Use the Up and Down buttons shown in the figure
above to arrange the list of cluster nodes based on your preferred priority.
Second, host prioritization is not guaranteed. If a virtual machine’s preferred
host is offline or lacks the capacity to host a virtual machine, the virtual
machine will be hosted on a cluster node other than the preferred node.
Failover rules
The Failover Cluster Service allows you to set some ground rules for the way
that virtual machines failover to other cluster nodes. These rules are all
controlled through a dialog box within the Failover Cluster Manager. To access
this dialog box, follow these steps:
1. Open the Server Manager.
2. Select the Failover Cluster Manager option from the Tools menu.
3. When the Failover Cluster Manager opens, select the Roles container.
4. Right-click on a clustered virtual machine.
5. Select the Properties command from the right-click menu.
6. When the virtual machine’s properties sheet appears, select the Failover
tab (Figure 4.27).
140
Understanding Hyper-V in Windows Server 2012
Figure 4.27
141
Understanding Hyper-V in Windows Server 2012
Chapter 5
PowerShell
Management
In Windows Server 2008 and 2008 R2, Microsoft allowed
many aspects of the Windows operating system to be
managed through PowerShell. Because PowerShell was
relatively new, however, there were certain operating
system components (including Hyper-V) that could not
be managed through PowerShell. Microsoft eventually
released a PowerShell module for Hyper-V, but this
module had to be downloaded separately. Thankfully,
the Windows Server 2012 version of Hyper-V includes
native PowerShell management capabilities. It is the goal
of this chapter to familiarize you with the various ways
in which Hyper-V can be managed through PowerShell.
142
Understanding Hyper-V in Windows Server 2012
Syntax simplification
Before discussing PowerShell management for Hyper-V, there is some
good news. While developing Windows Server 2012, Microsoft realized that
non‑developers often struggled with PowerShell. In spite of what the marketing
folks might say, the syntax for various PowerShell commands was often
complex and completely non-intuitive. Thankfully, Microsoft decided to simplify
the PowerShell syntax—especially as it relates to the Where-Object and
ForEach-Object cmdlets.
To show you an example of the way that the syntax has been simplified,
imagine that you want to see a list of all of the processes that are running on
the local Hyper-V host server. Prior to Windows Server 2012, you would have
had to use this command:
Get-Process | ForEach {$_.Name}
This same simplification also applies to operations in which you use operators
to filter the result set. For example, prior to Windows Server 2012, to see a list
of all of the processes using more than 500 handles, you would have had to use
the following command:
Get-Process | where {$_.Handles –gt 500}
143
Understanding Hyper-V in Windows Server 2012
Notice in the screen capture (Figure 5.1) that the PowerShell prompt changes
to reflect the name of the server that you are attached to.
Figure 5.1
You can use the Enter-PSSession cmdlet to establish a remote PowerShell session.
Another method is to use the Invoke-Command cmdlet to execute a single
command on a remote host (Figure 5.2). The trick is to specify the computer
name. You can even specify multiple computer names if you separate them
by commas. The actual command that you send to the remote machines must
appear in braces. For example, to send the Get-VM command to computers
named Lab1, Lab2 and Lab3, you would enter the following command:
Invoke-command –ComputerName Lab1, Lab2, Lab3 {Get-VM}
144
Understanding Hyper-V in Windows Server 2012
Figure 5.2
You can use the Invoke-Command cmdlet to remotely execute a PowerShell command.
Keep in mind that these are the simplest methods of establishing a remote
PowerShell session. Things can become more complex if you need to enter
an alternative set of permissions or if you need to modify the execution
policy to allow scripts to be run. For example, the block of PowerShell listed
below is used to manage an Office 365 environment through PowerShell.
Although Office 365 is beyond the scope of this book, this example illustrates
the point that there are more complex methods of establishing remote
PowerShell connectivity.
Set-ExecutionPolicy RemoteSigned
Import-Module MSOnline
$Cred = Get-Credential
$MySession = New-PSSession -ConfigurationName Microsoft.Exchange
-ConnectionUri https://ps.outlook.com/powershell -Credential $Cred
-Authentication Basic -AllowRedirection
Import-PSSession $MySession
145
Understanding Hyper-V in Windows Server 2012
Figure 5.3
The Get-VM cmdlet returns a list of the virtual machines that are present on the host.
Notice in Figure 5.3 that the Get-VM cmdlet returns virtual machine information
from the local server. Had we established a PowerShell session with a remote
server, virtual machine information from the remote server would have been
returned instead. If you need to perform a Get-VM query against a remote
server, however, it may not always be necessary to establish a session with the
server. Remote sessions are most useful when you need to redirect PowerShell
so that all operations are performed against the remote host instead of
the local host. If you simply need to retrieve a bit of information from the
remote host, you can often do so by using a standard PowerShell cmdlet and
appending the name of the remote server.
To see how this works, imagine that you want to see a list of the virtual
machines residing on a host named Prod1 (Figure 5.4). You can accomplish this
by entering the following command:
Get-VM –ComputerName Prod1
Figure 5.4
You can get a list of the virtual machines that reside on a specific host.
Of course, the list of virtual machines that are running on a specific host could
potentially be really long. Often you might be more interested in viewing
the state of a specific virtual machine rather than seeing a list of every virtual
machine that exists on a host server. In this case, you can append a virtual
machine name directly to the end of the Get-VM cmdlet (Figure 5.5). For
example, to view the current state of a VM named Lab-DC you can enter
this command:
Get-VM Lab-DC
Figure 5.5
146
Understanding Hyper-V in Windows Server 2012
Just as you can use the Get-VM cmdlet to retrieve basic information about
virtual machines, you can use the Get-VMHost cmdlet to access information
about host servers (Figure 5.6). Entering the Get-VMHost cmdlet by itself
returns the name of your host server along with its logical processor count,
memory capacity and whether or not the virtual machine is configured to allow
virtual machine migrations.
Figure 5.6
You can use the Get-VM cmdlet to retrieve virtual machine information.
147
Understanding Hyper-V in Windows Server 2012
One of the nice things about PowerShell is that it allows you to use attributes
and operators to filter a cmdlet’s output. Some of the commonly used
operators include:
Equal To -EQ
Or -OR
And -AND
Like -Like
Greater Than -GT
Less Than -LT
You can combine these operators with object attributes to retrieve very
granular information about your host servers and virtual machines.
One of the most basic examples of output filtering involves filtering virtual
machines by name. For example, entering the Get-VM cmdlet on the host Lab1
returns listings for a number of virtual machines. On this server the name of
each virtual machine reflects the virtual machine’s purpose. For example, virtual
machines starting with LAB15 are lab machines related to Microsoft’s wave 15
product release (Office 15, SharePoint 15, Exchange 15, etc.).
With this in mind, it might occasionally be necessary to get a list of the virtual
machines that are related to wave 15 testing. In this situation, the Like operator
could prove to be very handy. You can use the Like operator to list values that
are similar to a target value. For instance, you can list all of the virtual machines
that have names starting with Lab15 (Figure 5.9) by using the following cmdlet:
Get-VM | Where Name –Like ‘Lab15*’
Figure 5.9
You can use filtering to narrow down the list of virtual machines.
Being able to filter virtual machines by name is nice, but this is not the only
type of filtering you can do. It is also possible to filter on things like a virtual
machine’s state. For instance, if you want to see which virtual machines are
currently powered off (Figure 5.10), you can use the following command:
Get-VM | Where State –EQ ‘Off’
Figure 5.10
148
Understanding Hyper-V in Windows Server 2012
Obviously it’s handy to be able to see a list of the virtual machines that are
powered off, but remember that the command shown above only looks
at the current host server. What if you wanted to see a list of the running virtual
machines across multiple hosts? To accomplish this, you would simply add
the –ComputerName switch and the names of the hosts that should be
included in the query (Figure 5.11). The command might look something
like this:
Get-VM –ComputerName Lab1,Lab2,Lab3,Prod1,Prod2 | Where State –EQ
‘Running’
Figure 5.11
A filtered output can contain data from multiple Hyper-V host servers.
You can create a list of clustered virtual machines that are not currently running.
149
Understanding Hyper-V in Windows Server 2012
You can use PowerShell to determine which VMs are consuming excessive system resources.
It is worth noting that even though PowerShell displays the memory in
megabytes in this case, you must enter the MemoryAssigned value in bytes.
150
Understanding Hyper-V in Windows Server 2012
Figure 5.15
151
Understanding Hyper-V in Windows Server 2012
Figure 5.16
You can use the Get-VMHost cmdlet to retrieve host server information.
As you can see, there is a lot of information available both for virtual machines
and for host servers. But what can you do with all of this information?
For starters, you can control the output of the Get-VM or the Get-VMHost
cmdlets (Figure 5.17). For example, suppose that you want to see each virtual
machine’s name, the name of the host on which the virtual machine currently
resides and whether or not the virtual machine is clustered. You can accomplish
this by entering the following command:
Get-VM | FT VMName, ComputerName, IsClustered
Figure 5.17
You can control the output of the Get-VM or the Get-VMHost cmdlets.
Of course, you output the values of any of the available attributes and you can
also use the various attributes with operators such as –Like, -And, -GT, etc.
to achieve a filtered output.
It is worth noting that Microsoft provides a number of cmdlets that you can
use as shortcuts to retrieve very specific information about virtual machines.
For example, you can use the Get-VMMemory cmdlet to retrieve a virtual
machine’s memory configuration. Likewise, you can use the Get-VMProcessor
cmdlet to view a virtual machine’s virtual CPU configuration.
152
Understanding Hyper-V in Windows Server 2012
Reporting
In the previous section you learned how to modify the Get-VM
and the Get‑VMHost cmdlets to achieve the desired output, but you can
actually take things a step further and use PowerShell as a reporting engine.
You can use PowerShell to create CSV, Text and even HTML files.
153
Understanding Hyper-V in Windows Server 2012
Figure 5.19
Often, it is necessary to use the Select-Object cmdlet to specify what data to include
in a report.
154
Understanding Hyper-V in Windows Server 2012
CSV files
CSV files are almost always created in table format. CSV files are useful
for outputting large amounts of information in a format that can be read
by Microsoft Excel or imported into a database. You can create a CSV file by
appending the Export-CSV cmdlet to whatever command you are using. It
is worth noting, however, that you must manually specify the attributes to
include in the output. For example, to create a CSV file containing a listing of
your virtual machines, their state, the host, whether or not the virtual machine
is clustered, and the virtual machine’s uptime and status (Figure 5.21), you can
enter this command:
Get-VM | Select-Object VMName, State, ComputerName, IsClustered,
UpTime, Status | Export-CSV C:\Data\VMs.csv
Figure 5.21
Text files
The process of creating a text file is similar to that of creating a CSV file,
but PowerShell is a bit more forgiving when it comes to creating text files.
You don’t have to use the Select-Object cmdlet and can instead use FT or FL.
Rather than using the Export-CSV cmdlet, you will use the Out-File cmdlet.
Other than that, the commands are identical. For example, to write a list of
virtual machines to a text file (Figure 5.23), you can use this command:
Get-VM | FT VMName, State, ComputerName, IsClustered, UpTime, Status |
Out-File C:\Data\VMs.txt
155
Understanding Hyper-V in Windows Server 2012
Figure 5.23
HTML reports
The process of creating an HTML report is similar to that of creating a text
file—both use the Out-File cmdlet. However, to create an HTML report,
you need to add the ConvertTo-HTML cmdlet. For example, to write a list of
your virtual machines to an HTML file, use this command:
Get-VM | Select-Object VMName, State, ComputerName, IsClustered,
UpTime, Status | ConvertTo-HTML | Out-File C:\Data\VMs.htm
Just for fun, you can modify this command so that it creates the report
and automatically opens it in Internet Explorer (Figure 5.24). To do this,
append the Invoke-Expression cmdlet to the end of the command as shown
in the following example:
Get-VM | Select-Object VMName, State, ComputerName, IsClustered,
UpTime, Status | ConvertTo-HTML | Out-File CL\Data\VMs.htm | Invoke-
Expression C:\Data\VMs.htm
Figure 5.24
156
Understanding Hyper-V in Windows Server 2012
Figure 5.25
157
Understanding Hyper-V in Windows Server 2012
Figure 5.27
You will see a warning message if a virtual machine cannot be shut down gracefully.
The Start-VM cmdlet starts the virtual machine. The only required parameter is
the name of the virtual machine that you want to start (Figure 5.29).
158
Understanding Hyper-V in Windows Server 2012
Figure 5.29
Keep in mind that you will almost always have to make modifications to a newly
created virtual machine before using it. When you create a virtual machine
using the method discussed above, it is allocated minimal hardware that is
almost never suitable for real world use. The hardware allocation includes:
• 512 MB of memory
• DVD drive
• No virtual hard disk
• No network connection
• One virtual processor
Fortunately, you can use PowerShell cmdlets to modify a new or an existing
virtual machine.
Memory
The resource that you will likely have to adjust more often than any other is
memory. You can adjust memory by using the Set-VMMemory cmdlet.
Start the process by entering the Get-VMMemory cmdlet, followed by
the name of the virtual machine (Figure 5.32). This causes PowerShell to display
the virtual machine’s current memory configuration.
Figure 5.32
You can use the Get-VMMemory cmdlet to see a virtual machine’s memory assignment.
The easiest way to reallocate a virtual machine’s memory is to assign a static
amount of memory to the virtual machine by entering the following command:
Set-VMMemory <virtual machine name> –Startup <memory amount>
For example, to add 2 GB of memory to the virtual machine named
PowerShellVM (Figure 5.33), use the following command:
Set-VMMemory PowerShellVM –Startup 2.0GB
160
Understanding Hyper-V in Windows Server 2012
Figure 5.33
You can use the Set-VMMemory cmdlet to allocate memory to a virtual machine.
This command sets the startup memory to 2 GB. You can verify the operation’s
success by using the Get-VMMemory cmdlet.
If you want to configure a virtual machine to use dynamic memory, things
become a bit more complicated. You still use the Set-VMMemory cmdlet,
but you have to include a specification to enable dynamic memory. You will
also have to provide values for the minimum, startup and maximum memory.
You can optionally set a priority and a buffer value for the virtual machine as
well by using the following command:
Set-VMMemory <virtual machine name> -DynamicMemoryEnabled $True
–MinimumBytes <minimum memory> -StartupBytes <startup memory>
-MaximumBytes <maximum memory> -Priority <priority> -Buffer
<buffer value>
For example, suppose you want to configure the virtual machine PowerShellVM
to use a 1 GB of startup memory and you want to set the minimum memory
to 512 MB and the maximum memory to 2 GB. Let’s also assume that you want
to set the priority to 80 and the buffer to 25 (Figure 5.34). You can do this with
the following command:
Set-VMMemory PowerShellVM –DynamicMemoryEnabled $True –
MinimumBytes 512MB -StartupBytes 1GB –MaximumBytes 2GB –Priority 80
–Buffer 25
Figure 5.34
You can also use the Set-VMMemory for configuring dynamic memory.
Now that you know how to allocate memory to a virtual machine, here are
a couple of shortcuts that you can use. Keep in mind that you can use these
shortcuts with any type of hardware allocation. The following example uses
only memory allocation.
161
Understanding Hyper-V in Windows Server 2012
162
Understanding Hyper-V in Windows Server 2012
You can use the Get-VMNetworkAdapter cmdlet to access virtual network adapter
information for a virtual machine.
In many cases, you may find that although a network adapter has been
assigned to a virtual machine, you must connect that network adapter
to a virtual switch. Fortunately, this is relatively easy to do. While it is possible
to use a single command to attach a virtual network adapter to a virtual
switch, it’s better to use variables because the virtual network adapter name
and the virtual switch name may be long. Using variables reduces the chances
of making a mistake by mistyping one of the names.
Therefore, the first step in the process is to retrieve the name of the virtual
machine’s virtual network adapter and assign this name to a variable named
$VMNic using the following command:
$VMNic = Get-VMNetworkAdapter –VMName <virtual machine name>
The next step is to retrieve the name of the virtual switch by using
the following command:
Get-VMSwitch | Select-Object Name
If you have multiple virtual switches, you can narrow down the results by
specifying the virtual switch’s connectivity. For example, if you only want to use
an external virtual switch, you can use the following cmdlet:
Get-VMSwitch –SwitchType External | Select-Object Name
Now you just need to connect the virtual network adapter to the virtual switch.
You should be able to do this by using the Connect-VMNetworkAdapter
cmdlet; however, PowerShell apparently doesn’t allow you to add the virtual
switch name to a variable and use the variable to connect the virtual switch
to a virtual network adapter. Instead, you have to enter the virtual switch name
in long form. The command looks something like this:
Connect-VMNetworkAdapter –VMNetworkAdapter $VMNic –SwitchName
“<virtual switch name>”
163
Understanding Hyper-V in Windows Server 2012
As a more concrete example, for the virtual switch on a test server named
Intel(R) Gigabit CT Desktop Adapter #2 – Virtual Switch (Figure 5.38), you would
use the following command:
Connect-VMNetworkAdapter –VMNetworkAdapter $VMNic –SwitchName
“Intel(R) Gigabit CT Desktop Adapter #2 – Virtual Switch”
Figure 5.38
CPU cores
When you create a new virtual machine through PowerShell, Windows
automatically assigns a single virtual processor to the virtual machine. In some
cases, however, a single virtual processor might not be sufficient. Fortunately,
PowerShell makes it possible to add virtual processors to a virtual machine.
Before you attempt to modify a virtual machine’s virtual processor allocation,
it is a good idea to verify the number of virtual processors currently
assigned to the virtual machine (Figure 5.40). You can do this by using
the following command:
Get-VM <virtual machine name>| Select-Object VMName, ProcessorCount
Figure 5.40
It’s a good idea to verify a virtual machine’s current virtual CPU count.
Once you have verified the virtual machine’s current virtual processor
count, you can modify the number of virtual processors that are assigned
to the virtual machine by using the following command:
Set-VMProcessor <virtual machine name> -Count <number of virtual CPUs>
164
Understanding Hyper-V in Windows Server 2012
You can use the Set-VMProcessor cmdlet to configure a virtual machine’s virtual CPU usage.
Of course there are also some other switches that you can use with
the Set‑VMProcessor cmdlet. For instance, if you want to set a reserve,
maximum and relative weight value, you can use a command like this:
Set-VMProcessor <virtual machine name> -Count 2 –Reserve 10 – Maximum
75 – RelativeWeight 200
In case you are not familiar with these particular values, here are their
meanings:
• Weight – A relative weight that affects how much CPU time a virtual
machine will receive. The default weight is 100.
You can also use the Set-VMProcessor cmdlet to enable compatibility for
older operating systems by including the –Compatibility For Older Operating
Systems Enabled switch in the command, as in the following example:
Set-VMProcessor <virtual machine name>
-CompatibilityForOlderOperatingSystemsEnabled $true
165
Understanding Hyper-V in Windows Server 2012
This command (Figure 5.42) works really well for creating a virtual machine
and its virtual hard disk. The problem is that the virtual machine is still a bit
lacking. By default, this virtual machine is only equipped with 512 MB of
memory, a single virtual processor, and the virtual network adapter is not
connected to a virtual switch (Figure 5.43). Never mind the fact that you might
need to create some additional virtual hard disks for the virtual machine.
Figure 5.42
You can create a virtual hard disk and a virtual machine at the same time.
Figure 5.43
166
Understanding Hyper-V in Windows Server 2012
This example illustrates how to create a new virtual machine named NewVM5.
The virtual machine will be equipped with two 50-GB virtual hard disks, two
gigabytes of virtual memory, four virtual processors, and it will be connected
to a virtual switch.
Start by creating the virtual machine and its 50-GB boot drive (Figure 5.44),
using the following command:
New-VM –Name NewVM5 –NewVHDPath F:\NewVM5\disk1.VHDX –
NewVHDSize 50GB –Path F:\NewVM5
Figure 5.44
You can create a new virtual machine from the command line.
Now that you have created the virtual machine, you need to provision it. Start
by adding the second 50-GB virtual hard disk (Figure 5.45). You can create
a new hard disk using this command:
New-VHD F:\NewVM5\Disk2.VHDX –Size 50GB
Figure 5.45
The new virtual hard disk is not attached to the virtual machine.
167
Understanding Hyper-V in Windows Server 2012
This command not only displays the virtual hard disks that are in use, it also
shows which IDE ports are currently in use. When you join the newly created
virtual hard disk to the virtual machine, you will have to choose a set of
ports that are not in use. For example, to use IDE port 0.1 you would use
this command:
Add-VMHardDiskDrive NewVM5 IDE 0 1 –Path F:\NewVM5\Disk2.vhdx
The command does not generate any output, so to verify its success you need
to reissue the Get-VMHardDiskDrive cmdlet (Figure 5.47) as shown below:
Get-VMHardDiskDrive NewVM5
Figure 5.47
You must add the virtual hard disk to the virtual machine.
Now that the virtual hard disks are in place, you simply need to provision
memory, CPU and network connectivity. The task of provisioning memory
and CPU resources to the new virtual machine is easy (Figure 5.48) and can be
accomplished using these commands:
Set-VMMemory NewVM5 –Startup 4GB
Set-VMProcessor NewVM5 –Count 4
Figure 5.48
168
Understanding Hyper-V in Windows Server 2012
Figure 5.49
You can use the Hyper-V Manager to verify the virtual machine settings.
169
Understanding Hyper-V in Windows Server 2012
170
Understanding Hyper-V in Windows Server 2012
Figure 5.53
171
Understanding Hyper-V in Windows Server 2012
If you decide instead to remove the most recently created virtual machine
snapshot, the command syntax is exactly the same, except that you
would use the Remove-VMSnapshot cmdlet (Figure 5.56) instead of
the Restore‑VMSnapshot cmdlet. The syntax is as follows:
Remove-VMSnapshot –Name (Get-VMSnapshot –VMName “<virtual machine
name>”).Name –VMName “<virtual machine name>”
Figure 5.56
172
Understanding Hyper-V in Windows Server 2012
Getting help
Although you can do a lot with regard to managing Hyper-V through
PowerShell, the PowerShell cmdlets and their syntaxes are sometimes
difficult to remember. Fortunately, Microsoft provides a number of different
mechanisms to help you use PowerShell.
The question mark switch is used to retrieve the full syntax for any PowerShell cmdlet.
Any time you use the question mark switch, PowerShell asks you if you want
to run the Update-Help cmdlet. The Update-Help cmdlet causes PowerShell
to download the latest help files from the Internet. When the help file update
completes, the full command syntax will be displayed.
Get-Help
PowerShell cmdlets are a combination of nouns and verbs. Sometimes,
however, it can be difficult to remember what noun-verb combinations can be
used together, and this is where the Get-Help cmdlet comes into play. Get-Help
can show you all of the nouns that can be used with a particular verb or all of
the verbs that can be used with a specific noun. For example, in Hyper-V many
of the cmdlets are designed to be used on virtual machines and therefore use
VM as a noun (Get-VM, Set-VM, etc.). If you want to see all of the actions that
can be performed on a VM (Figure 5.59), you can use the following command:
Get-Help *-VM
173
Understanding Hyper-V in Windows Server 2012
Figure 5.59
These are all of the verbs that can be used with the VM noun.
As previously mentioned, Get-Help can be used with both nouns and verbs.
Notice in Figure 5.59 that the last cmdlet on the list is Suspend-VM. If you are
curious as to whether the Suspend verb can be used with nouns other than VM,
you can enter this command:
Get-Help Suspend-*
When you enter this command (Figure 5.60), you will see that PowerShell
uses the Suspend verb for a number of different purposes, some of which are
not even related to Hyper-V. For example, you can use the Suspend verb to
suspend a service or a print job.
Figure 8.60
The Get-Help cmdlet can help you to figure out what noun and verb combinations will
work together.
What-If
As you have no doubt noticed throughout this chapter, some PowerShell
commands are complicated. That being the case, it would be nice to have
a way to make sure that you are entering the correct command before you
actually press Enter. The good news is that Windows gives you a way to
find out what a specific PowerShell command would do before you actually
execute the command. The trick is to append the –WhatIf switch to the end of
the command.
For example, suppose that you weren’t quite sure what would happen if you
executed the New-VM command. You could append the –WhatIf switch to
the command (Figure 5.61) and PowerShell would tell you that the command
would create a new virtual machine named New Virtual Machine.
Figure 5.61
The WhatIf switch is used to find out what will happen if you perform a certain command.
174
Understanding Hyper-V in Windows Server 2012
It is worth noting that not every PowerShell cmdlet can be used with
WhatIf. To see which cmdlets support the use of WhatIf (Figure 5.62), enter
the following command:
Get-Command | Where Definition –Like *whatif*
Figure 5.62
IntelliSense
In addition to the PowerShell interface that is shown throughout this chapter,
Microsoft also provides a feature called the PowerShell Integrated Scripting
Environment (ISE). In the Windows Server 2012 version of PowerShell,
the Integrated Scripting Environment contains a feature called IntelliSense,
which anticipates the command that you are about to type and offers help as
you enter the command.
To see how this feature works, go to your Hyper-V server’s Metro interface,
click the Administrative Tools tile and double-click on the Windows PowerShell
ISE command (Figure 5.63), which is found on the Administrative Tools list.
175
Understanding Hyper-V in Windows Server 2012
Figure 5.63
The Windows PowerShell ISE icon is found on the Administrative Tools menu.
When the Windows PowerShell ISE window opens, start typing a PowerShell
command. As you type, the IntelliSense feature will engage and help you with
the command’s syntax (Figure 5.64). For instance, if you type New-V, then
IntelliSense will show you all of the cmdlets that start with New-V. As you
progress through the list of commands, IntelliSense even offers syntax help.
Figure 5.64
176
Understanding Hyper-V in Windows Server 2012
Figure 5.65
TechNet
Finally, Microsoft TechNet is a great resource for getting help with PowerShell
commands. The page is located at: http://technet.microsoft.com/en-us/library/
hh848559.aspx (Figure 5.66) and contains a full list of every available Hyper-V-
related cmdlet. Clicking on a cmdlet takes you to a page that provides the full
syntax (Figure 5.67) and some usage examples for the cmdlet.
177
Understanding Hyper-V in Windows Server 2012
Figure 5.66
TechNet provides a reference for all of all of the Hyper-V related cmdlets.
Figure 5.67
Clicking on a link takes you to a page with the cmdlet’s syntax and usage examples.
178
Understanding Hyper-V in Windows Server 2012
179
Understanding Hyper-V in Windows Server 2012
2010
Products
of the Year
GOLD