Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Leveraging PowerShell to Stage VCSA Updates via vCenter REST API

Staging in VMware VCSA (vCenter Server Appliance) allows you to upload/download patches and updates, In the staging process it does not directly install or apply the patches/updates immediately but just copies patch updates on VCSA appliance. Which allows for a faster remediation process during updates because the necessary files are already present on the vCenter Server.

On the vCenter server to manually stage updates on VCSA, login to vCenter Server Management portal with 5480 port. Go to Update in the left pane, Check for updates, Select the patch from list and click stage option. Once complete patch is downloaded locally on vCenter server, it will show patch marked as staged and Install button will be activated and visible. You can unstage updates as well. 

Note: It will take time to stage patch on vCenter based on the network speed to the URL configured. 

Due to recent changes by Broadcom, direct access to the VMware website depot for downloading updates from the vCenter server has been discontinued. The steps outlined in this article provide a valuable solution by automating the VCSA patch update process. 

Screenshot of the vCenter Server Management interface, 'Update' tab. It shows 'Current version details' as 8.0.0.400. Under 'Available updates', version 8.0.0.500 is listed as 'Staged', with an 'INSTALL' button visible, indicating it's ready for deployment. Details include 'Type: Fix', 'Release Date: Apr 10, 2025', 'Reboot required: Yes', and 'Severity: Critical'.

Check very related article here on Performing vCenter Offline Updates Via Your On-Prem Web Server Securely & Seamlessly

I have created this PowerShell script which helps to stage patches on vCenter Server connecting its REST API. You will need to provide information of vCenter, user, password, patch URL location. It will show the patches available and stage the latest update on VCSA server.

Screenshot of a PowerShell console running 'Stage-VCSACUpdate.ps1'. The output shows 'Available versions: Version: 8.0.0.500 - Severity: CRITICAL - Date: 04/10/2025 00:00:00'. It then indicates 'Auto-selected latest version: 8.0.0.500' and 'Staging update version: 8.0.0.500...' followed by 'Staging triggered successfully.' and 'Session closed.'.

Download Stage-VCSAUpdatePatch.ps1 here or it is also available github.com.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
# vCenter Update Script
# created by vCloud-Lab.lab
# This script stages latest update version on a vCenter Server using the REST API.
# Requires PowerShell 7 or newer

param (
    [string]$vcServer = "marvel.vcloud-lab.com",
    [string]$vcUser = "[email protected]",
    [string]$vcPass = "Computer@123",
    [string]$Version = "",  # Leave blank to auto-select latest
    [string]$SourceType = "MANUAL",
    [string]$Url = "https://192.168.34.99/vc_patches/"  # Optional: URL where patch metadata is hosted
)

# Ignore SSL certificate warnings (dev/test only)
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }

# --- Get vCenter Session ---
function Get-VCenterSession {
    $authUri = "https://$vcServer/rest/com/vmware/cis/session"
    $authBytes = [System.Text.Encoding]::UTF8.GetBytes("${vcUser}:${vcPass}")
    $authHeader = "Basic " + [System.Convert]::ToBase64String($authBytes)
    $headers = @{
        "Authorization" = $authHeader
        "Content-Type"  = "application/json"
    }
    $response = Invoke-RestMethod -Uri $authUri -Method Post -Headers $headers -SkipCertificateCheck
    return $response.value
}

# --- Get Pending Updates (New Function) ---
function Get-PendingUpdates {
    param (
        [string]$vcServer,
        [string]$sessionId,
        [string]$sourceType = "MANUAL",
        [string]$url = "",
        [bool]$includeMajor = $true
    )

    $headers = @{ "vmware-api-session-id" = $sessionId }

    $params = @{
        source_type = $sourceType
        enable_list_major_upgrade_versions = $includeMajor.ToString().ToLower()
    }
    if ($url) { $params.url = $url }

    $query = ($params.GetEnumerator() | ForEach-Object {
        [System.Web.HttpUtility]::UrlEncode($_.Key) + "=" + [System.Web.HttpUtility]::UrlEncode($_.Value)
    }) -join '&'

    $uri = "https://$vcServer/api/appliance/update/pending?$query"

    return Invoke-RestMethod -Method Get -Uri $uri -Headers $headers -SkipCertificateCheck
}

# --- Main Script ---
try {
    $session = Get-VCenterSession
    $headers = @{ 
        "vmware-api-session-id" = $session
        "Content-Type" = "application/json"
    }

    # Get list of pending updates using new function
    $updates = Get-PendingUpdates -vcServer $vcServer -sessionId $session -sourceType $SourceType -url $Url

    if (-not $updates) {
        Write-Host "❌ No pending updates found." -ForegroundColor Red
        return
    }

    Write-Host "✅ Available versions:" -ForegroundColor Cyan
    $updates | ForEach-Object {
        Write-Host "• Version: $($_.version) - Severity: $($_.severity) - Date: $($_.release_date)"
    }

    # Use provided version or pick the latest
    if ([string]::IsNullOrWhiteSpace($Version)) {
        $Version = ($updates | Sort-Object -Property version -Descending | Select-Object -First 1).version
        Write-Host "`nℹ️ Auto-selected latest version: $Version" -ForegroundColor Yellow
    } elseif (-not ($updates.version -contains $Version)) {
        Write-Host "`n❌ ERROR: Provided version '$Version' not found in pending updates!" -ForegroundColor Red
        return
    }

    # Stage the update
    $stageUri = "https://$vcServer/api/appliance/update/pending/$($Version)?action=stage"
    Write-Host "`n🚀 Staging update version: $Version..."
    $stageResponse = Invoke-RestMethod -Method Post -Uri $stageUri -Headers $headers -SkipCertificateCheck #-Body '{}'

    Write-Host "✅ Staging triggered successfully." -ForegroundColor Green
}
catch {
    Write-Error "❌ ERROR: $($_.Exception.Message)"
}
finally {
    if ($session) {
        $logoutUri = "https://$vcServer/rest/com/vmware/cis/session"
        Invoke-RestMethod -Method Delete -Uri $logoutUri -Headers @{ "vmware-api-session-id" = $session } -SkipCertificateCheck
        Write-Host "`n🔒 Session closed." -ForegroundColor DarkGray
    }
}

Useful Articles
Install VMware VCSA Patch Upgrade using PowerCLI vCenter REST API
Performing vCenter Offline Updates Via Your On-Prem Web Server Securely & Seamlessly
Retrieve VCSA vCenter Version with PowerShell REST API
PowerShell & VCSA: Checking for Pending & Latest Updates via REST API
Configure VCSA Update Web Repository Settings with PowerShell and REST API
Reset vCenter Server Appliance (VCSA) Forgotten Root Password Without Reboot
Delete VMware VCSA Management Schedule Backup Job using PowerShell
Modify Broadcom VCSA Management Schedule Backup Job using PowerCLI
Configure vCenter Server Appliance Schedule Backup With VMware.PowerCLI
Automating vCenter Server Appliance Backups with PowerShell On Demand
VMware Photon OS Remote SSH: Password Authentication Fix
Monitoring Dell Server Health Using RACADM CLI: Sensor Info from iDRAC
Fix CPU Support Error: Installing VMware ESXi 9 on Unsupported CPUs
Turn Your PowerShell Into a Web Server: Instantly Share Local HTML & Files!

Go Back

Comment

Protected by Mathcha

Blog Search

Page Views

1 5 7 3 4 7 0 2

Archive

Follow me on Blogarama