Be Ready for Windows Server 2012 Hyper-V… With PowerShell

As an IT Pro, you might be reading this post and thinking “I hate command lines and that’s not gonna work for me”, but WAIT! Don’t close the webpage yet…  Well, you’re right… I also do hate commands (At least one thing you got to agree with me. We can save a lot of freaking work with them.). I’m not a developer and some of them need some developing skills, blah, blah, blah.

The good news is that Microsoft knows that too. PowerShell has been made for people like us, that are not familiar with scripting and need something easy to do the things we normally do via graphical interface using commandlets (Powershell commands).

My mission here is to prove that you can really be a Powershell specialist and do almost all your tasks on Hyper-V using commands. You think it is impossible??? Keep reading then…

The first thing, before we start, is to update the PowerShell help content. The examples provided by the PowerShell help is great and we will need it. Microsoft is always providing new content and information that can be downloaded and installed locally on your servers.

To do that, you just need to open a PowerShell window from your Hyper-V Server and run the command Update-Help. That’s it… All modules will be updated with the latest details. If you prefer, you can update only the Hyper-V module by typing Update-Help –Module Hyper-V.

Second step and tip: Use PowerShell ISE. This version provides a GUI console that helps you to identify spelling errors and command issues. Very handy.

Third and last tip before we start. Get to know the existing PowerShell commandlets for Hyper-V. By typing Get-Command –Module Hyper-V you can see a list of all commands related to the Hyper-V module. Don’t worry, you don’t need to decorate all of them. Well… you can try, but I don’t think it would be easy to decorate 162 commands in one go. Smile

You are ready to go then. Seated in front your computer with all set. But… where to start?

To get it even easier, let’s try to use some daily task you might have to do as a Hyper-V Administrator. What about a new virtual machine?

The secret here is to know how to find and use the command for your tasks. Using the example above, let’s try to find commandlets that can create NEW things.

Get-Command –Module Hyper-V –Name *New*

PS 01

This command will show you every commandlet that has the word New on it. From the list you can easily find the command New-VM. That’s what we are looking for.

If you are searching a particular noun or even a verb, you can also use the –noun or –verb option:

Get-Command –Noun VM

Get-Command –Verb Stop

Great, first secret done: Find the command. The next one is to know how to use it. That’s even easier!

Now that we know the command we need, let’s use the help to get more information about it.

Help New-VM

PS 02

“Hmmm, I can see all the information Leo… but I’m lazy. Too much things to read and understand”. Yeah we all are… But if you already know the command, the help content can give you some tip about improvements and tricks.

You can also filter the help results using the following examples:

Help New-VM –Detailed

Help New-VM –Full

Now, if you just want to go straight and execute the task as quick as possible, you just need to know one command, THE ONE command.

Help New-VM –Examples

PS 03

The –Examples switch can show what IT Pros are looking for… The command examples to copy, paste, change and run… Easy done.

Now, going a little bit deeper, let’s dig in the New-VM command. Have a look in this example:

New-VM –Name NewVM –MemoryStartupBytes 512MB –path C:\Hyper-V\VMs

You don’t have to be an expert to know what is going on there right? Just a new VM called NewVM with the memory startup bytes configured with 512MB created in the C:\Hyper-V\VMs path.

After reading that, those bloody IT Pro thoughts are polluting your mind again “But that’s too long, I can do that faster using GUI!”

What about, the same command, but with less details and almost the same result?

New-VM –Name NewVM

You think it still complicated? Just try New-VM then… It works too!

New-VM

Still not convinced? What about a GUI interface that you can use to build the whole command using nice lists and dropdown menus with a hot chocolate after the command execution?

Just type Show-Command and this beauty below will pop up.

Powershell 01

You need something easier? F&^$@ mate… get a trainee to do the job for you! He might get the coffee for you as well. Smile

These were a couple of examples of how you can explore and adopt PowerShell to automate your daily tasks without being a script expert. Not that hard right?

You like scripting? You think it was too easy? What about create a hundred of new VMs in just one shot?

$CreateVM = @(); (1..100) | %{ $CreateVM += New-VM -Name "NewVM$_" }

So who is faster now? Why don’t you try to create 100 new VM’s using the GUI?

You see? We can go from the dummies commands till advanced and nice tasks. After a couple of tests and practicing you will have PowerShell doing all the job for you.

This link also will help you: It has a list with all Hyper-V commands, with examples, explanations and available options: http://technet.microsoft.com/library/hh848559.aspx

Another very nice tool that works as an Add-on for PowerShell ISE is the Microsoft Script Explorer. You can use it to find tons of existing scripts on Microsoft and from the community. http://www.microsoft.com/en-us/download/details.aspx?id=29101

So, IT Pro fellas, not being a developer is not an excuse to use PowerShell anymore… Winking smile

 

Leandro Carvalho
MCSA+S+M| MCSE+S | MCTS | MCITP | MCBMSS | MCT | MVP Virtual Machine
My Blog | MSVirtualization (pt-BR) | LinhadeCodigo | MVP Profile

Twitter: LeandroEduardo | LinkedIn: Leandroesc

Follow

Get every new post delivered to your Inbox.

Join 38 other followers