functions/Add-BlackDuckConnector.ps1

<#
.SYNOPSIS
  
Create a single Black Duck connector
  
.DESCRIPTION
Adds a single Black Duck connector using the tool connector config API endpoints
  
.EXAMPLE
Add-BlackDuckConnector "My Code Dx Project" "Black Duck Connector 1" "My Black Duck Project" "1.0.0" https://samplehostname.blackducksoftware.com/ username@email.com mypw1234 "15:00"
 
Output (entry ID)
 
  
#>


Function Add-BlackDuckConnector
{
    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$ProjectName,
        [Parameter(Mandatory=$true)]
        [string]$NewConnectorName,
        [Parameter(Mandatory=$true)]
        [string]$ToolProjectName,
        [Parameter(Mandatory=$true)]
        [string]$ToolProjectVersion,
        [Parameter(Mandatory=$true)]
        [string]$HostURL,
        [Parameter(Mandatory=$true)]
        [string]$UserName,
        [Parameter(Mandatory=$true)]
        [string]$Password,
        [Parameter(Mandatory=$false)]
        [string]$RefreshTime
    )
    
    # Get the Code Dx project ID from the name
    $projects = Get-Projects
    $ProjectHash = @{}
    $projects.projects | ForEach-Object {
        $ProjectHash[$_.name] = $_.id
    }
    $ProjectID = $ProjectHash.$ProjectName
    
    # Create a blank tool connector
    $ConnectorInfo = Add-BlankConnector $ProjectID "Black Duck Hub" $NewConnectorName
    $ConnectorID = $ConnectorInfo.id
    
    
    # Get the Black Duck tool project ID
    $ToolProjectID = Get-ToolProject "project" $ToolProjectName $ConnectorID $HostURL $UserName $Password

    # Get the Black Duck tool version ID
    $ToolVersionID = Get-ToolVersion "version" $ToolProjectID $ToolProjectVersion $ConnectorID $HostURL $UserName $Password


    # Update the blank tool connector with the proper configuration info
    $uri = $CDXSERVER + "/x/tool-connector-config/values/" + $ConnectorID

    If($RefreshTime -ne "") {
        $RefreshInterval = @{ daily = $RefreshTime }
    }else {
        $RefreshInterval = $false
    }
    
    $body = Convertto-Json @{
        project = $ToolProjectID
        version = $ToolVersionID
        "auto-refresh-interval" = $RefreshInterval
        server_url = $HostURL
        username = $UserName
        password = $Password
    }
    $CreateBlackDuck = Invoke-RestMethod -Uri $uri -Method Put -Body $body -Headers $headers -ContentType "application/json" 
    Write-Verbose ( $CreateBlackDuck | Format-Table | Out-String )

}