Public/Providers.ps1

function New-TMProvider {
    [CmdletBinding(DefaultParameterSetName = 'ByName')]
    param(
        [Parameter(Mandatory = $false)]
        [psobject] $TMSession = 'Default',
        [Parameter(
            Mandatory = $true,
            ParameterSetName = 'ByName',
            Position = 0)]
        [string] $Name,
        [Parameter(Mandatory = $true,
            ParameterSetName = 'ByObject',
            Position = 0)]
        [psobject] $Provider,
        [Parameter(Mandatory = $false)]
        [switch] $PassThru
    )

    $TMSession = Get-TMSession $TMSession

    if ($PSCmdlet.ParameterSetName -eq 'ByName') {
        $Provider = New-Object -TypeName pscustomobject -Property @{ name = $Name }
    }

    $ProviderCheck = Get-TMProvider -Name $Provider.name
    if ($ProviderCheck) {
        Write-Warning 'The provider already exists:'
        return $ProviderCheck
    }

    $RestSplat = @{
        Uri                  = "https://$($TMSession.TMServer)/tdstm/api/provider"
        Method               = 'POST'
        WebSession           = $TMSession.TMRestSession
        StatusCodeVariable   = 'StatusCode'
        SkipHttpErrorCheck   = $true
        SkipCertificateCheck = $TMSession.AllowInsecureSSL
        Body                 = (@{
                name        = $Provider.name
                description = $Provider.description
                comment     = $Provider.comment
                project     = $TMSession.UserContext.Project.Id

            } | ConvertTo-Json -Depth 5 -Compress)
    }

    try {
        $Response = Invoke-RestMethod @RestSplat
        if ($StatusCode -eq 200) {
            if ($PassThru) {
                return $Response
            }
        } else {
            throw "The response status code $StatusCode does not indicate success: $Response"
        }
    } catch {
        throw "Error creating Provider: $_"
    }
}


function Get-TMProvider {
    param(
        [Parameter(Mandatory = $false)][PSObject]$TMSession = 'Default',
        [Parameter(Mandatory = $false)][String]$Name,
        [Parameter(Mandatory = $false)][Switch]$ResetIDs
    )

    $TMSession = Get-TMSession $TMSession

    $RestSplat = @{
        Uri                  = "https://$($TMSession.TMServer)/tdstm/api/provider?project=$($TMsession.UserContext.Project.Id)"
        Method               = 'GET'
        WebSession           = $TMSession.TMRestSession
        StatusCodeVariable   = 'StatusCode'
        SkipHttpErrorCheck   = $true
        SkipCertificateCheck = $TMSession.AllowInsecureSSL
    }

    try {
        $Response = Invoke-RestMethod @RestSplat
        if ($StatusCode -eq 200) {
            $Response = $Response | Sort-Object -Property 'name'

            if ($ResetIDs) {
                foreach ($Result in $Response) {
                    $Result.id = $null
                }
            }

            if ($Name) {
                return ($Response | Where-Object { $_.name -eq $Name })
            } else {
                return $Response
            }

        } else {
            throw "The response status code $StatusCode does not indicate success: $Response"
        }
    } catch {
        throw "Error retrieving Providers: $_"
    }
}