About the book
Written by: Richard Siddaway
Pages: 500
Publisher: ManningISBN-10: 1935182005
ISBN-13: 978-1935182009
Get 30% discount
DotNetSlacker readers can get 30% off the full print book or ebook at www.manning.com using the promo code
dns30 at checkout.
Introduction
Virtualization is the technique of hosting a number of virtual servers on a single physical machine. Introduced to the mainframe world over 40 years ago, it has become a major component of Windows based infrastructures in the last few years. Virtualization reduces the number of physical machines we need to administer but increases the overall total of machines as we now have to administer the host machine as well as the virtual machines. PowerShell becomes even more necessary.
There are a number of virtualization technologies available. What is even better is that we can use PowerShell with most of them. VMware is the one most people will think of first. They have released a PowerShell snapin for managing their environment. Some excellent information on using PowerShell with VMware can be found on the blogs of Hal Rottenberg, Alan Renouf and Jonathan Medd. A PowerGUI power pack is also available that uses the VMWare cmdlets.
Microsoft has Virtual Server for which Ben Pearce has posted a number of PowerShell scripts. Windows 2008 introduced Hyper-V. These can be managed with System Center Virtual Machine Manager.
An alternative for Hyper-V is to use the Hyper-V PowerShell library of functions that can be found on codeplex. Written by James O'Neill it is a free download. The zip file will need to be unblocked before extraction otherwise PowerShell will keep asking for permission to run the scripts. The zip file contains two files:
- hyperv.format.ps1xml
- hyperv.ps1
Hyperv.ps1 is a library of functions that is based on WMI. Run PowerShell with elevated privileges and dot source the file
. ./hyperv.ps1
This will load the functions and update the format data using the hyperv.format.ps1xml file. This is a good example of a format file if you need to create your own.
Functions vs ModulesThe Hyper-V library is currently a set of functions. This makes it usable with PowerShell v1 and v2. PowerShell v2 introduces the concept of modules which make the use of function libraries more dynamic. Hopefully, the library will be upgraded to a module when PowerShell v2 becomes the norm.
Now we have loaded the functions let's see how to use them.
Discovering Hyper-V functions
First thing we need to know is what functions are available. There is a help file in pdf format that is available for download but it may not always be accessible. Often we only need a reminder of the name. PowerShell v2 includes function names in tab completion.
Problem
We need to know which functions have been loaded by the Hyper-V library.
Solution
The function provider can be used to access the information.
Listing 1: Discover Hyper-V functions
Discussion
The Hyper-V library is based on WMI. We can view the WMI classes. It's sometimes more useful to access WMI directly but the functions make life easier. The obvious way to access the function names is to interrogate the function provider using Get-ChildItem. However, we cannot use a filter on the function provider. Quick step sideways and we can use where instead of a filter. If we just select the name we can reduce the output to an amount that is easily viewable.
There are some 80 functions in total. In PowerShell v2 get-verb creeps in just to be confusing. If we want to see the code for a particular function we can view the definition property which holds the code. If we want to see more than one function's worth then open the library file in an editor.
Let's start by using the functions to view the status of our virtual machines.
Virtual Machine Status
Before we can do any work with our virtual machines we need to know their status. Are they running? Can we switch them on from PowerShell? PowerShell takes fewer resources than the Hyper-V manager and using a Remote Desktop Connection provides a better experience than connecting from the GUI.
Problem
The status of virtual machines has to be changed, i.e. stop and start. In addition we need to be able to view the status of our VMs
Solution
We can test the status using two functions from the Hyper-V library.
Listing 2: Test Virtual Machine status
Discussion
Starting a virtual machine under Hyper-V involves Start-VM. 1 The name of the VM is provided as a parameter. A server parameter can be supplied to all of the functions to work with a remote system. Remember that the library is based on WMI so remote administration is built in.
FirewallNetsh must be used to configure the firewall on our servers to allow remote administration via WMI.
Ping-VM can be used as a quick check on our VMs. If we need to check more servers the name, and server, could be put into a csv file. One thing that isn't shown is the uptime which is a statistic that seems to fascinate managers for some reason. We can access this through Get-VM which also shows the state of the VM.
I use virtual machines a lot for experimenting with technologies and preparing demos. It is useful to be able to stop and start machines. We can use Shutdown-VM to cleanly close down the virtual machine.
As well as using the functions from the command line we can incorporate them into our scripts.
VM uptime
Formatting of output has been an issue since the first days of computing. PowerShell gives us access to the properties of an object so that we can easily modify how we display the data.
Problem
The uptime reported by Get-VM is in milliseconds. Working at this scale is not intuitive so I want to view the uptime in a more easily understood format.
Solution
We can write a function that displays the uptime in a more easily understood format.
Listing 3: Determine uptime of Virtual Machine
Discussion
We'll do this as a function so that it is always available. The function accepts a server name as a parameter. It then uses Get-VM to retrieve the information about the virtual machine. We can create a timespan object using the using the OnTimeInMilliseconds (uptime) property. Write-Host is used to display the data as hours:minutes;seconds.
The function is loaded by dot sourcing it and can be accessed from the command line or within a script by passing the name of a virtual machine. If required the format file that ships with the library could be modified to output the uptime in this format.
Virtual machines have virtual hard disks. Which while it sounds like the beginning of a nursery rhyme means that we have another object to investigate and test.
Check disk status
One of PowerShell's strengths is its composable nature. This means we can take pieces of functionality and easily combine them to provide a more sophisticated outcome. We can use Get-VMDisk to view all the virtual hard disks associated with our Hyper-V server. The status of individual disks can be checked using Test-VHD.
Problem
We need to view the status of all the virtual hard disks known to our Hyper-V server.
Solution
We have to combine a couple of functions to do this.
Listing 4: Check Virtual Disk status
Get 30% discount
DotNetSlacker readers can get 30% off the full print book or ebook at www.manning.com using the promo code
dns30 at checkout.
Discussion
Start by reading all of the disk information in an array using Get-VMDisk. We can then iterate through the disks testing each in turn. The results are added to the disk object as a NoteProperty. We can then use Format-Table to display the data.
Adding a property in this manner is a simple way to carry data through the rest of the script for future display or processing. It is especially useful as we don't need to worry about keeping track of extra variables.
In a similar way we can test the disk sizes.
Check disk usage
Disk space is always an issue. We need to be able to monitor the size of the virtual hard drives. If they become too large we may need to investigate as running out of disk space on the volumes that host the vhd files would be generally considered a bad thing to happen.
Problem
We need to examine the space used by our vhd files.
Solution
This time we combine Get-VHDInfo with Get-VMDisk.
Listing 5: Check Virtual Disk capacity
Discussion
Use Get-VMDisk to retrieve the list of disks. A foreach cmdlet is used to pass the disk path of each disk into Get-VHDInfo. Format-Table displays the disk and size information.
Compact disks
Virtual hard disks can be compacted to reclaim unused disk space. Ideally we would combine this with defragmenting. We can mount the vhd file in the file system of the Hyper-V server. We can use the standard Windows defragmentation tool to remove the file fragmentation. Compacting the disks has to be performed when the virtual machine is switched off. If this is a production machine we need to schedule this as part of the standard maintenance window. Doing this during business hours won't win many friends.
Problem
We need to compact the hard disks of our virtual machines.
Solution
Compact-VHD provides the answer.
Listing 6: Compact a Virtual Disk
Discussion
Get-VMDisk supplies a list of disks that we pipe into a foreach. Compact-VHD takes the path to the disk as a parameter and starts a job (not a PowerShell job) to perform the compaction. We can check status with
Get-WmiObject -NameSpace root\virtualization msVM_storagejob | ft jobStatus, description, percentcomplete -auto
There is a large number of other tasks we can perform to manage our Hyper-V environment. We can combine other functions from the Hyper-V library to complete these tasks.
About Manning Publications
 |
Manning Publication publishes computer books for professionals--programmers, system administrators, designers, architects, managers and others. Our focus is on computing titles at professional levels. We care about the quality of our books. We work with our authors to coax out of them the best writi...
This author has published 33 articles on DotNetSlackers. View other articles or the complete profile here.
|
You might also be interested in the following related blog posts
12 ASP.NET MVC Best Practices
read more
Dont Repeat Yourself
read more
Unit Testing - Do Repeat Yourself
read more
Practice, Code Exercises, and Code Katas
read more
Azure July CTP
read more
Opera files antitrust complaint with the EU against Google Chrome OS
read more
Lets build an Application Together.
read more
Technology Related Links for May 4th, 2009
read more
Code Sample Taxonomy
read more
What is this Open Cloud Manifesto...anyways???
read more
|
|
Please login to rate or to leave a comment.