15 April 2014

Hyper-V and SCVMM Powershell Cheat Sheet

PowerShell integration with both native Hyper-V management and extended through VMM is great.  It can be a real time shaver as long as you don't have to figure out or stick together all the PowerShell commands.
I found the best way of work to simply be opening the PowerShell ISE and run and edit the commands from there.   This article will be updated as and when I find useful commands.

Find Virtual machines with checkpoints
Checkpoint is a fantastic feature but they have a very limited scope of where they SHOULD be used.

Get-SCVirtualMachine |where {$_.VMCheckpoints -ne " "} |select name ,vmcheckpoints

Network Enhancements

Disable VMQ on multiple hosts

This is a handy command to disable VMQ for the VM data network adapters or teams.  This prevents VMQ driver issues from impacting VM network performance.  I found it only worked successfully if you disable the NICs AND the team.

Invoke-Command -ComputerName Hyperv06,Hyperv07,Hyperv08,Hyperv09,Hyperv10,Hyperv11,Hyperv12,Hyperv13 -ScriptBlock {disable-NetAdapterVmq "NIC6 ", "NIC7", "VM-Traffic-Team"}

Dynamic Memory
Dynamic memory is a great way to increase the VM density on a host.  It is also a primary target for trying to troubleshoot performance issues per VM

Get a list of Virtual machines and memory information

Get-SCVirtualMachine | select Name,DynamicMemoryEnabled, Memory, DynamicMemoryMinimumMB , DynamicMemoryMaximumMB , DynamicMemoryDemandMB, DynamicMemoryBufferPercentage |Sort-Object name |Format-Table -AutoSize

Get a list of virtual machines that do not have dynamic memory enabled

Get-SCVirtualMachine | where {$_.DynamicMemoryEnabled -eq $false} | Sort-Object name| select Name,DynamicMemoryEnabled, Memory, DynamicMemoryMinimumMB , DynamicMemoryMaximumMB , DynamicMemoryDemandMB, DynamicMemoryBufferPercentage  |Format-Table -AutoSize

To change a VM from static to dynamic memory.  Just specify the name and the memory values

$vm = Get-SCVirtualMachine -Name Win2012r2Stop-SCVirtualMachine -VM $vm -ShutdownSet-SCVirtualMachine -VM $vm -DynamicMemoryEnabled $True -DynamicMemoryBufferPercentage 40 -DynamicMemoryMaximumMB 8192 -DynamicMemoryMinimumMB 1024 -MemoryMB 4096Start-SCVirtualMachine -VM $vmGet-SCVirtualMachine -Name $vm | select Name,DynamicMemoryEnabled, Memory, DynamicMemoryMinimumMB , DynamicMemoryMaximumMB , DynamicMemoryDemandMB, DynamicMemoryBufferPercentage |Sort-Object name |Format-Table -AutoSize

Automating VM moves between stand alone hosts
When dealing with non clustered hosts you can make use of the share nothing live migration feature to essentially give you a similar experience to HA vm during things like host patching.  This typically involves three steps.  The first step is to drain all the VM from the first host, path and reboot the host.  Second step is to move all the VM's to the second host and the path and reboot the first host.  Third step is to load balance across the server by splitting the amount of vm to be equal on each host.

Step one and two would use this command but just reversing the order of the hosts.

Get-SCVirtualMachine -VMHost hyperv02 | Move-SCVirtualMachine -VMHost hyperv01 -HighlyAvailable $false -UseLAN -UseDiffDiskOptimization -Path "D:\vmstorage" -RunAsynchronously

Step three moves half of the VMs from one host to another.

$amount = Get-SCVirtualMachine -VMHost hyperv01 $unit = $amount.Count$half = $unit / 2$vmnames = Get-SCVirtualMachine -VMHost hyperv01 |select name -First $half
$vmnames | ForEach-Object {Move-SCVirtualMachine -VM $_.name -VMHost hyperv02 -HighlyAvailable $false -UseLAN -UseDiffDiskOptimization -Path "D:\vmstorage" } 
Host reboot
When you have a non HA host, VMs will not automatically be moved to another host when the host is shut down or rebooted.  The action that the VM will take is determined by the start-up setting on the VM itself.

To check the current settings for all the VMs on a host use

Get-SCVirtualMachine -VMHost hyperv03 |select name, stopaction, startaction

The most seamless way of getting a VM to return, is the save the state and to resume the VM

Get-SCVirtualMachine -VMHost hyperv03 | Set-SCVirtualMachine -StartAction TurnOnVMIfRunningWhenVSStopped -StopAction SaveVM

The only problem with this is that to perform the save state the RAM is committed to a file written to the same location as the hot's default VM path.  If there is a constraint there you will have failures during host shutdown.

Certain machines should also not be saved as it could cause issues. an example of this would be a domain controller.

To shut down the machines and start them up again use the following.

Get-SCVirtualMachine -VMHost hyperv03 | Set-SCVirtualMachine -StartAction TurnOnVMIfRunningWhenVSStopped -StopAction ShutdownGuestOS

This will also trigger certain non Microsoft VMs to shut down.

Failover Hyper-V clusters are most commonly configured with Cluster Shard Volumes CSV as the storage

Retrieve a list of all VMs that reside on a particular CSV volume

For a single host

Get-SCVirtualMachine -VMHost hyperv03 | select name, location | where {$_.location -match "volume2"}

For Multiple hosts

$hst = "hyperv06", "hyperv07", "hyperv08", "hyperv09", "hyperv10", "hyperv11","hyperv12", "hyperv13"
$hst | ForEach-Object {Get-SCVirtualMachine -VMHost $_ | select hostname,name, location  | where {$_.location -match "volume5"} } |  Sort-Object name

Move HA VM from one host to another

Move-SCVirtualMachine -VM  vmname -VMHost hostname

Move storage of all VMs on a host on a volume to another

$sourcehost = "hyperv08"
Get-SCVirtualMachine -VMHost $sourcehost | where {$_.location -match "volume5"} | foreach-object {Move-SCVirtualMachine  -VM $_.name -VMHost $sourcehost -Path C:\ClusterStorage\Volume2}


No comments:

Post a Comment