Abstract
In this paper, we explore the basic usage of PowerShell in the pursuit of automating the management of Hyper-V deployments. This paper is targeted to IT professionals who have been "GUI-bound" to their management interfaces and have been working with or are embarking on the journey of Hyper-V administration. We'll explore the usage of Hyper-V specific cmdlets with the intent of ultimately being able to include multiple cmdlets in a "script" to allow for pushing one button and accomplishing the combined actions of many multiple mouse clicks in the GUI. No more repetitive tasks!
Sample
As Hyper-V becomes more popular as an enterprise-level virtualization platform, the need for understanding and using PowerShell to automate and efficiently manage the Hyper-V environment is imperative. This paper discusses the usage of PowerShell cmdlets specific to installing and configuring Hyper-V. As a general approach to presenting this topic the discussion is directed toward IT professionals who have worked with virtualization platforms such as Hyper-V through the GUI, but have not had much hands-on experience with PowerShell. That said, in this paper we are "baby-stepping" our way through the usage of single cmdlets for Hyper-V management. Cmdlets are the building "blocks" of PowerShell scripts. The expectation is that as the readers' understanding of PowerShell for Hyper-V increases, they will naturally move on to the level of PowerShell usage at a more complex script level. So this paper is for all of the virtualization admins who are tethered to their mouse and the graphical interface. If this is you, we will walk (using cmdlets) before we run (scripts that is).
During our discussion the Hyper-V platform used for specific and general points of interest is Hyper-V version 4 running over a fully installed instance of Server 2012 R2. We examine the PowerShell cmdlets run on the "Host" system.
Cmdlets follow a Verb-Noun convention.
Before we step into managing Hyper-V with PowerShell, let's address the very important usage of PowerShell's built-in Help files. Knowing how to use and interpret these files will go a long way in understanding the purpose of any cmdlet and how to use it as examples are provided with all the necessary details. Also, Microsoft updates the Help files on an ongoing basis as the number of available cmdlets increases so updating the Help files is a best practice. That is accomplished by using the Update-Help -Module Hyper-V cmdlet. There are currently 170 cmdlets just for Hyper-V alone.
PowerShell cmdlets specific to Hyper-V enable Virtualization administrators to fully manage their Hyper-V deployments. As PowerShell is closely integrated with the operating system an administrator can simplify the management and maintenance of their burgeoning and increasingly critical virtualized server platform.
Installation and Initial Configuration of Hyper-V with PowerShell
Hyper-V version 3 and later automatically loads the application-specific module to support the appropriate environment. For example, when a role is installed in Server Manager the PowerShell module to support that role is installed also. Knowing that, is still a good idea the know how to manually import, or install, the modules for trouble-shooting purposes at the very least. As a universal approach to learning anything it is always best to be able to "look under the hood" and have a superior grasp of how the technology works. As an extreme "old school" example of this think "it is best to know how to use a slide rule before learning how to use a calculator."
As with all installations of Hyper-V if functionality beyond the base or default set of PowerShell commands, referred to as "cmdlets," is required then the module-specific set of cmdlets must be installed. This enables specialized scripting for a particular Server-based application such a Hyper-V. This is accomplished by running the Install-WindowsFeature Hyper-V -Restart command. As a best practice prior to installing any module in PowerShell it is recommended to determine which specific modules or features are already installed. This can be done by running the Get-WindowsFeature "*Hyper-V*". We now verify successful installation by re-booting the server. After the server restarts, launch PowerShell from the command window, and type Get-WindowsFeature | where {$_.Installed -eq $True} to verify the successful installation of the Hyper-V role.
Securing PowerShell's Execution Policy
Prior to getting started with PowerShell and creating new guest VMs, it a very good idea to determine the execution policy on the host system for running PowerShell. Given PowerShell's capability regarding all things configuration, its usage in the wrong hands could result in a real problem.