public/Connect-NVIDIADLSServiceInstance.ps1

 <#
    .SYNOPSIS
    Connects to a NVIDIA DLS service instance.
 
    .DESCRIPTION
    Connects to a NVIDIA DLS service instance.
 
    .EXAMPLE
    Connect-NVIDIADLSServiceInstance -Server 'nls.fqdn' -Credential $credential
 
    .NOTES
    Tested on NVIDIA DLS 3.5.0.
 
    .OUTPUTS
    None.
 
    .LINK
    https://ui.licensing.nvidia.com/api-doc/dls-api-docs.html
#>


function Connect-NVIDIADLSServiceInstance {
    [CmdletBinding()]
    [OutputType([Void])]
    param (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]$Server,

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]$Credential,
        
        [Parameter(Mandatory = $false)]
        [Switch]$SkipCertificateCheck
    )

    begin {
        try {
            $splat = @{}

            if ($PSBoundParameters.ContainsKey('SkipCertificateCheck')) {
                $splat.Add('SkipCertificateCheck', $true)
            }

            $headers = @{ 'Content-Type' = 'application/json' }
  
            $body = @{
                'username' = $credential.GetNetworkCredential().UserName
                'password' = $credential.GetNetworkCredential().Password
            } | ConvertTo-Json -Compress
        }
        catch {
            throw $_
        }
    }

    process {
        try {
            $response = Invoke-RestMethod -Method Post -Uri ('https://{0}/auth/v1/login' -f $Server) -Headers $headers -Body $body @splat
    
            if ($null -eq $response) {
                throw $_
            }

            Set-Variable -Name ('_NVIDIA_DLS_{0}' -f $Server) -Scope Global -Value (ConvertTo-SecureString -AsPlaintext -Force -String $response.token)

            if ($null -eq ${global:_NVIDIA_DLS_Default_Server}) {
                Set-Variable -Name '_NVIDIA_DLS_Default_Server' -Scope Global -Value $Server
            }

            Set-Variable -Name ('_NVIDIA_DLS_{0}_SkipCertificateCheck' -f $Server) -Scope Global -Value $SkipCertificateCheck
        }
        catch {
            Write-Error -Message ("Error encountered logging into NVIDIA DLS {0}: {1}" -f $Server, $_) -RecommendedAction 'Verify username and password and that the server is operational'
        }
    }

    end { }
}