Monday 20 September 2010

Get-VirtualMachine.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script gets the list of Virtual Machines on a Hyper-V Host 
  4. .DESCRIPTION 
  5.     This script uses WMI to get the VMs defined on a Hyper-V host 
  6.     then displays them. 
  7. .NOTES 
  8.     File Name  : Get-VirtualMachine.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINK 
  12.     This script posted to: 
  13.         http://www.pshscripts.blogspot.com 
  14.     MSDN sample posted tot 
  15.         http://msdn.microsoft.com/en-us/library/cc136822%28VS.85%29.aspx 
  16. .EXAMPLE 
  17.     PSH [c:\foo\: Get-VirtualMachine  
  18.     5 Virtual Machines on: COOKHAM2 
  19.  
  20.     PSMC-DC1 
  21.     PSMC-EXCH1 
  22.     PSMC-SQL 
  23.     PSMC-SRV1 
  24.     PSMC-SRV2 
  25. #> 
  26.  
  27. # Get list of VMs from WMI 
  28. $vmbase = get-wmiobject Msvm_ComputerSystem -namespace root\virtualization -ComputerName Cookham2 
  29.  
  30. # Get hosting computer System Name 
  31.  
  32. $HostName = $vmbase | ? {$_.Caption -eq "Hosting Computer System"} | select name 
  33.  
  34. # Print results 
  35. "{0} Virtual Machines on: {1}" -f $($vmbase.count-1),$Hostname.name 
  36. $vmbase | where {$_.Caption -ne "Hosting Computer System"} | sort elementname | ft elementname -HideTableHeaders 
Technorati Tags: ,,,,

No comments: