Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Staging vCenter Server Appliance (VCSA) updates using Rest API

This script is a another part of Upgrade Vcenter Server Appliance (VCSA) with VMware PowerCLI. In this article I am using Rest API calls to update VCSA via VAMI. At the moment I am able to stage the updates, but installation of update to vCenter is not working. Once I get solution I will revise this script. I have uploaded this script for my reminder that I need to complete this script.

Download this script Update-vCenterRestAPI.ps1 here or it is also available on github.com/janviudapi.

 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
Write-Host "vCenter authentication information" -BackgroundColor Yellow
$vCenter = 'dccomics.vcloud-lab.com'  
$username = '[email protected]'   
$password = '123456'  
 
$secureStringPassword = ConvertTo-SecureString $password -AsPlainText -Force  
$encryptedPassword = ConvertFrom-SecureString -SecureString $secureStringPassword  
$credential = New-Object System.Management.Automation.PsCredential($username,($encryptedPassword | ConvertTo-SecureString))  
#$credential.GetNetworkCredential().Password  
 
#Type credential and process to base 64  
#$credential = Get-Credential -Message 'Type vCenter Password' -UserName '[email protected]'  
$auth = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($credential.UserName+':'+$credential.GetNetworkCredential().Password))  
$head = @{  
  Authorization = "Basic $auth"  
}  

Write-Host "Login to vCenter" -BackgroundColor Yellow
#Authenticate against vCenter  
$a = Invoke-WebRequest -Uri "https://$vCenter/rest/com/vmware/cis/session" -Method Post -Headers $head -SkipCertificateCheck
$token = ConvertFrom-Json $a.Content | Select-Object -ExpandProperty Value  
$session = @{'vmware-api-session-id' = $token}  
  

#Get All Vcenter Server VAMI updates 
$vamiUpdateAPiUrl = "https://$vCenter/api/appliance/update"  
  
#Get vCenter appliance version
$vamiUpdatAPI = Invoke-WebRequest -Uri $vamiUpdateAPiUrl -Method Get -Headers $session -SkipCertificateCheck
$vamiUpdateList = ConvertFrom-Json $vamiUpdatAPI.Content 

#Get list of available updates for vCenter Server, It takes some time to download update catalog.
#https://developer.vmware.com/apis/vsphere-automation/latest/appliance/api/appliance/update/pending/version/get/
#https://developer.vmware.com/apis/vsphere-automation/latest/vcenter/
#https://vdc-repo.vmware.com/vmwb-repository/dcr-public/30fe7bca-1d83-49ec-985d-2b944e996948/4c5145e2-1d5a-4866-af04-f56c12fda442/landing_types.html#PKG_com.vmware.appliance
#https://vdc-download.vmware.com/vmwb-repository/dcr-public/423e512d-dda1-496f-9de3-851c28ca0814/0e3f6e0d-8d05-4f0c-887b-3d75d981bae5/VMware-vSphere-Automation-SDK-REST-6.7.0/docs/apidocs/operations/com/vmware/appliance/update/pending.stage_and_install-operation.html
#https://dccomics.vcloud-lab.com/ui/app/devcenter/api-explorer
$vamiCheckUpdateUrl = "https://$vCenter/api/appliance/update/pending?source_type=LOCAL_AND_ONLINE"
$vamiCheckUpdatAPI = Invoke-WebRequest -Uri $vamiCheckUpdateUrl -Method Get -Headers $session -SkipCertificateCheck
$vamiCheckUpdatesList = ConvertFrom-Json $vamiCheckUpdatAPI.Content
$vamiLastestUpdate = $vamiCheckUpdatesList | Sort-Object Version -Descending | Select-Object -First 1

#Check VCSA detailed update information
$vamiCheckUpdateVersionUrl = "https://$vCenter/api/appliance/update/pending/$($vamiLastestUpdate.version)"
$vamiCheckUpdateVersionAPI = Invoke-WebRequest -Uri $vamiCheckUpdateVersionUrl -Method Get -Headers $session -SkipCertificateCheck
$vamiCheckUpdateVersionAPI | ConvertFrom-Json | Select-Object name, release_date

Write-Host "Get vCenter Patches updates list" -BackgroundColor Yellow  
#PreCheck VCSA detailed update information - Post
$vamiPreCheckUpdateUrl = "https://$vCenter/api/appliance/update/pending/$($vamiLastestUpdate.version)?action=precheck" 
$vamiPreCheckUpdateAPI = Invoke-WebRequest -Uri $vamiPreCheckUpdateUrl -Method Post -Headers $session -SkipCertificateCheck
ConvertFrom-Json $vamiPreCheckUpdateAPI.Content
$vamiPreCheckUpdateAPI.Content | ConvertFrom-Json | select-object -expandproperty issues

Write-Host "Stage update on vCenter Server" -BackgroundColor Yellow  
$vamiStageUpdateUrl = "https://$vCenter/api/appliance/update/pending/$($vamiLastestUpdate.version)?action=stage" 
$vamiStageUpdateAPI = Invoke-WebRequest -Uri $vamiStageUpdateUrl -Method Post -Headers $session -SkipCertificateCheck
#ConvertFrom-Json $vamiStageUpdateAPI.Content
Start-Sleep -Seconds 600

Write-Host "Check Staged update info on vCenter Server" -BackgroundColor Yellow  
$vamiGetStageUpdateUrl = "https://$vCenter/api/appliance/update/staged" 
$vamiGetStageUpdateAPI = Invoke-WebRequest -Uri $vamiGetStageUpdateUrl -Method Get -Headers $session -SkipCertificateCheck
ConvertFrom-Json $vamiGetStageUpdateAPI.Content

Write-Host "Install update on vCenter Server" -BackgroundColor Yellow
# $vamiInstallUpdateRawBody = @'
# {
#   "user_data": {
#      "key": "obj-103",
#      "value": "string"
#   }
# }
# '@

$vamiInstallUpdateRawBody = @'
{
  "user_data": { 
    "key": "id",
    "value": "8.0.0.10200"
  }
}
'@
$vamiInstallUpdateBody = $vamiInstallUpdateRawBody | ConvertFrom-Json
$vamiInstallUpdateUrl = "https://$vCenter/api/appliance/update/pending/$($vamiLastestUpdate.version)?action=install" 
$vamiInstallUpdateAPI = Invoke-WebRequest -Uri $vamiInstallUpdateUrl -Method Post -Headers $session -Body $vamiInstallUpdateBody -SkipCertificateCheck
#ConvertFrom-Json $vamiInstallUpdateAPI.Content

Here is the screenshot of staged patch on VCSA.

Useful Articles
Registering HPE ILO amplifier pack (Hardware support manager) with vCenter 7 Lifecycle manager
VMware LifeCycle Manager import updates bundle and patch ESXi server
How to update a patch on an ESXi host via command line
Add a Trusted Root Certificate to the Certificate Store VMware Photon OS
Patching update VMware vCenter Server Appliance from a zipped update bundle Web server
Powershell GUI VMware ESXi custom patch bundle builder
Create a custom TCPIP stack on ESXi server - VMware PowerCLI GUI



This post first appeared on Tales From Real IT System Administrators World And Non-production Environment, please read the originial post: here

Share the post

Staging vCenter Server Appliance (VCSA) updates using Rest API

×

Subscribe to Tales From Real It System Administrators World And Non-production Environment

Get updates delivered right to your inbox!

Thank you for your subscription

×