Public/Get-JuribaImportVulnerability.ps1

#Requires -Version 7
function Get-JuribaImportVulnerability {
    <#
        .SYNOPSIS
        Gets one or more vulnerabilities from the import API.

        .DESCRIPTION
        Gets a Vulnerability from the import API.
        Takes the ImportId as an input.
        Optionally takes a UniqueIdentifier as an input and will return a single Vulnerability with that UniqueIdentifier.
        Optionally takes a Name as an input and will return all Vulnerabilities matching that name.
        Optionally takes a Filter as an input and will return all Vulnerabilities matching that filter. See swagger documentation for examples of using filters.
        If specified, only one of UniqueIdentifier, Name or Filter can be supplied. Omit all to return all Vulnerabilities for the import.

        .PARAMETER Instance

        Optional. Juriba DPC instance to be provided if not authenticating using Connect-Juriba. For example, https://myinstance.dashworks.app:8443

        .PARAMETER APIKey

        Optional. API key to be provided if not authenticating using Connect-Juriba.

        .PARAMETER UniqueIdentifier

        UniqueIdentifier for the Vulnerability. Cannot be used with Name or Filter.

        .PARAMETER ImportId

        ImportId for the Vulnerability.

        .PARAMETER Name

        Name for the Vulnerability. Cannot be used with UniqueIdentifier or Filter.

        .PARAMETER Filter

        Filter for Vulnerability search. Cannot be used with Name or UniqueIdentifier.

        .PARAMETER InfoLevel

        Optional. Sets the level of information that this function returns. Accepts Basic or Full.
        Basic returns only the UniqueIdentifier, use when confirming a Vulnerability exists.
        Full returns the full json object for the Vulnerability.
        Default is Basic.

        .EXAMPLE
        PS> Get-JuribaImportVulnerability -Instance "https://myinstance.dashworks.app:8443" -APIKey "xxxxx" -ImportId 1 -InfoLevel "Full"

        .EXAMPLE
        PS> Get-JuribaImportVulnerability -Instance "https://myinstance.dashworks.app:8443" -APIKey "xxxxx" -ImportId 1 -UniqueIdentifier "CVE-1999-0095" -InfoLevel "Basic"

        .EXAMPLE
        PS> Get-JuribaImportVulnerability -Instance "https://myinstance.dashworks.app:8443" -APIKey "xxxxx" -ImportId 1 -Name "CVE-1999-0095"

        .EXAMPLE
        PS> Get-JuribaImportVulnerability -Instance "https://myinstance.dashworks.app:8443" -APIKey "xxxxx" -ImportId 1 -Filter "eq(severityScore, '3.6')"

    #>

    [OutputType([Object[]])]
    [CmdletBinding(DefaultParameterSetName="Default")]
    param (
        [Parameter(Mandatory=$false)]
        [string]$Instance,
        [Parameter(Mandatory=$false)]
        [string]$APIKey,
        [parameter(Mandatory=$false, ParameterSetName="UniqueIdentifier")]
        [string]$UniqueIdentifier,
        [parameter(Mandatory=$false, ParameterSetName="Name")]
        [string]$Name,
        [parameter(Mandatory=$false, ParameterSetName="Filter")]
        [string]$Filter,
        [parameter(Mandatory=$true)]
        [int]$ImportId,
        [parameter(Mandatory=$false)]
        [ValidateSet("Basic", "Full")]
        [string]$InfoLevel = "Basic"
    )
    if ((Get-Variable 'dwConnection' -Scope 'Global' -ErrorAction 'Ignore') -and !$APIKey -and !$Instance) {
        $APIKey = ConvertFrom-SecureString -SecureString $dwConnection.secureAPIKey -AsPlainText
        $Instance = $dwConnection.instance
    }

    if ($APIKey -and $Instance) {
        $limit = 50 # page size
        $uri = "{0}/apiv2/imports/{1}/vulnerabilities" -f $Instance, $ImportId
    
        switch ($PSCmdlet.ParameterSetName) {
            "UniqueIdentifier" {
                $uri += "/{0}" -f $UniqueIdentifier
            }
            "Name" {
                $uri += "?filter="
                $uri += [System.Web.HttpUtility]::UrlEncode("eq(name,'{0}')" -f $Name)
                $uri += "&limit={0}" -f $limit
            }
            "Filter" {
                $uri += "?filter="
                $uri += [System.Web.HttpUtility]::UrlEncode("{0}" -f $Filter)
                $uri += "&limit={0}" -f $limit
            }
            Default {
                $uri += "?limit={0}" -f $limit
            }
        }
    
        $headers = @{
            'x-api-key' = $APIKey
            'cache-control' = 'no-cache'
        }
    
        $vulnerability = @()
        try {
            $result = Invoke-WebRequest -Uri $uri -Method GET -Headers $headers -ContentType "application/json"
            if ($result.Content -eq '[]') {return $vulnerability}
            $vulnerability = switch($InfoLevel) {
                "Basic" { ($result.Content | ConvertFrom-Json).UniqueIdentifier }
                "Full"  { $result.Content | ConvertFrom-Json }
            }
            # check if result is paged, if so get remaining pages and add to result set
            if ($result.Headers.ContainsKey("X-Pagination")) {
                $totalPages = ($result.Headers."X-Pagination" | ConvertFrom-Json).totalPages
                for ($page = 2; $page -le $totalPages; $page++) {
                    $pagedUri = $uri + "&page={0}" -f $page
                    $pagedResult = Invoke-WebRequest -Uri $pagedUri -Method GET -Headers $headers -ContentType "application/json"
                    $vulnerability += switch($InfoLevel) {
                        "Basic" { ($pagedResult.Content | ConvertFrom-Json).UniqueIdentifier }
                        "Full"  { $pagedResult.Content | ConvertFrom-Json }
                    }
                }
            }
            return $vulnerability
        }
        catch { 
            if ($_.Exception.Response.StatusCode.Value__ -eq 404) {
                # 404 means the Vulnerability was not found, don't treat this as an error
                # as we expect this function to be used to check if a Vulnerability exists
                Write-Verbose "Vulnerability not found"
            } else {
                Write-Error $_
            }
        }

    } else {
        Write-Error "No connection found. Please ensure `$APIKey and `$Instance is provided or connect using Connect-Juriba before proceeding."
    }
}