Warning: The following post was written by a scripting luddite. The author readily admits that he would have difficulty coding his way out of a paper bag, and if the fate of the world depended on his ability to either write code or develop software then you had better start hoarding bottled water and cans of tuna. Fortunately for everyone, there are heroes to help him!
I love the Graphical User Interface (GUI). I use it every day in both the Windows client and Windows Server operating systems. It makes my life easier on a day to day basis.
With that being said, there are several tasks that administrators must do on a regular basis. There is no simple and reliable way to create repetitive task macros in the GUI. Hence we can either work harder, or we can learn to use scripting tools like Windows PowerShell.
Along the way I have gotten some help from some friends. Ed Wilson’s books have provided a wealth of information for me, and Sean Kearney has been my go-to guy when I need help. There was a time when I was teaching a class and was asked ‘Can PowerShell do that?’ I replied by saying that if I asked Sean Kearney to write a PowerShell script to tie my shoes, I was reasonably sure he could do it because PowerShell can do ANYTHING. Well one of my students posted that comment on Twitter, and got the following reply from Sean (@EnergizedTech):
Get-Shoe | Invoke-Tie
It makes sense too…because PowerShell works with a very simple Verb-Noun structure, and if you speak English it is easy to learn.
I may be a scripting luddite, but I do know a thing or two about virtualization, and especially Hyper-V. So it only stands to reason that if I was going to start learning (and even scarier, teaching) PowerShell, I would start with the Hyper-V module. As a good little Microsoft MVP and Community Leader, it only makes sense that I would take you along for the ride🙂
Most of what can be done in PowerShell can also be done in the GUI. If I want to see a list of the virtual machines on my system, I simply open the Hyper-V Manager and there it is.
PowerShell is almost as simple… Type Get-VM.
By the way you can filter it… if you only want virtual machines that start with the letter S, try:
One of the advantages of PowerShell is that it allows you to manage remote servers, rather than having to log into them you can simply run scripts against them. If you have a server called SWMI-Host1, you can simply type:
Get-VM –Server SWMI-Host1
Starting and stopping virtual machines is simple…
Again, your wildcards will work here:
This command will start all VMs that start with the letter O.
If you want to check how much memory you have assigned to all of your virtual machines (very useful when planning as well as reporting) simply run the command:
I did mention that you could use this command for reporting… to make it into an HTML report run the following:
Get-VMMemory * | ConvertTo-HTML | Out-File c:\VMs\MemReport.htm
Get-VMMemory * | ConvertTo-CSV | Out-File c:\VMs\MemReport.csv
The report created is much more detailed than the original screen output, but not so much so as to be unusable. See:
So far we have looked at VMs, we have started and stopped them… but we haven’t actually made any changes to them. Let’s create a new virtual machine, then make the changes we would make in a real world scenario.
New-VM –Name PSblog –MemoryStartupBytes 1024MB –NewVHDPath c:\VHDs\PSblog.vhdx –NewVHDSizeBytes 40GB –SwitchName CorpNet
With this simple script I created a virtual machine named PSblog with 1024MB of RAM, a new virtual hard disk called PSblog.vhdx that is 40GB in size, and connected it to CorpNet.
Now that will work, but you are stuck with static memory. Seeing as one of the great features of Hyper-V is Dynamic Memory, let’s use it with the following script:
Set-VMMemory –VMName PSblog –DynamicMemoryEnabled $true –MinimumBytes 512MB –StartupBytes 1024MB MaximumBytes 2048MB
Now we’ve enabled dynamic memory for this VM, setting the minimum to 512MB, the maximum to 2048MB, and of course the startup RAM to 1024MB.
For the virtual machine we are creating we might need multiple CPUs, and because some of our hosts may be newer and other ones older we should set the compatibility mode on the virtual CPU to make sure we can Live Migrate between all of our Intel-based hosts:
Set-VMProcessor –VMName PSblog –Count 4 –CompatibilityForMigrationEnabled $true
At this point we have created a new virtual machine, configured processor, memory, networking, and storage (the four food groups of virtualization), and are ready to go.
I will be delving deeper into Hyper-V management with PowerShell over the next few weeks, so stay tuned!
NOTE: While nothing in this article is plagiarized, I do want to thank a number of sources, on whose expertise I have leaned rather heavily. Brien Posey has a great series of articles on Managing Hyper-V From the Command Line on www.VirtualizationAdmin.com which is definitely worth reading. He focuses on an add-on set of tools called the Hyper-V Management Library (available from www.Codeplex.com) so many of the scripts he discusses are not available out of the box, but the articles are definitely worth a read. Rob McShinsky has an article on SearchServerVirtualization (a www.TechTarget.com property) called Making sense of new Hyper-V 2012 PowerShell cmdlets which is great, and links to several scripts for both Server 2008 R2 and Server 2012. Thanks to both of them for lending me a crutch… you are both worthy of your MVP Awards!