functions/Get-ToolVersion.ps1

<#
.SYNOPSIS
  
Returns tool version ID for given product ID and version name.
  
.DESCRIPTION
Returns tool version ID for given product ID and version name using the CodeDx endpoint.
  
.EXAMPLE
Get-ToolVersion "version" myProject "1.0.0" 63 https://samplehostname.blackducksoftware.com/ username@domainname.com password1234
 
Output
1
  
#>


Function Get-ToolVersion
{
    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$FieldName,
        [Parameter(Mandatory=$true)]
        [string]$ToolProjectID,
        [Parameter(Mandatory=$true)]
        [string]$ToolVersionName,
        [Parameter(Mandatory=$true)]
        [string]$ConnectorID,
        [Parameter(Mandatory=$true)]
        [string]$HostURL,
        [Parameter(Mandatory=$true)]
        [string]$UserName,
        [Parameter(Mandatory=$true)]
        [string]$Password
    )

    if ($ToolVersionName -eq "latest") {
        return "cdx_use_latest_ver"
    }
    
    $uri = $CDXSERVER + "/x/tool-connector-config/values/" + $ConnectorID + "/populate/" + $FieldName

    $body = Convertto-Json @{
        host_url = $HostURL
        server_url = $HostURL
        username = $UserName
        password = $Password
        project = $ToolProjectID
    }

    $ToolVersions = Invoke-RestMethod -Uri $uri -Method Post -Body $body -Headers $headers -ContentType "application/json" 
    Write-Verbose ( $ToolVersions | Format-Table | Out-String )

    # Get the tool project ID from the name
    $ToolVersions | ForEach-Object {
        if($_.display -eq $ToolVersionName) {
            $ToolVersionID = $_.value
        }
    }
    Write-Verbose ($ToolVersionID)
    return $ToolVersionID
}