Skip to content

Instantly share code, notes, and snippets.

@abix-
Last active August 29, 2015 14:24
Show Gist options
  • Save abix-/58f090dfd9a90b89cc61 to your computer and use it in GitHub Desktop.
Save abix-/58f090dfd9a90b89cc61 to your computer and use it in GitHub Desktop.
How can I login to vSphere and PowerCLI faster?
function cvi([switch]$client) {
#Read vcenters.csv and draw menu
$vcenters = Import-Csv "C:\Scripts\vcenters.csv"
Write-Host "Select a server from the list"
for($i = 1; $i -lt $vcenters.count + 1; $i++) { Write-Host "[$i] $($vcenters[$i-1].vCenter)" }
#Switch $option to determine selected vCenter
$option = Read-Host
switch -Regex ($option) {
"\d" {
#Assign variable to selected vCenter and get user credentials
$destvCenter = $vcenters[$option-1]
$cred = Get-Credential -UserName $destvCenter.Credentials -Message $destvCenter.vCenter
#if -client switch is used, launch vSphere client and connect with credentials
if($client) { Start-Process -FilePath "C:\Program Files (x86)\VMware\Infrastructure\Virtual Infrastructure Client\Launcher\VpxClient.exe" -ArgumentList "-s $($destvCenter.vCenter) -u $($cred.username) -p $($cred.GetNetworkCredential().Password)" }
#Disconnect from all vCenters and connect to selected vCenter
Write-Host "Connecting to $($destvCenter.vCenter)"
#Disconnect-VIServer -Server * -Force -ErrorAction SilentlyContinue | Out-Null
Connect-VIServer -Credential $cred -server $destvCenter.vCenter
}
default { Write-Host "$option - Invalid Option" }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment