BACKGROUND IMAGE: iSTOCK/GETTY IMAGES
When Windows 8.1 and Windows Server 2012 R2 hit their shared general availability, Microsoft also made a new version...
of PowerShell available. At least half of these little scripts could be helpful to VDI administrators for some of their activities, if not for day-in, day-out routine management tasks.
One great way to put Windows PowerShell scripts to work is through the huge library of Hyper-V cmdlets for Windows Server 2012 R2 and Windows 8.1. It looks to be complete and in sync with the GA materials -- captions and cautions notwithstanding.
It's not just for virtual servers, no sir!
There are a total of 164 Hyper-V cmdlets in this collection, many of which VDI administrators will find valuable. And it's been shown that automation for VDI can decrease the amount of manual management tasks administrators have to handle, especially when it comes to desktop provisioning, updates and more.
Table 1 provides a selective list of items plucked from the TechNet reference, to give you some sense of what kinds of potential treasures lurk inside. Each cmdlet is linked to its corresponding documentation page.
Table 1: VDI-friendly Hyper-V cmdlets
|Add-VMDvdDrive||Installs a DVD drive in a virtual machine (VM).|
|Add-VMHardDiskDrive||Installs a hard disk drive in a VM.|
|Add-VMMigrationNetwork||Adds a network to the list of networks that can be used for VM migration.|
|Checkpoint-VM||Creates a VM snapshot.|
|Connect-VMNetworkAdapter||Connects a virtual network adapter to a virtual network.|
|Convert-VHD||Converts the format and type of the virtual hard disk file of a VM by copying data from a source file into a new file, in either VHDX or VHD format.|
|Copy-VMFile||Copies a file to a VM.|
|Disconnect-VMNetworkAdapter||Disconnects a virtual network adapter from a virtual network or a network resource pool.|
|Dismount-VHD||Specifies the path to files that represent the VHDs you want to be dismounted.|
|Grant-VMConnectAccess||Allows a user to connect to a VM, usually for the purpose of granting other applications permissions to start a VM session.|
|Get-VM||Retrieves a VMObject for each VM on a Hyper-V host.|
|Import-VM||Imports a VM from a given folder.|
|Measure-VM||Retrieves data about the resource utilization of a given VM, including processor and memory usage, network traffic and disk capacity.|
|Merge-VHD||Merges VHDs onto a Hyper-V in a differencing VHD chain.|
|Mount-VHD||Mounts one or more VHDs that are specified in the VHD files.|
|Move-VMStorage||Allows you to specify a path for moving a VM's storage.|
|New-VMResourcePool||Creates a resource pool on a Hyper-V host. The local computer is default.|
|Remove-VM||Deletes the configuration file for a given VM, but does not delete its VHDs.|
|Repair-VM||Restores a VM to a more stable state, using fixes found in a compatibility report.|
|Resize-VHD||Allows you to shrink or expand a VHD, although there is a minimum size.|
You can glean a lot just by reading through the list of cmdlet names on the Hyper-V cmdlets page at TechNet. I guarantee you that spending some time spelunking on this Web page will produce some useful leads for further investigation.
Additional PowerShell info of interest to VDI admins
PowerShell itself will happily show you how to use these scripts: simply type Get-Command –Module Hyper-V inside the PowerShell window, and you'll get a complete list of what's available for Hyper-V in the latest release.
You can search these cmdlets for particular verbs (such as add, copy, grant, get, mount, move, remove, resize and so forth) to look at them by category. All you need to do is to add -Name *search* to the preceding Get-Command input line, where you replace search with the actual string you're interested in (add, copy, grant and so forth) to see what's available. The Get-help cmdlet command accepts cmdlet names as input and will produce additional information by appending any of these additional switches to the end of a command line:
- -Examples: for code examples
- -Detailed: for verbose output
- -Full: everything available, just like in the TechNet resource
More on VDI automation
Guide to automation for virtualization
Performing virtualization automation with PowerShell
Weighing automation tools for virtualization provisioning
If you want to access the Hyper-V module inside PowerShell, you must turn it on first. Do so by entering this command string inside the PowerShell window (running with Administrative privileges):
Add-WindowsFeature Hyper-V -IncludeManagementTools
You also need to run the PowerShell console "as administrator" (right-click, then select Run as administrator from the pop-up menu) if you want to work with and manage Hyper-V. Once you're up and running, you'll find PowerShell an invaluable tool for managing VDI.
PowerShell 4.0 is so new that TechNet's documentation still hasn't caught up with that newly released code base. You'll have to grab a working version from the Windows Management Framework 4.0 Preview at TechNet. The 4.0 preview is said to be very, very close to the final version in terms of content, coverage, code and details.