Sorting bits into bytes...

Helping the community

One of the reasons I joined ITQ about a year ago is that they like to help people. That is one of those qualities that really stand out. And often people tell me that I’m a helping guy myself. Specially in these Corona times we have the help each other out wherever we can. So I was checking the community page of VMware and someone asked help with generating a custom report with vROps. I thought to myself that should be easy. The challenge here is that they have a pretty large environment (14 vROps instances). The requested information is also not easily exported into a report. So its API time!

So here is my first try in getting that report.

 

<#

Author: Kabir Ali - info@kablog.nl
Scriptname: RG (Report Generator) 
Version: 1.0 (Tested)
Date: Dec 18 2020 
Why: Sean asked for it @ https://communities.vmware.com/t5/vRealize-Operations-Manager/vRealize-Operations-Manager-Custom-Report/m-p/2817896#M18248
Remark: Test setup
        vROps Version 8.2.0 (16949153)

#>

<#
Example:

.\RG.ps1 -vROpsServer "vROps.local.domain" -vROpsUser "admin" -vROpsPass "VMware1!" $CSVLocation "c:\temp\RG.csv"

#>


Param (
    
    [Parameter(Mandatory = $true)][string]$vROpsServer,
    [Parameter(Mandatory = $true)][string]$vROpsUser,
    [Parameter(Mandatory = $true)][string]$vROpsPass,
    [Parameter(Mandatory = $true)][string]$CSVLocation
)


# Adding certificate exception to prevent API errors
add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

# Building vROps API string & invoking REST API
$BaseURL = "https://" + $vROpsServer + "/suite-api/api/"
$BaseAuthURL = "https://" + $vROpsServer + "/suite-api/api/auth/token/acquire"
$Type = "application/json"
# Creating JSON for Auth Body
$AuthJSON =
"{
  ""username"": ""$vROpsUser"",
  ""password"": ""$vROpsPassword""
}"
# Authenticating with API
Try {
    $vROpsSessionResponse = Invoke-RestMethod -Method POST -Uri $BaseAuthURL -Body $AuthJSON -ContentType $Type
}
Catch {
    $_.Exception.ToString()
    $error[0] | Format-List -Force
}
# Extracting the session ID from the response
$vROpsSessionHeader = @{"Authorization"="vRealizeOpsToken "+$vROpsSessionResponse.'auth-token'.token
"Accept"="application/json"}


# Get vROps name
$ResourcesURL = $BaseURL+"adapters"
Try {
    $NodeName = Invoke-RestMethod -Method GET -Uri $ResourcesURL -Headers $vROpsSessionHeader -ContentType $Type
    $vROpsNodeName = ($NodeName.adapterInstancesInfoDto.resourceKey | where {$_.name -like "vRealize Operations Manager Adapter*"}).resourceIdentifiers.value | select-object -first 1
}
Catch {
    $_.Exception.ToString()
    $error[0] | Format-List -Force
}


# Get version and build info
$ResourcesURL = $BaseURL+"versions/current"
Try {
    $Versions = Invoke-RestMethod -Method GET -Uri $ResourcesURL -Headers $vROpsSessionHeader -ContentType $Type
    $vROpsReleaseName = $Versions.ReleaseName
    $vROpsBuildNumber = $Versions.BuildNumber
}
Catch {
    $_.Exception.ToString()
    $error[0] | Format-List -Force
}


# Get license data
$ResourcesURL = $BaseURL+"deployment/licenses"
Try {
    $License = Invoke-RestMethod -Method GET -Uri $ResourcesURL -Headers $vROpsSessionHeader -ContentType $Type
    $vROpsLicenseCapacity = $License.solutionLicenses.capacity
    $vROpsLicenseUsage = $License.solutionLicenses.usage
}
Catch {
    $_.Exception.ToString()
    $error[0] | Format-List -Force
}


# Get active and open alerts
$ResourcesURL = $BaseURL+"alerts"
Try {
    $Alerts = Invoke-RestMethod -Method GET -Uri $ResourcesURL -Headers $vROpsSessionHeader -ContentType $Type
    $vROpsAlerts = $Alerts.alerts | where {$_.status -eq "ACTIVE" -and $_.controlState -eq "OPEN"}
        # Based on the Alert find the human readable names
        [array]$vROpsResourceName = @()
        foreach($AlertItem in $vROpsAlerts) {
            $ResourceId = $AlertItem.resourceId
            $ResourcesURL = $BaseURL+"resources/"+$resourceId
            $vROpsVMName = Invoke-RestMethod -Method GET -Uri $ResourcesURL -Headers $vROpsSessionHeader -ContentType $Type
                $vROpsResourceName += New-Object PSObject -Property @{
                    ResourceName = $vROpsVMName.resourceKey.name
                    ResourceAlert = $AlertItem.alertDefinitionName
                }
    }
}
Catch {
    $_.Exception.ToString()
    $error[0] | Format-List -Force
}

[array]$Report = @()
foreach($itemNodeName in $vROpsNodeName) {
    foreach($itemReleaseName in $vROpsReleaseName){
        foreach ($itemBuildNumber in $vROpsBuildNumber){
            foreach ($itemLicenseCap in $vROpsLicenseCapacity){
                foreach ($itemLicenseUse in $vROpsLicenseUsage) {
                    foreach ($itemResource in $vROpsResourceName) {
                        $Report += New-Object PSObject -Property @{
                        AlertOnObject = $itemResource.ResourceName
                        AlertDescription = $itemResource.ResourceAlert
                        LicenseUsage = $itemLicenseUse
                        LicenseCapacity = $itemLicenseCap
                        BuildNumber = $itemBuildNumber
                        ReleaseName = $itemReleaseName
                        NodeName = $itemNodeName
                        }
                    }
                }
            }
        }
    }
}
$Report | Select-Object NodeName, ReleaseName, BuildNumber, LicenseCapacity, LicenseUsage, AlertObject, AlertDescription | export-csv -Delimiter "," -NoTypeInformation C:\temp\RG.csv

# Close sessions and done
Invoke-RestMethod -Method POST -Uri $BaseURL"/auth/token/release" -Headers $vROpsSessionHeader -ContentType $Type

 

A link to the question itself.

Leave a Reply