Skip to content

Instantly share code, notes, and snippets.

@Panzerbjrn
Created April 28, 2021 11:39
Show Gist options
  • Save Panzerbjrn/66c28059bfe72d493024d46b8df60394 to your computer and use it in GitHub Desktop.
Save Panzerbjrn/66c28059bfe72d493024d46b8df60394 to your computer and use it in GitHub Desktop.
Basic script to get vROps data
$Accept = 'application/json'
$AuthHeaders = @{
accept = $Accept
}
$Body = @{
refresh_token = '<Insert Your Own API Key Here>'
}
$AuthResponseSplat = @{
Method = "Post"
Uri = "https://console.cloud.vmware.com/csp/gateway/am/api/auth/api-tokens/authorize"
Body = $Body
ContentType = $ContentType
}
$AuthResponse = Invoke-RestMethod @AuthResponseSplat -Headers $AuthHeaders -ErrorAction STOP -Verbose
####
$BaseURI = "https://www.mgmt.cloud.vmware.com/vrops-cloud/suite-api/api/"
$ContentType = 'application/json'
$Headers = @{
accept = $Accept
'content-type' = $ContentType
Authorization = "CSPToken $($AuthResponse.access_token)"
}
Write-Output "BaseURI is $BaseUri"
$InvokeRestMethodSplat = @{
Method = "GET"
Headers = $Headers
ContentType = $ContentType
}
$URI = $BaseURI + "resources"
$Result = Invoke-RestMethod @InvokeRestMethodSplat -Uri $URI
$Resources = $Result.resourceList
$FinalPage = ($Result.links | where {$_.Name -eq "last"}).href.split('=').split('&')[1]
$Page = 1
DO{
Write-Output "Page $($Page)"
$Uri = $BaseURI + "resources?page=$Page&amp;pageSize=1000"
#$Result = Invoke-RestMethod @InvokeRestMethodSplat -Uri $Uri
$Result = Invoke-RestMethod @InvokeRestMethodSplat -Uri $Uri
$Resources += $Result.resourceList
Write-Output "There are $($Resources.count) resources"
$Page++
Sleep -s 1
}
UNTIL ((($Result.links | where {$_.Name -eq "last"}).href) -eq (($Result.links | where {$_.Name -eq "Current"}).href))
$VMs = $Resources | Where-Object {$_.ResourceKey.resourceKindKey -eq "VirtualMachine"}
$VHosts = $Resources | Where-Object {$_.ResourceKey.resourceKindKey -eq "HostSystem"}
$Clusters = $Resources | Where-Object {$_.ResourceKey.resourceKindKey -eq "ClusterComputeResource"}
$DataStores = $Resources | Where-Object {$_.ResourceKey.resourceKindKey -eq "datastore"}
####################################
#Sets Dates
$Date = Get-Date
$Start = $Date.AddDays(-1)
$Finish = $Date
$RollUpTypeAVG = 'AVG'
$RollUpTypeMAX = 'MAX'
$IntervalType = 'DAYS'
$IntervalCount = "2"
[int64]$StartDateEpoc = Get-Date -Date $Start.ToUniversalTime() -UFormat %s
$StartDateEpoc = $StartDateEpoc*1000
[int64]$EndDateEpoc = Get-Date -Date $Finish.ToUniversalTime() -UFormat %s
$EndDateEpoc = $EndDateEpoc*1000
ForEach ($VM in $VMs[(0..(($VMs).count -1) | Get-Random)]){
$ResourceID = $VM.identifier
$StatsURL1 = $BaseURI + 'resources/stats?resourceId='+ $ResourceID + '&rollUpType='+ $RollUpTypeAVG + '&intervalType=' + $IntervalType + '&intervalcount=' + $IntervalCount + '&begin=' + $StartDateEpoc + '&end=' + $EndDateEpoc
$StatsURL2 = $BaseURI + 'resources/stats?resourceId='+ $ResourceID + '&rollUpType='+ $RollUpTypeMAX + '&intervalType=' + $IntervalType + '&intervalcount=' + $IntervalCount + '&begin=' + $StartDateEpoc + '&end=' + $EndDateEpoc
$PropertiesUri = $BaseURI + "resources/$ResourceID/properties"
$RelationshipsURI = $BaseURI + "resources/$ResourceID/relationships"
$VMRelationships = (Invoke-RestMethod @InvokeRestMethodSplat -Uri $RelationshipsURI).resourceList
$VMStats1 = Invoke-RestMethod @InvokeRestMethodSplat -Uri $StatsURL1
$VMStats2 = Invoke-RestMethod @InvokeRestMethodSplat -Uri $StatsURL2
$VMProperties = (Invoke-RestMethod -Uri $PropertiesUri @InvokeRestMethodSplat).property
}
ForEach ($Cluster in $Clusters[(0..(($Clusters).count -1) | Get-Random)]){
$ResourceID = $Cluster.identifier
$StatsURL1 = $BaseURI + 'resources/stats?resourceId='+ $ResourceID + '&rollUpType='+ $RollUpTypeAVG + '&intervalType=' + $IntervalType + '&intervalcount=' + $IntervalCount + '&begin=' + $StartDateEpoc + '&end=' + $EndDateEpoc
$StatsURL2 = $BaseURI + 'resources/stats?resourceId='+ $ResourceID + '&rollUpType='+ $RollUpTypeMAX + '&intervalType=' + $IntervalType + '&intervalcount=' + $IntervalCount + '&begin=' + $StartDateEpoc + '&end=' + $EndDateEpoc
$PropertiesUri = $BaseURI + "resources/$ResourceID/properties"
$RelationshipsURI = $BaseURI + "resources/$ResourceID/relationships"
$CluRelationships = (Invoke-RestMethod @InvokeRestMethodSplat -Uri $RelationshipsURI).resourceList
$CluStats1 = Invoke-RestMethod @InvokeRestMethodSplat -Uri $StatsURL1
$CluStats2 = Invoke-RestMethod @InvokeRestMethodSplat -Uri $StatsURL2
$CluProperties = (Invoke-RestMethod -Uri $PropertiesUri @InvokeRestMethodSplat).property
}
ForEach ($VHost in $Vhosts[(0..(($Vhosts).count -1) | Get-Random)]){
$ResourceID = $VHost.identifier
$StatsURL1 = $BaseURI + 'resources/stats?resourceId='+ $ResourceID + '&rollUpType='+ $RollUpTypeAVG + '&intervalType=' + $IntervalType + '&intervalcount=' + $IntervalCount + '&begin=' + $StartDateEpoc + '&end=' + $EndDateEpoc
$StatsURL2 = $BaseURI + 'resources/stats?resourceId='+ $ResourceID + '&rollUpType='+ $RollUpTypeMAX + '&intervalType=' + $IntervalType + '&intervalcount=' + $IntervalCount + '&begin=' + $StartDateEpoc + '&end=' + $EndDateEpoc
$PropertiesUri = $BaseURI + "resources/$ResourceID/properties"
$RelationshipsURI = $BaseURI + "resources/$ResourceID/relationships"
$VHRelationships = (Invoke-RestMethod @InvokeRestMethodSplat -Uri $RelationshipsURI).resourceList
$VHStats1 = Invoke-RestMethod @InvokeRestMethodSplat -Uri $StatsURL1
$VHStats2 = Invoke-RestMethod @InvokeRestMethodSplat -Uri $StatsURL2
$VHProperties = (Invoke-RestMethod -Uri $PropertiesUri @InvokeRestMethodSplat).property
}
ForEach ($Datastore in $Datastores[(0..(($Datastores).count -1) | Get-Random)]){
$ResourceID = $Datastore.identifier
$StatsURL1 = $BaseURI + 'resources/stats?resourceId='+ $ResourceID + '&rollUpType='+ $RollUpTypeAVG + '&intervalType=' + $IntervalType + '&intervalcount=' + $IntervalCount + '&begin=' + $StartDateEpoc + '&end=' + $EndDateEpoc
$StatsURL2 = $BaseURI + 'resources/stats?resourceId='+ $ResourceID + '&rollUpType='+ $RollUpTypeMAX + '&intervalType=' + $IntervalType + '&intervalcount=' + $IntervalCount + '&begin=' + $StartDateEpoc + '&end=' + $EndDateEpoc
$PropertiesUri = $BaseURI + "resources/$ResourceID/properties"
$RelationshipsURI = $BaseURI + "resources/$ResourceID/relationships"
$DSRelationships = (Invoke-RestMethod @InvokeRestMethodSplat -Uri $RelationshipsURI).resourceList
$DSStats1 = Invoke-RestMethod @InvokeRestMethodSplat -Uri $StatsURL1
$DSStats2 = Invoke-RestMethod @InvokeRestMethodSplat -Uri $StatsURL2
$DSProperties = (Invoke-RestMethod -Uri $PropertiesUri @InvokeRestMethodSplat).property
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment