Saturday 9 January 2010

Get-VMMemory

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays the virtual Memory assigned to Hyper-V 
  4.     VMs on a given host. 
  5. .DESCRIPTION 
  6.     This script uses The Hyper-V module from Codeplex to first 
  7.     get all the VMs on a given server, then display the memory  
  8.     allocated to each VM. 
  9. .NOTES 
  10.     File Name  : get-VMMemory.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 
  13.                  and HyperV module from Codeplex 
  14.                  http://pshyperv.codeplex.com 
  15. .LINK 
  16.     This script posted to: 
  17.         http://www.pshscripts.blogspot.com 
  18. .EXAMPLE 
  19.     PSH [C:\Foo]: c:\foo\get-VMMemory.ps1 Foo21 
  20.     On Server Foo21 there are 2 VMs as follows: 
  21.     Virtual Machine                       Memory 
  22.     SQL-2008-R2-CTP                          768 
  23.     Server-2008-R2                           768 
  24. .EXAMPLE 
  25.     PSH [C:\Foo]: "Server2" | c:\foo\get-VMMemory.ps1 Foo21 
  26.     On Server Server2 there are 3 VMs as follows: 
  27.     Virtual Machine                       Memory 
  28.     Server-2008-R2-1                         768 
  29.     Server-2008-R2-2                         768 
  30.     Server-2008-R2-3                         768 
  31. #> 
  32.  
  33. # Parameter is the machine to look at 
  34. param
  35. [Parameter(Position=0, Mandatory=$false,ValueFromPipeLine=$true)]  
  36. [string] $Computer = "MyServer" 
  37. ## 
  38. # Start of script 
  39. ## 
  40.  
  41. # Import the module 
  42. Import-Module HyperV 
  43.  
  44. # Get all the VMs on the target system 
  45. $Vms = Get-Vm -Server $Computer 
  46.  
  47. # Now loop through the VMs and display memory allocated 
  48. "On Server {0} there are {1} VMs as follows:" -f $Computer,$Vms.count 
  49. "{0,-30}    {1,10}" -f "Virtual Machine", "Memory" 
  50. Foreach ($Vm in $Vms) { 
  51. $Mem = Get-Vmmemory $Vm
  52. "{0,-30}    {1,10}" -f $Vm.Elementname,$Mem.Virtualquantity 
  53. }  
  54. # End of script 

No comments: