Reader small image

You're reading from  Windows Server 2016 Automation with PowerShell Cookbook - Second Edition

Product typeBook
Published inSep 2017
Reading LevelBeginner
Publisher
ISBN-139781787122048
Edition2nd Edition
Languages
Right arrow
Authors (2):
Thomas Lee
Thomas Lee
author image
Thomas Lee

Thomas Lee is a consultant/trainer/writer based in the UK and has been in the IT business since the late 1960s. After graduating from Carnegie Mellon University, Thomas joined ComShare where he was a systems programmer building the Commander II time-sharing operating system, a forerunner of today's cloud computing paradigm. In the mid-1970s, he moved to ICL to work on the VME/K operating system. After a sabbatical in 1980/81, he joined Accenture, leaving in 1988 to run his own consulting and training business, which is still active today. Thomas holds numerous Microsoft certifications, including MCSE (one of the first in the world) and later versions, MCT (25 years), and was awarded Microsoft's MVP award 17 times.
Read more about Thomas Lee

 Ed Goad
Ed Goad
author image
Ed Goad

Ed Goad is a systems architect who has been working in various roles in the IT field for 16 years. He first became interested in scripting and automation when presented with a task to uninstall software from over 1,000 systems with limited time and resources. He has worked with scripting and automation on multiple platforms and languages including PowerShell, VBscript, C#, and BASH scripting. Ed currently holds multiple Microsoft certifications, most recently including the Microsoft Certified IT Professional Enterprise Administrator. Additional non-Microsoft certifications include VMware Certified Professional (VCP), Red Hat Certified System Administrator (RHCSA), EMC Proven Professional, Brocade Certified Network Engineer (BCNE), and Cisco Certified Network Associate (CCNA). Ed is currently on a sabbatical and volunteering full time at the Amor Fe y Esperanza school in Honduras(http://www.afehonduras.org). There he is teaching computer and math classes to the kids who live and work in the garbage dump outside of the capital city of Tegucigalpa.
Read more about Ed Goad

View More author details
Right arrow

Chapter 11. Managing Hyper-V

In this chapter, we cover the following recipes:

  • Installing and configuring Hyper-V feature
  • Using Windows PowerShell Direct
  • Securing Hyper-V host
  • Creating a virtual machine
  • Configuring VM hardware
  • Configuring Hyper-V networking
  • Implementing nested Hyper-V
  • Managing VM state
  • Configuring VM and storage movement
  • Configuring VM replication
  • Managing VM checkpoints
  • Monitoring Hyper-V utilization and performance
  • Creating a Hyper-V health report

Introduction


Hyper-V is Microsoft's virtual machine hypervisor. Both Windows Server 2016 and Windows 10 include Hyper-V as an option you can install. The Hyper-V feature is included in all versions of Windows Server 2016, as well as in the Enterprise, Professional, and Education editions of Windows 10. Nested Hyper-V, the ability to run Hyper-V inside a Hyper-V VM, is available in both Windows 10 Anniversary Update and Windows Server 2016. Additionally, Microsoft has made the Microsoft Hyper-V Server available as a free version of the Hyper-V hypervisor. The Hyper-V Server runs virtual machines with no GUI. You configure and manage remotely.

Hyper-V was first released with Server 2008. Successive versions of Windows brought improvements in features, hardware support, and scalability. The first version did not include PowerShell cmdlet support, but that was rectified in later releases. With Server 2016 there is good PowerShell coverage. This chapter focuses solely on Hyper-V inside Windows...

Installing and configuring Hyper-V feature


To install Hyper-V on Windows Server 2016, you install the Hyper-V feature. In this recipe, you do the set up remotely from a client machine using the Hyper-V cmdlets and PowerShell's remoting capabilities.

Getting ready

For this recipe, you need to have the host computers on which you install Hyper-V. This recipe uses two servers, HV1 and HV2. Each server is a member of the domain on which you have added no additional services. As an alternative to having two systems running, you could use embedded Hyper-V and create the two VMs inside a third.

To demonstrate remote configuration, you perform this recipe from a third computer, CL1 running Windows 10 (Anniversary Update). This recipe makes use of a second hard disk, an H: drive on the HV1 and HV2 systems that you use to store Hyper-V VMs and virtual disks.

You need the Hyper-V tools on CL1—add them using the Enable-WindowsOptionalFeature and use the -Online switch, as follows:

# Add windows optional...

Using Windows PowerShell Direct


PowerShell Direct (PSD) is a new feature with Windows Server 2016 (and on Windows 10 Anniversary Update or later). PSD enables you to use PowerShell remoting to access a Hyper-V VM without needing to setup networking and firewall settings. With PSD, you use Invoke-Command, specifying either the VM's name or the VM's VMID (the VMID is a GUID used internally by Hyper-V to identify a VM). You can also use the VM name or VMID to enter a remote session using Enter-PSSession.

In previous versions of Hyper-V, you needed some sort of networking connection between your Hyper-V host and the guest OS in order to remote into the guest. This was often setting up firewall exceptions and establishing network connectivity. With PSD, you can use the VM's name or ID and remote straight in.

Getting ready

For this recipe, you need a Hyper-V Host running on either Windows 10 Anniversary Update (or later) or Windows Server 2016, with Hyper-V loaded and operational. Additionally, you...

Securing Hyper-V host


With server virtualization becoming more and more the norm, managing groups of Hyper-V Servers can be simplified by using VM groups. VM Groups are a new feature in Windows Server 2016 that allows you to create and use groups of servers. Server 2016 supports two different types of VM Groups: VM collection groups and management collection groups. A VM collection group is a collection of Hyper-V VMs. You can carry out tasks on the groups rather than on each individual VM. A management collection group is a collection of VM collection groups and other nested management collection groups. VM groups are especially useful for backups and for VM replication. In backup situations, a number of VMs making up a multi-tier application need to be backed up together.

In this recipe, you create a VM collection group containing two servers. Then you set the MAC addresses used by the two servers and you enable host resource protection.

Getting ready

This recipe assumes you have two servers...

Create a virtual machine


You create Hyper-V virtual machines in several distinct steps. First, you need to create the VM itself—creating a virtual machine and the virtual hard drive and assign hardware such as memory, CPU cores, and DVD drives (and drive contents).

Once the VM is created, you need to work out how to install an OS into the VM. This process can be complex if you use native commands.

In this recipe, you create a simple VM that installs the OS into the VM based on GUI input. This recipe is therefore often the precursor to other configuration (that is you create a new VM and then add features and configure it per your requirements. This is the same experience you would observe if you had a physical machine, with an empty C: drive that you boot from a Windows Server 2016 installation ISO.

Getting ready

For this recipe, you need a Hyper-V VM host—use HV1 which needs an H: drive. The H: drive is where you store the VM and virtual disks.

You also need the ISO image for Windows Server...

Configuring VM hardware


Configuring hardware in your virtual machine is very much like configuring a physical computer. With a physical computer, you can adjust the CPUs and BIOS settings. You can also adjust physical RAM, network interfaces, disk interfaces and disk devices, and DVD drives (with/without a loaded DVD), and so on. Each of these physical components is provided within a Hyper-V VM and the PowerShell cmdlets make it simple to adjust the virtual hardware in a VM.

In this recipe, you adjust the VM's BIOS, CPU count, memory, add a SCSI controller, and finally create and add a virtual disk to the SCSI controller. Just like in most commercial grade physical servers, not all of these components can be changed while the server is running. You run this recipe from HV1 and turn the VM1 VM off before configuring the virtual hardware.

This recipe does not cover the virtual NIC which you need in order to use the VM in your network. Configuring the VM's networking is covered in the Configuring...

Configuring Hyper-V networking


In the Creating a virtual machine recipe, you created a VM, VM1. This virtual machine has, by default, a single network card that Hyper-V sets to acquire IP address details from DHCP. In this recipe, you assign the NIC to a switch and configure IP address details.

Getting ready

This recipe assumes you have created VM1 as per the Creating a virtual machine recipe. The recipe also makes use of a DHCP server running on DC1. You set this DHCP server up in the Installing and authorizing a DHCP Server recipe, and configured the DHCP server in the Configure DHCP scopes recipe, in Chapter 8, Managing Windows Network Services.

How to do it...

Here you see how to configure Hyper-V networking:

  1. Get NIC details and any IP Address from the VM1 VM:
      Get-VMNetworkAdapter -VMName VM1
  1. Get VM networking details:
      $user = 'Localhost\Administrator' 
      $pass =  ConvertTo-SecureString -String 'Pa$$w0rd' `
               -AsPlainText -Force
      $cred = New-Object -TypeName...

Implementing nested Hyper-V


Nested Hyper-V is a new feature in Windows 2016 and Windows 10 (Anniversary update and later). Nested Hyper-V enables a Hyper-V VM to host VMs which also have virtualization enabled. You could, for example, take a physical host (say, HV1) and on that host run a VM (say VM1). With nested Hyper-V, you could enable your VM1 VM to host VMs and create a nested VM inside it called Nested1.

Nested VMs have a number of uses. First, nested MVs hosted in one VM are provided hardware isolation from nested VMs run in other VMs. This provides a further level of security for virtual machines. It's also useful for testing and education/training. In a training course, you could give a student one VM and enable him to create additional VMs as part of the course. And most IT pros just find it cool! You could, for example, run all the recipes in this chapter using Nested VMs.

Enabling nested Hyper-V is very simple. First, you must update the virtual CPU in the VM you want to support...

Managing VM state


Managing the VM state involves stopping and starting or pausing and resuming a VM. You can also save and restore a VM.

Getting ready

This recipe uses the VM1 VM created in the Creating a virtual machine recipe. This recipe assumes the VM1 VM is stopped when you start this recipe. If this VM is running, then first stop it using Stop-VM.

How to do it...

Here is how to manage VM state:

  1. Get the VM's state to check if it is off:
      Get-VM     -Name VM1
  1. Start the VM, get its status, then wait until the VM has an IP address assigned and the networking stack is working, then examine the VM's state:
      Start-VM  -VMName VM1 
      Get-Vm    -VMName VM1 
      Wait-VM   -VMName VM1 -For IPAddress 
      Get-VM    -VMName VM1
  1. Suspend and resume a VM:
      Suspend-VM -VMName VM1 
      Get-VM     -VMName VM1 
      Resume-VM  -VMName VM1 
      Get-VM     -VMName VM1
  1. Save the VM and check status:
      Save-VM    -VMName VM1 
      Get-VM     -VMName VM1
  1. Resume the saved VM and view the...

Configuring VM and storage movement


Hyper-V enables you to both move a VM and to move the storage for a VM to a new location. Moving a VM and moving a VM's storage are two important features you can use to manage your Hyper-V hosts.

With live migration, you can move a Hyper-V VM to a different VM host with no downtime. This works best when the VM is held on shared storage (via a fiber channel SAN, iSCSI, or SMB). You can also move a VM's storage (that is any VHD/VHDX associated with the VM) to a different location. You can also combine these and move a VM supported by local storage to another Hyper-V host (moving both the VM and the underlying storage).

In this recipe, you first move the storage for the VM1 VM. You created this VM in the Creating a virtual machine recipe and stored the VM configuration and the VM's VHD on the H: drive. To move the storage, you create a new SMB share and then move the VM's storage to the new SMB share.

In the second part of this recipe, you do a live migration...

Configuring VM replication


In Hyper-V, VM replication is a disaster recovery feature. It creates a replica of a VM on a remote Hyper-V Server and then keeps the replica up to date. The VM on the remote host is not active, but can be made active should the VM's host for some reason fail.

With Hyper-V replication, the source VM host bundles up any changes in a running VM's VHD file(s) and sends them to the replica server on a regular basis. The replica server then applies those changes to the dormant replica.

Once you have a replica established, you can test the replica to ensure it can start should you need that. Also, you can failover to the replica—bringing the replicated VM up based on the most recently replicated data. If the source VM host becomes inoperable before it can replicate changes on the source VM, there is a risk of those changes being lost.

In this recipe, you create and use a replica of a VM, VM1, that you have running on your HV1 server. The recipe sets up the replica on the...

Managing VM checkpoints


With Hyper-V in Server 2016, a checkpoint captures the state of a VM into a restore point. Hyper-V then enables you to roll back a VM to a checkpoint. Windows Server 2008's version of Hyper-V provided this feature. With Server 2008, these restore points were called snapshots.

With Server 2012, Microsoft changed the name to checkpoint. This made the terminology consistent with System Center, and avoided confusion with respect to the Volume Shadow Copy Service (VSS) snapshots used by backup systems. Whilst the Hyper-V team did change the terminology, some of the cmdlet names remain unchanged. To restore a VM to a checkpoint, you use the Restore-VMSnapShot cmdlet.

When you create a checkpoint, Hyper-V temporarily pauses the VM. It then creates a new differencing disk (AVHD). Hyper-V then resumes the VM which writes all data to the differencing disk. You can create a variety of checkpoints for a VM.

Checkpoints are great for a variety of scenarios. It can be useful for troubleshooting...

Monitoring Hyper-V utilization and performance


In Chapter 6, Managing Performance, you looked at performance counters and the Performance Logs and Alerts (PLA) feature of Windows Server 2016.

With PLA, applications, services, drivers, and OS components can expose operation data via counter sets and counters. A counter is a measurement of one specific value, such as the %ProcessorTime counter that measures how much CPU is being used at any given moment. Counters are grouped for convenience into counter sets. The Processor counter set contains the %ProcessorTime counter.

In this recipe, you examine some of the wide range of Hyper-V performance counters available to you. PLA in general, and Hyper-V in particular, expose a very large number of counter sets containing a wide range which Hyper-V exposes. It is very easy to get excited about the wealth of counters available. But most of the information provided is probably of little use to most IT professionals managing Hyper-V hosts and VMs.

A given...

Creating a Hyper-V health report


Any Hyper-V host that you deploy is a critical part of your IT infrastructure. If the Hyper-V host goes down or starts suffering performance or capacity issues, it can affect all the VMs running on that host. Your Hyper-V hosts are almost certainly mission critical.

If you deploy Hyper-V, it is important you report on and monitor the health of your Hyper-V host, as well as the health of the VMs. By monitoring the reports, you can detect issues, possibly before they become critical. If your VM host, for example, has a slowly increasing CPU load, you can consider moving a VM to another VM host.

Reports that you use to monitor the health of a Hyper-V host fall into two broad categories: the VM host itself, and the VMs running on that host. This recipe creates two reports to mirror this.

This recipe uses a variety of methods to obtain key performance and usage metrics and converts this information into an object. The recipe begins by defining a PowerShell hash table...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Windows Server 2016 Automation with PowerShell Cookbook - Second Edition
Published in: Sep 2017Publisher: ISBN-13: 9781787122048
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Authors (2)

author image
Thomas Lee

Thomas Lee is a consultant/trainer/writer based in the UK and has been in the IT business since the late 1960s. After graduating from Carnegie Mellon University, Thomas joined ComShare where he was a systems programmer building the Commander II time-sharing operating system, a forerunner of today's cloud computing paradigm. In the mid-1970s, he moved to ICL to work on the VME/K operating system. After a sabbatical in 1980/81, he joined Accenture, leaving in 1988 to run his own consulting and training business, which is still active today. Thomas holds numerous Microsoft certifications, including MCSE (one of the first in the world) and later versions, MCT (25 years), and was awarded Microsoft's MVP award 17 times.
Read more about Thomas Lee

author image
Ed Goad

Ed Goad is a systems architect who has been working in various roles in the IT field for 16 years. He first became interested in scripting and automation when presented with a task to uninstall software from over 1,000 systems with limited time and resources. He has worked with scripting and automation on multiple platforms and languages including PowerShell, VBscript, C#, and BASH scripting. Ed currently holds multiple Microsoft certifications, most recently including the Microsoft Certified IT Professional Enterprise Administrator. Additional non-Microsoft certifications include VMware Certified Professional (VCP), Red Hat Certified System Administrator (RHCSA), EMC Proven Professional, Brocade Certified Network Engineer (BCNE), and Cisco Certified Network Associate (CCNA). Ed is currently on a sabbatical and volunteering full time at the Amor Fe y Esperanza school in Honduras(http://www.afehonduras.org). There he is teaching computer and math classes to the kids who live and work in the garbage dump outside of the capital city of Tegucigalpa.
Read more about Ed Goad