SHARE
TWEET

Get-vCenter

jmeg8r May 7th, 2018 (edited) 1,863 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ?#requires -Version 1
  2.  
  3.  
  4. # Allows easy connection to a particular vCenter in your VMware environment with multiple vCenters
  5.  
  6.  
  7.  
  8. function Get-vCenter
  9. {
  10.    
  11.     Write-Verbose -Message 'Please specify which vCenter you want to connect to' -Verbose
  12.    
  13.     $Global:vCenters = $NULL #Can be used outside of function to refer to the correct vCenter
  14.  
  15.     $IvCenters = $null
  16.     $i = $null
  17.     $DvCenters = $null
  18.  
  19.  
  20.     $IvCenters = 'vCenter1.mydomain.com', 'vCenter2.mydomain.com', 'vCenter3.mydomain.com'
  21.     $i = 1
  22.     $IvCenters | %{Write-Host $i":" $_; $i++}
  23.     $DvCenters = Read-host 'Enter the number next to the vCenter you wish to connect to'
  24.     $Global:vCenters = $IvCenters[$DvCenters -1]
  25.     ' '
  26.     ' '
  27.     write-verbose -Message "The vCenter you have selected is $Global:vCenters" -Verbose
  28.     ' '
  29.     ' '
  30.     ' '
  31.     ' '
  32.     ' '
  33.    
  34.     $title = 'vCenter'
  35.     $message = "Is [$Global:vCenters] the correct vCenter?"
  36.  
  37.     $yes = New-Object System.Management.Automation.Host.ChoiceDescription '&Yes', `
  38.         'The vCenter choice is correct. Proceed to next step'
  39.  
  40.     $no = New-Object System.Management.Automation.Host.ChoiceDescription '&No', `
  41.         'The vCenter choice is not correct. Repeat previous step'
  42.  
  43.       $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
  44.  
  45.     $result = $host.ui.PromptForChoice($title, $message, $options, 0)
  46.  
  47.     switch ($result)
  48.         {
  49.             0 {'You selected Yes.'}
  50.             1 {'You selected No.'}
  51.         }
  52.    
  53.     ' '
  54.  
  55.         If ($result -eq 0)
  56.     {
  57.         Write-Verbose -Message "You have chosen [$Global:vCenters] as the vCenter to connect to" -Verbose
  58.         ' '
  59.         ' '
  60.         ' '
  61.         ' '
  62.         ' '
  63.  
  64.     }
  65.     ElseIf ($result -eq 1)
  66.     {
  67.         Clear-Variable -Name vCenters -Scope Global
  68.         Clear-Variable -Name result
  69.         Get-vCenter
  70.     }
  71.  
  72.     Connect-VIServer -Server $Global:vCenters -Force
  73.  
  74.  
  75. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top