GetVI-HostList


posted by Richard Giles
10-06-2008

Downloads: 372
File size: 2.6kB
Views: 1,083

Embed
GetVI-HostList
  1. ## ===================================================================== 
  2. ## Title       : GetVI-HostList 
  3. ## Description : Retrieve VMware Hosts 
  4. ## Author      : Idera 
  5. ## Date        : 9/11/2008 
  6. ## Input       : -VIserver : Virtual Infrastructure server 
  7. ##                -verbose -debug    
  8. ## Output      : VMs 
  9. ## Usage       : PS> . GetVI-HostList 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-HostList $VIserver  
  23.  
  24. function GetVI-HostList([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. `n`n`n`n`tPress <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.         ## Display the list of Hosts found on the network. 
  60.         Get-VMHost 
  61.         Read-Host "`n`n`t Press <Enter> to continue " 
  62.     
  63. } ## EOF: GetVI-HostList() 
  64.  
  65. function CheckVIToolKit() 
  66.     ## Before we do anything we must check to see if the user has the VI toolkit installed. 
  67.     ## If user does not then we prompt the user and exit. 
  68.     $Error.Clear() 
  69.     Get-PSSnapin vmware* 
  70.     if($Error.Count -ne 0) 
  71.     
  72.         Clear-Host 
  73.         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 
  74.         Write-Host "`t`t go to the Settings menu in Powershell Plus and click on Manage Snapins." -foregroundcolor red -backgroundColor yellow 
  75.         Read-Host  "`n`n`t Press <Enter> to continue." 
  76.         Clear-Host 
  77.         break 
  78.     
  79. }## EOF: CheckVIToolKit() 
  80.  
  81. ## Run Main 
  82. main 
Filed under: , ,

Retrieve VMware Hosts from a Virtual Instructure Server using the VI Tookkit 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.