Menu

Virtual Geek

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

PowerShell & VCSA: Checking for Pending & Latest Updates via REST API

This script helps me to get the latest available pending updates for VCSA in vCenter Server Appliance Management portal before stage and install. It shows all in dept information of pending update from local web repo website url. Which will be helpful to automate installation further with correct version based on dates, severity and other information.

A screenshot of a PowerShell script running in a terminal, showing a successful authentication and a detected pending update for VMware vCenter Server 8.0 via the REST API.

To check the latest update on VCSA management portal manually, open the vCenter url with 5480 port. Navigate to Update and click CHECK UPDATES button.

Check the latest updates for your vCenter Server Appliance (VCSA) directly from the VCSA Management interface. Learn how to view current versions, identify pending patches, and manage your VMware environment's update status efficiently.

To use this script provide information as vCenter Server, Username and Password with Url of local web server where vCenter Updates are hosted (Performing vCenter Offline Updates Via Your On-Prem Web Server Securely & Seamlessly & Patching update VMware vCenter Server Appliance from a zipped update bundle Web server).

Download this script Get-VCSAPendingLatestUpdate.ps1 here or it is also available on 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# vCenter Update Check Script
# created by vCloud-Lab.lab
# This script checks for pending updates on a vCenter Server using the REST API.
# Requires PowerShell 7 or newer

# vCenter Server Details
$vcServer = "marvel.vcloud-lab.com"
$vcUser = "[email protected]"
$vcPass = "Computer@123"

# API Parameters
$sourceType = "MANUAL"  # Valid values: LAST_CHECK, LOCAL, MANUAL
$url = "https://192.168.34.99/vc_patches/" 
$enableMajorUpgrades = $true  # Set to $false to disable major version checks

# Ignore SSL certificate warnings (remove in production)
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }

# Function to get vCenter API session
function Get-VCenterSession {
    param(
        [string]$server,
        [string]$user,
        [string]$pass
    )
    
    $authUri = "https://$server/rest/com/vmware/cis/session"
    $authBytes = [System.Text.Encoding]::UTF8.GetBytes("${user}:${pass}")
    $authHeader = "Basic " + [System.Convert]::ToBase64String($authBytes)
    
    $headers = @{
        "Authorization" = $authHeader
        "Content-Type" = "application/json"
    }
    
    try {
        $response = Invoke-RestMethod -Uri $authUri -Method Post -Headers $headers -SkipCertificateCheck
        return $response.value
    }
    catch {
        Write-Error "Authentication failed: $($_.Exception.Message)"
        exit 1
    }
}

# Function to check pending updates
function Get-PendingUpdates {
    param(
        [string]$server,
        [string]$sessionId,
        [string]$source,
        [string]$url,
        [bool]$enableMajor
    )
    
    $apiEndpoint = "https://$server/api/appliance/update/pending"
    
    # Build query parameters
    $queryParams = @{
        source_type = $source
        enable_list_major_upgrade_versions = $enableMajor.ToString().ToLower()
    }
    
    if (-not [string]::IsNullOrEmpty($url)) {
        $queryParams.url = $url
    }
    
    # Create headers with session token
    $headers = @{
        "vmware-api-session-id" = $sessionId
        "Accept" = "application/json"
    }
    
    # Build full URI with query parameters
    $uriBuilder = [System.UriBuilder]::new($apiEndpoint)
    $uriBuilder.Query = ($queryParams.GetEnumerator() | 
        ForEach-Object { 
            [System.Web.HttpUtility]::UrlEncode($_.Key) + "=" + 
            [System.Web.HttpUtility]::UrlEncode($_.Value) 
        }) -join '&'
    
    try {
        $response = Invoke-RestMethod -Uri $uriBuilder.Uri -Method Get -Headers $headers -SkipCertificateCheck
        return $response
    }
    catch {
        $statusCode = $_.Exception.Response.StatusCode.value__
        $errorMsg = switch ($statusCode) {
            401 { "Session not authenticated" }
            403 { "Session not authorized" }
            404 { "Update source not found" }
            500 { "Internal server error" }
            default { "HTTP error $statusCode" }
        }
        Write-Error "API request failed: $errorMsg"
        return $null
    }
}

# Main execution
try {
    # Get authentication token
    $session = Get-VCenterSession -server $vcServer -user $vcUser -pass $vcPass
    Write-Host "Authenticated successfully. Session ID: $session`n" -ForegroundColor Green

    # Check for pending updates
    Write-Host "Checking for pending updates (source: $sourceType)..."
    $updates = Get-PendingUpdates -server $vcServer -sessionId $session `
        -source $sourceType -url $url -enableMajor $enableMajorUpgrades

    if ($updates) {
        Write-Host "`nPENDING UPDATES FOUND:`n" -ForegroundColor Yellow
        $updates | Format-List | Out-String | Write-Host
        
        # Show summary
        $updateCount = $updates.Count
        $lastUpdate = $updates | Sort-Object -Property version -Descending | Select-Object -First 1
        Write-Host "`nUpdate Summary:" -ForegroundColor Cyan
        Write-Host "Total pending updates: $updateCount"
        Write-Host "Latest version available: $($lastUpdate.version)"
        Write-Host "Release date: $($lastUpdate.release_date)"
        Write-Host "Severity: $($lastUpdate.severity)"
    }
    else {
        Write-Host "`nNo pending updates found." -ForegroundColor Green
    }
}
catch {
    Write-Error "Script execution failed: $($_.Exception.Message)"
}
finally {
    # Clean up session if exists
    if ($session) {
        Write-Host "`nCleaning up session..." -ForegroundColor DarkGray
        $logoutUri = "https://$vcServer/rest/com/vmware/cis/session"
        $headers = @{"vmware-api-session-id" = $session}
        Invoke-RestMethod -Uri $logoutUri -Method Delete -Headers $headers -SkipCertificateCheck | Out-Null
    }
}

Useful Articles
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
PowerCLI Gather complete Virtual Distributed Switch (VDS) information from VMware vCenter
VMware PowerCLI Find vCenter server name of any inventory object
Create List or Remove VMware VM Snapshots with PowerCLi
How to PowerCLI VMware Snapshots Virtual Machines disks consolidation is needed

Go Back

Comment

Blog Search

Page Views

13869636

Archive

Follow me on Blogarama