PowerCLI

 View Only
  • 1.  PowerCLI Connect-CIServer to vCloud Director sometimes fails on first attempt (succeeds on retry), what best practice retry pattern?

    Posted Jan 05, 2026 03:03 AM

    Hi everyone,

    I'm running a scheduled DailyCheck PowerCLI report that connects to two vCloud Director environments and collects a few vCD-side items (vApp status, catalog/media, vApp network, cells), then generates an HTML report.

    Two issues I want to improve:

    1. vCD login reliability: sometimes Connect-CIServer fails temporarily, and I want to retry up to 3 times (with delay).
    2. Report accuracy: currently some parts show "N/A" (because my HTML logic uses ?: "N/A" when the variable is empty). The problem is that "N/A" hides the real reason (authentication issue, etc.). I want the report to show the actual reason when data cannot be collected.

    #connect vCloudDirectory 
    connect-Ciserver -server $cloudDirectorServer -Credential $credential
     
    $vAppState_vCDss = Get-ciVApp -Name "daily-check-$today" -Orgvdc "S-Z-POD-Support" | Select-Object Name, Status
    $CatalogMedia_vCDss = Get-Catalog -Name "daily-check-$today" -Org "Support" 
    $MediaItems_vCDss = Get-Media -Catalog "daily-check-$today" -server "x.x.x.x" | Select-Object Name, status
    $vAppss = Get-OrgVdc -Name "S-Z-POD-Support" | Get-CIVApp -Name "daily-check-$today"
    $vAppNetworkss = Get-CIVAppNetwork -VApp $vAppss
    $vCenterss = Search-Cloud -QueryType virtualcenter -server xxxx | Select-Object Name, status,ListenerState
    $sscell =  Search-Cloud -QueryType cell -server xxxx | Select-Object Name, @{Name="IsActive";Expression={$_.IsActive ? "Active" : "Inactive"}}
     
    Disconnect-CIServer -Server $cloudDirectorServer -Confirm:$false
     

    Where "N/A" is coming from (HTML logic)

    <td>$($CatalogMedia_vCDss ? "Created successfully" : "N/A")</td>
    <td>$($MediaItems_vCDss ? "Upload successfully" : "N/A")</td>
     
    <td>$($vAppNetworkss.Name ? $vAppNetworkss.Name : "N/A")</td>
    <td>$($vAppNetworkss ? "Created successfully" : "N/A")</td>
    My questions are 
    1. What's the cleanest PowerCLI pattern to retry Connect-CIServer up to 3 times (with delay) and avoid session leaks?
    2. How turning "N/A" into a real failure reason? what's the cleanest way to capture and log the real failure reason from the PowerCLI exception?

    Any guidance or examples would help a lot....



    -------------------------------------------


  • 2.  RE: PowerCLI Connect-CIServer to vCloud Director sometimes fails on first attempt (succeeds on retry), what best practice retry pattern?

    Posted Jan 07, 2026 06:26 PM

    I don't have a lab handy to test your code, that said:

    1.) I'd be looking into why you have to reconnect, that along could be involved with the missing data. Wrong API version, network, certs, etc.
    2.) -ErrorVariable will help you collect error messages and evaluate them

    -------------------------------------------



  • 3.  RE: PowerCLI Connect-CIServer to vCloud Director sometimes fails on first attempt (succeeds on retry), what best practice retry pattern?

    Posted Jan 18, 2026 08:00 PM

    1- The error is: "Unable to connect to vCloud server" 'The server returned the following: GatewayTimeout' , and i think this cause of our DCs in our environment.

    -------------------------------------------



  • 4.  RE: PowerCLI Connect-CIServer to vCloud Director sometimes fails on first attempt (succeeds on retry), what best practice retry pattern?

    Posted Jan 19, 2026 03:26 AM
    Edited by new_bember Jan 19, 2026 03:26 AM

    vCenter connect start
    try { Disconnect-ViServer -server * -EA Stop -Confirm:$FALSE}
    catch [VMware.VimAutomation.Sdk.Types.V1.ErrorHandling.VimException.ServerObnFailureException] { Write-host "Were not connected to any server." -f Yellow }
    catch [System.Management.Automation.ActionPreferenceStopException] { Write-host "Already disconnected from last used vCenter." }
    try { $conn = Connect-VIServer -Server $server -Credential $ADCreds -ErrorAction Stop }
    catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidLogin] { Write-host "Catch smth" -f Yellow; Get-Credential -UserName $username -Message "Enter AD VM domain credentials" | Export-Clixml $ADCredsFileName; $ADCreds = Import-Clixml $ADCredsFileName }
    finally { if(!($conn)){ $conn = Connect-VIServer -Server $server -Credential $ADCreds }}
    ### vCenter connect end

    You could think about using try/catch blocks for your connection process. Just need to figure out first what kind of exceptions you need to catch

    -------------------------------------------