Public/Authentication/Connect-ActiveBatch.ps1

Function Connect-ActiveBatch {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string] $Uri,
        [Parameter(Mandatory = $true)]
        [string] $JobScheduler,

        [Parameter(Mandatory = $false)]
        [pscredential] $Credential,

        # Parameter help description
        [Parameter(Mandatory = $false)]
        [int] $ValidForSeconds = 120,
        [switch] $ReturnResponse
    )


    $_ABSession.Uri = Get-ABPath "Base" -PathParameters @{
        Scheme = ([uri]$Uri).Scheme
        Uri    = ([uri]$Uri).host
    }

    $_ABSession.Credential = $Credential
    $Path = Get-ABPath "Auth" 

    
    try {
        $Response = Invoke-ABRestMethod -uri "$($_ABSession.Uri)$Path" -Method POST -Headers @{
            accept = "application/json"
        } -ContentType "application/json" -body @{
            jobScheduler = $JobScheduler
            username     = $Credential.UserName
            password     = $Credential.GetNetworkCredential().Password
        }
    }
    catch {
        $_ABSession.isValid = $false
        $_ABSession.InvalidReason = $_
        $_ABSession.Token = $null
    }
    
    if ($Response) {
        $_ABSession.Token = $Response.token
        $_ABSession.isValid = $true
        $_ABSession.InvalidReason = ""
        $_ABSession.Expires = (Get-Date).AddSeconds(($Response.ValidForSeconds))
    }
    
    if ($returnResponse) { Return $_ABSession }
}