Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Submit a catalog item request via vRA API in PowerShell
# VMware vRealize Automation API sample in PowerSHell
#
# Submits a catalog item request via the vRA API
#
# For vRO 7.0+
#set global variables
$baseUrl = 'https://vra01a.corp.local'
$vrauser = 'myuser'
$vrapass = 'mypassword'
$tenant = 'dev02'
$catalogItemName = 'HVD 7'
#connect to AppHost
$url = "$baseUrl/identity/api/tokens"
$req = [System.Net.WebRequest]::Create($url)
$req.Method ="POST"
$req.Contenttype = "application/json"
$Body = [byte[]][char[]] '{"username":"$vrauser","password":"$vrapass","tenant":"$tenant"}';
$Stream = $req.GetRequestStream();
$Stream.Write($Body, 0, $Body.Length);
$Stream.Flush();
$Stream.Close();
$response = $req.GetResponse();
$reader = new-object System.IO.StreamReader($response.GetResponseStream())
$stuff = $reader.ReadToEnd()
$obJson = $stuff | ConvertFrom-Json
$jID = $obJson.id
$jExpires = $obJson.expires
#get catalog item by name
$url = "$baseUrl/catalog-service/api/consumer/entitledCatalogItemViews?`$filter=name+eq+'$catalogItemName'"
$req = [System.Net.WebRequest]::Create($url)
$req.Method ="GET"
$req.Headers.Add('Authorization','Bearer ' + $jID)
$response = $req.GetResponse();
$reader = new-object System.IO.StreamReader($response.GetResponseStream())
$stuff = $reader.ReadToEnd()
$obJson = $stuff | ConvertFrom-Json
$arrContent = $obJson.content | select catalogItemId, name, description | Where-Object {$_.name -eq $catalogItemName}
$CatalogItemId = $arrContent.catalogItemId
# get catalog item template
$url = "$baseUrl/catalog-service/api/consumer/entitledCatalogItems/$CatalogItemId/requests/template"
$R=Invoke-WebRequest $url -Headers $headers
# modify the template as needed for the request
$Form = $R.Content | ConvertFrom-Json
$Form.data.vSphere_Machine_1.data.cpu = 8
$Form.data.vSphere_Machine_1.data.memory = 8192
#submit request
$url = "$baseUrl/catalog-service/api/consumer/entitledCatalogItems/$CatalogItemId/requests"
$cjson = $Form | ConvertTo-Json -Depth 10
$K=Invoke-WebRequest $url -Headers $headers -Method POST -Body $cjson -ContentType "application/json"
if ($K.StatusCode -eq 201) {write-host "successful"}
@Pisamad

This comment has been minimized.

Copy link

Pisamad commented Sep 24, 2018

Hi,
I tried to use this process but in my case, there are some properties defined in my business group. I expected to show them in the requests/template as it's the case when I use VRA portal. But it's not the case.
How can I merge subtenant properties with requests properties ?
Note that a simple user can not access to the subtenants info via "identity/api/tenants/{tenant}/subtenants".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.