Wednesday, May 22, 2013

Hyper-V PowerShell


UPDATE … more from Bill Baer
http://blogs.technet.com/b/wbaer/archive/2014/03/21/quick-starting-demos-with-windows-powershell.aspx
Learning to use PowerShell to manage your Hyper-V environment?

Simple Management

First type Get-vm (note if module is not loaded type import-module hyperv)

Now I notice a column for state so I just want the running vms so
type Get-vm | where {$_.state –eq ‘running’} (older PS)
type Get-vm | state –eq ‘running’ (using new PS3.0)

How I need to shutdown the running vm’s so
type Get-vm | state –eq ‘running’ | stop-vm

Start VM’s

So you need to start a VM first get a list
get-vm

Then sort based on name using like or contains.  You can use -like for wildcards, –contain for exact matches, or –ccontain for case sensitive exact matching. 

For my scenario, it is best to use –like in my where cmdlet.
get-vm | where name –like ‘*10’ (PS3.0)

Now just start em up in order, DC, DB, and finally SP, but wait you need to wait before going to the next vm.

So I finally verify for the VM heartbeat before I start another VM.


Here are more optional views

Now lets connect.

Simple VM Connection

So you need to connect to your vm?  Just just get a list of vms you want to start.  Save as variable and then using vmconnect connect to all in one step.

However since this is an array we need to step through each element in our array

Have fun with your VM’s

Stop VM’s

So just as before when we started just get-vm and then pipe to vm-stop.  Note that I filtered out just the running vm’s

VM Snapshot

So you need to snapshot several VM at one time? 
No problem, just pipe your vm’s to Checkpoint-vm

Note above that I listed the VM’s and then showed the syntax to verify the snapshots.
Now lets give the snapshots a name just add –snapshotname ‘string’

Now that fun …

Simple Network Configuration

Now I need to connect all the vm’s to a common switch.  So let’s look at a new command to show vm network settings.  See here that all the vm’s are connected to Private.
type Get-vm | Get-VMNetworkAdapter

I need a list of Virtual Switches, here’s how.
type Get-VMSwitch

I need to connect all the VM’s to a Demo Switch, here’s how.
type Get-vm | Get-VMNetworkAdapter | Connect-VMNetworkAdapter –SwitchName ‘Demo’
Now displaying all VM’s attached to Demo
type Get-vm | Get-VMNetworkAdapter | where SwitchName –eq ‘Demo’

VM Settings Administration

Now you need to set a setting for all your vm’s at once, no problem.
First list your vm’s, then pipe that list to set-vm.  Here is a list of settings, I am going to set the startup memory on all my vm’s with sp in the name.

References:

Hyper-V: Scripts
http://social.technet.microsoft.com/wiki/contents/articles/176.hyper-v-scripts.aspx
Hyper-V Settings
http://technet.microsoft.com/en-us/library/hh848575(v=wps.620).aspx
Orderly Shutdown from Scripting Guy
http://blogs.technet.com/b/heyscriptingguy/archive/2013/02/21/use-powershell-to-perform-an-orderly-shut-down-of-a-hyper-v-server.aspx
Guide to Hyper-V PowerShell (Technet)
http://technet.microsoft.com/en-us/library/hh848559(v=wps.620).aspx
VM Report Building
http://blog.basefarm.com/blog/2013/01/25/basic-inventory-of-hyperv-virtual-machines-using-powershell/
Virtual Switch
http://technet.microsoft.com/en-us/library/jj679878.aspx
Matching
http://technet.microsoft.com/en-us/library/ee692798.aspx
Kewl Article on Top Ten List on Hyper-v cmdlets
http://www.altaro.com/hyper-v/10-awesome-hyper-v-cmdlets/

MCT Hints

Delete VMs and cleanup from class
http://cursurimicrosoft.com/index.php/home/delete-all-vm-from-the-previous-moc-course-using-powershell/