PowerShell Script to Look at Hosts and Their Advisories Since a Specific Date
The end point to look at hosts and their advisory data is:
https://api.app.flexerasoftware.com/api/inventory/hosts/510/advisories/
To get the host and their advisories list, use the following:
GET /api/inventory/hosts/510/advisories/
Below is a sample PowerShell script to look at hosts and their advisories since a specific date:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$global:ErrorArray = @()
$global:QueryLimit = 2000 #<- Increase to max number of hosts you want...
##########################################################################################################################################################################################
# Name URL Token $Sites = ( "Flexera SVM", "https://api.app.flexerasoftware.com/api/" ,"Token YOUR TOKEN HERE")
$Header = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$Header.Add("Content-Type", 'application/json')
$Header.Add("Authorization", $Sites[2] )
function Display-Errors ()
{
if ($global:ErrorArray.Count -eq 0)
{
#Write-Message ((Write-Spacing) + " All Good " + (Write-Header)) $false
}
else
{
Write-Message (" Errors: ") $true
foreach ($item in $global:ErrorArray)
{
Write-Message (" " + $item + " " + (Write-Header)) $true
}
}
}
function QueryData ($BaseURL, $Header, $URL)
{
# Get First Page of results (20 items)
$result = @()
$results = @()
try
{
$result = Invoke-RestMethod ($BaseURL + $URL) -Method Get -Headers $Header
if ($result.results)
{
$results = $result.results
}
else
{
$results = $result
}
}
catch
{
$global:ErrorArray += ("Error QueryData1 " + $BaseURL + $URL + " " + $_.Exception.Message + " " + $_.Exception.ItemName)
}
#Get the next pages of results, if any
while (![string]::IsNullOrWhiteSpace($result.next))
{
try
{
$result = Invoke-RestMethod $result.next -Method Get -Headers $Header
$results += $result.results
if ($results.count -gt $global:QueryLimit)
{
break;
}
}
catch
{
$global:ErrorArray += ("Error QueryData2 " + $URL + $result.next + " " + $_.Exception.Message + " " + $_.Exception.ItemName)
return $results
}
}
return $results
}
function ShowHostData ($BaseURL, $Header, $StartDate, $Date)
{
$Hosts = QueryData $BaseURL $Header "inventory/hosts/"
foreach ($hostItem in $Hosts)
{
Write-Host $hostItem.Name -ForegroundColor Green
$Advisories = QueryData $BaseURL $Header ("inventory/hosts/" + $hostItem.id + "/advisories/?modified__gte=" + $Date)
if ($Advisories.count -eq 0)
{
Write-Host " " "No Advisories Since " $StartDate
}
else
{
foreach ($item in $Advisories)
{
Write-Host " " $item.advisory_identifier $item.title $item.modified_date
}
}
}
}
#####################################
# Get Advisories Data since this date
$StartDate = "9/1/2018"
#####################################
$date1 = Get-Date -Date "01/01/1970"
$date2 = Get-Date -Date $StartDate
$UnixDate = (New-TimeSpan -Start $date1 -End $date2).TotalSeconds
ShowHostData $Sites[1] $Header $StartDate $UnixDate
Display-Errors