From time to time the first try to connect to a vCenter-Server fails, building a simple loop to allow a limited number of retries fixes this possible issue.
$vSphereServer = "vcsa.local"
$vSphereUser = "administrator@vsphere.local"
$vSpherePassword = "********"
$result = @{}
$retries = 6
$viConnection = $null
#
while ($viConnection -eq $null) {
# My-Logger 'Connecting to Management vCenter Server $vSphereServer ...'
#
$error.clear()
$viConnection = Connect-VIServer $vSphereServer -User $vSphereUser -Password $vSpherePassword -WarningAction SilentlyContinue
#
if ($viConnection -eq $null) {
$retries = $retries - 1
if ($retries -eq 0) {
throw ("Connecting to vCenter ($vSphereServer) failed ($vSphereUser): $error")
}
Start-Sleep -s 10
}
}
$result.viConnection = $viConnection
Disclaimer
Since i’m trying to get rid of Evernote, too anoying too often, i’ll start to document non-private-stuff here.