GetVI-VMList


posted by Richard Giles
10-06-2008

Downloads: 410
File size: 3kB
Views: 1,841

Embed
GetVI-VMList
  1. ## ===================================================================== 
  2. ## Title       : GetVI-VMList 
  3. ## Description : Retrieve VMware VMs 
  4. ## Author      : Idera 
  5. ## Date        : 9/11/2008 
  6. ## Input       : -VIserver : Virtual Infrastructure server 
  7. ##                -verbose -debug    
  8. ## Output      : VMs 
  9. ## Usage       : PS> . GetVI-VMList myVIserver -v -d 
  10. ## Notes       :  
  11. ## Tag         : PowerShell, VMware 
  12. ## ===================================================================== 
  13. param 
  14.    [string]$VIserver
  15.    [switch]$verbose
  16.    [switch]$debug 
  17.  
  18. function main() 
  19.    if ($verbose) {$VerbosePreference = "Continue"
  20.    if ($debug) {$DebugPreference = "Continue"
  21.    CheckVIToolKit 
  22.    GetVI-VMList $VIserver  
  23.  
  24. function GetVI-VMList([string]$VIserver = ""
  25.     ## Clear Screen. 
  26.     Clear-Host 
  27.      
  28.     ## Warn user they must be logged on with an account that has connection rights to the VI server. 
  29.     $answer = Read-Host "`n`n`n`tNOTE: You must be logged on with an account that has connection rights to the VI server. Press <X> to exit, <C> to continue if you have a connection to a VI Server already or <Enter> to continue. " 
  30.     if($answer -eq "x"
  31.     
  32.         break 
  33.     
  34.     else 
  35.     
  36.         if($VIserver -eq ""
  37.         
  38.             Write-Host "`n`n`n`tERROR- A VI server must be specified!`n`n" -foregroundcolor red 
  39.             Read-Host "`n`t Press <Enter> to continue " 
  40.             break 
  41.         
  42.         else 
  43.         
  44.             if($answer -eq "C"
  45.             
  46.                 ## do nothing as we have a connection we can use already.. 
  47.             
  48.             else 
  49.             
  50.                 Connect-VIServer $VIserver 
  51.                 Write-Host "`n`n`t You have connected to Server: $VIserver.Name with a SessionID of: $VIserver.SessionID ." 
  52.                 Read-Host "`n`t Press <Enter> to continue " 
  53.             
  54.         
  55.          
  56.         ## Clear Screen. 
  57.         Clear-Host 
  58.          
  59.         ## List the servers 
  60.         $answer = Read-Host "`n`n`t Enter (S) to specify a VM or (L) to display a list of all VM's " 
  61.             if($answer -eq "S"
  62.             
  63.                 $answer = Read-Host "`n`n`t Enter the name of VM to display " 
  64.                 Get-VM $answer | Format-Table -autosize 
  65.                 Read-Host "`n`n`t Press <Enter> to continue " 
  66.             
  67.             elseif($answer -eq "L")              
  68.             
  69.                 Get-VM | Format-Table | more 
  70.                 Read-Host "`n`n`t Press <Enter> to continue " 
  71.             
  72.             else 
  73.             
  74.                 Read-Host "`n`n`t Invalid option entered, Press <Enter> to continue " 
  75.                 break 
  76.             }     
  77.     
  78. } ## EOF: ConnectVI-Server() 
  79.  
  80. function CheckVIToolKit() 
  81.     ## Before we do anything we must check to see if the user has the VI toolkit installed. 
  82.     ## If user does not then we prompt the user and exit. 
  83.     $Error.Clear() 
  84.     Get-PSSnapin vmware* 
  85.     if($Error.Count -ne 0) 
  86.     
  87.         Clear-Host 
  88.         Write-Host "`n`n`t`t ERROR - To run this script, the VI Toolkit must be installed and registered with Powershell. If the VI Tollkit is installed," -foregroundcolor red -backgroundColor yellow 
  89.         Write-Host "`t`t go to the Settings menu in Powershell Plus and click on Manage Snapins." -foregroundcolor red -backgroundColor yellow 
  90.         Read-Host  "`n`n`t Press <Enter> to continue." 
  91.         Clear-Host 
  92.         break 
  93.     
  94. }## EOF: CheckVIToolKit() 
  95.  
  96. ## Run Main 
  97. main 
Filed under: , ,

Retrieve VM List from a Virtual Infrastructure Server using VI Toolkit PowerShell snapin.

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.