$loginUrl = 'https://login.flexera.com/oidc/token' $endpoint = 'https://api.flexera.com' $orgId = '28010' Write-Host "Org ID: $orgId" $refreshToken = Read-Host -AsSecureString "Refresh token" $refreshToken = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($refreshToken)) $postParams = @{ grant_type='refresh_token'; refresh_token=$refreshToken } $resp = Invoke-WebRequest -Uri $loginUrl -Method POST -Body $postParams -ContentType 'application/x-www-form-urlencoded' $accessToken = ($resp.Content | ConvertFrom-Json).access_token $resp = Invoke-WebRequest -Uri "$endpoint/fnms/v1/orgs/$orgId/licenses?view=index" -Method GET -Headers @{Authorization="Bearer $accessToken"} $licenses = $resp | ConvertFrom-Json $n = 0 $printedCsvHeader = $false foreach ($license in $licenses) { $n = $n + 1 Write-Host "License $n of $($licenses.Length) - $($license.name)" $nextPage = "$endpoint/fnms/v1/orgs/$orgId/licenses/$($license.id)/consumption?limit=10000&view=extended" while ($nextPage -ne $null) { $resp = Invoke-WebRequest -Uri $nextPage -Method GET -Headers @{Authorization="Bearer $accessToken"} $nextPage = $null $respJson = $resp.Content | ConvertFrom-Json $consumptions = $respJson.values foreach ($consumption in $consumptions) { $obj = [PSCustomObject]@{ LicenseName = $license.name LicenseType = $license.type LicenseCompany = $license.company.name LicenseProductName = '' LicenseProductCompany = '' LicenseProductVersion = '' LicenseProductEdition = '' Consumed = $consumption.consumed ChargebackType = $consumption.chargeback.type ChargebackCurrency = $consumption.chargeback.currency ChargebackCost = $consumption.chargeback.cost ChargebackPoints = $consumption.chargeback.points DeviceName = $consumption.device.name DeviceDomain = $consumption.device.domain DeviceModel = $consumption.device.model DeviceCappedCoreCount = $consumption.device.cappedCoreCount DeviceCoreCount = $consumption.device.coreCount UserSamAccountName = $consumption.user.samAccountName UserDomain = $consumption.user.domain GroupLocation = $consumption.group.Location GroupCorporateUnit = $consumption.group.corporateUnit GroupCostCenter = $consumption.group.costCenter VmType = $consumption.vm.type VmHostName = $consumption.vm.host.name VmHostModel = $consumption.vm.host.model VmHostCoreCount = $consumption.vm.host.coreCount VmHostProcessorCount = $consumption.vm.host.processorCount VmHostConsumedVmCores = $consumption.vm.host.consumedVmCores VmHostChargebackCurrency = $consumption.vm.host.chargeback.currency VmHostChargebackCost = $consumption.vm.host.chargeback.cost VmHostChargebackPoints = $consumption.vm.host.chargeback.points VmHostIsEligibleForSubCapacity = $consumption.vm.host.isEligibleForSubCapacity VmPoolName = $consumption.vm.pool.name VmPoolCoreCount = $consumption.vm.pool.coreCount VmPoolConsumedVmCores = $consumption.vm.pool.consumedVmCores VmClusterName = $consumption.vm.cluster.name VmClusterConsumedVmCores = $consumption.vm.cluster.consumedVmCores } if ($consumption.products.Length -gt 1) { # bypass PowerShell consuming single object arrays $obj.LicenseProductName = $license.products.name $obj.LicenseProductCompany = $license.products.company.name $obj.LicenseProductVersion = $license.products.version $obj.LicenseProductEdition = $license.products.edition } elseif ($consumption.prodcuts.Length -eq 1) { $obj.LicenseProductName = $license.products[0].name $obj.LicenseProductCompany = $license.products[0].company.name $obj.LicenseProductVersion = $license.products[0].version $obj.LicenseProductEdition = $license.products[0].edition } if ($printedCsvHeader -eq $false) { $obj | ConvertTo-Csv | Select-Object -Skip 1 # skip junk PowerShell type line in output $printedCsvHeader = $true } else { $obj | ConvertTo-Csv | Select-Object -Skip 2 # skip past headers } } if ($respJson.nextPage -ne $null) { $nextPage = "$endpoint$($respJson.nextPage)" } } }