Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ?#requires -Version 1
- # Allows easy connection to a particular vCenter in your VMware environment with multiple vCenters
- function Get-vCenter
- {
- Write-Verbose -Message 'Please specify which vCenter you want to connect to' -Verbose
- $Global:vCenters = $NULL #Can be used outside of function to refer to the correct vCenter
- $IvCenters = $null
- $i = $null
- $DvCenters = $null
- $IvCenters = 'vCenter1.mydomain.com', 'vCenter2.mydomain.com', 'vCenter3.mydomain.com'
- $i = 1
- $IvCenters | %{Write-Host $i":" $_; $i++}
- $DvCenters = Read-host 'Enter the number next to the vCenter you wish to connect to'
- $Global:vCenters = $IvCenters[$DvCenters -1]
- ' '
- ' '
- write-verbose -Message "The vCenter you have selected is $Global:vCenters" -Verbose
- ' '
- ' '
- ' '
- ' '
- ' '
- $title = 'vCenter'
- $message = "Is [$Global:vCenters] the correct vCenter?"
- $yes = New-Object System.Management.Automation.Host.ChoiceDescription '&Yes', `
- 'The vCenter choice is correct. Proceed to next step'
- $no = New-Object System.Management.Automation.Host.ChoiceDescription '&No', `
- 'The vCenter choice is not correct. Repeat previous step'
- $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
- $result = $host.ui.PromptForChoice($title, $message, $options, 0)
- switch ($result)
- {
- 0 {'You selected Yes.'}
- 1 {'You selected No.'}
- }
- ' '
- If ($result -eq 0)
- {
- Write-Verbose -Message "You have chosen [$Global:vCenters] as the vCenter to connect to" -Verbose
- ' '
- ' '
- ' '
- ' '
- ' '
- }
- ElseIf ($result -eq 1)
- {
- Clear-Variable -Name vCenters -Scope Global
- Clear-Variable -Name result
- Get-vCenter
- }
- Connect-VIServer -Server $Global:vCenters -Force
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement