PowerShell Script to Query Historic Advisories by Product and Version

The following is a PowerShell script to query historic advisories by product and version.

###############################################################################################################################################

$Site = ( "Account",     "https://api.app.flexerasoftware.com/api/"    ,"Token 0000000000000000000000000000000000000000")

###############################################################################^^^^^^^^^^^^^YOUR TOKEN HERE^^^^^^^^^^^^########################

$global:QueryLimit = 10000

$global:WebServiceHeader = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"

$global:WebServiceHeader.Add("Content-Type", 'application/json')

$global:WebServiceHeader.Add("Authorization", $Site[2])

$global:WebServiceURLSecunia = $Site[1]

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$global:ErrorArray = @()

function Display-Errors ()

{

    if ($global:ErrorArray.Count -eq 0)

    {

         #Write-Host (" All Good "  + (Write-Header)) $false

    }

    else

    {

        Write-Host  (" Errors: ") -ForegroundColor Yellow

        foreach ($item in $global:ErrorArray)

        {

            Write-Host  ("     " + $item ) -ForegroundColor Green

        }

    }

}

function DeleteData ($BaseURL, $Header, $URL)

{

    try

    {

        $result = Invoke-RestMethod ($BaseURL + $URL) -Method Delete -Headers $Header

    }

    catch

    {

        $global:ErrorArray += ("Error QueryData " + $BaseURL + $URL + " " +  $_.Exception.Message + " " + $_.Exception.ItemName)

    }

}

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 " + $result.next + " " +  $_.Exception.Message + " " + $_.Exception.ItemName)

            return $results

        }

    }

    return $results

}

function FindAssetList ($URL, $match)

{

    $Hosts = QueryData $global:WebServiceURLSecunia $global:WebServiceHeader $URL

    foreach  ($item in $Hosts)

    {

        if ($item.name -like $match)

        {

            Write-Host "Match Found" $item.id $item.name

            return $item.id

        }

        else

        {

            Write-Host "Match Not Found" $item.id $item.name

        }

    }

    return 0

}

function FindItem ($URL, $match)

{

    $items = QueryData $global:WebServiceURLSecunia $global:WebServiceHeader $URL

    foreach  ($item in $items)

    {

        if ($item.name -like $match)

        {

            Write-Host "Match Found" $item.id $item.name

            return $item.id

        }

        else

        {

            # Write-Host "Match Not Found" $item.id $item.name

        }

    }

    return 0

}

function DisplayRelatedData ($URL)

{

    $items = QueryData $global:WebServiceURLSecunia $global:WebServiceHeader $URL

    foreach  ($item in $items)

    {

        Write-Host "   " "Related Products:" -ForegroundColor Yellow

        foreach  ($product in $items.products)

        {

            Write-Host "                  " $product.name

        }

    }

}

function DisplaySAIDData ($URL)

{

    $items = QueryData $global:WebServiceURLSecunia $global:WebServiceHeader $URL

    foreach  ($item in $items)

    {

        Write-Host $item.advisory_identifier $item.title

        DisplayRelatedData ("advisories/" + $item.id + "/")

    }

}

$AssetListName = "CHrome"

$AssetListID = FindItem "asset-lists/" $AssetListName

if ($AssetListID -ne 0)

{

    DisplaySAIDData ("historic-advisories/?asset_list=" + $AssetListID)

}

Display-Errors