public/account/Add-PSTSAccount.ps1

<#
.DESCRIPTION
Connect to azuredevops with your PAT.
 
.PARAMETER account
Devops organisation.
 
.PARAMETER Endpoint
Specifies the endpoint. dev.azure.com
 
.PARAMETER personalAccessToken
Your PAT
#>

function Add-PSTSAccount {
   param(
      [Parameter(Mandatory=$true)] [string] $account,
      [Parameter(Mandatory=$true)] [string] $endPoint,
      [string] $personalAccessToken = $null
   )

   begin {
        $credPath = "$([Environment]::GetFolderPath('ApplicationData'))/REST-Devops"

        if ((Test-Path -Path $credPath ) -eq $false) { mkdir $credPath }

        $credFile= "$($credPath)/$($account)"
   }

   process {

       $token = [string]::Empty

       if ($personalAccessToken -eq $([string]::Empty)) {

            #test load cache lookup
            if ((Test-Path -Path $credFile ) -eq $false) {
                read-host `
                -assecurestring | `
                convertfrom-securestring | `
                out-file $credFile
            }

            $pwd = (get-content $credFile) `
            | ConvertTo-SecureString

            $creds= New-Object System.Management.Automation.PSCredential($account, $pwd)
            $token = $creds.GetNetworkCredential().Password
        }
        else
        {
            $token = $personalAccessToken

            $tmp = $($token | ConvertTo-SecureString -AsPlainText -Force)
            $tmp| convertfrom-securestring | out-File $credFile

        }


   }

    end
    {
        [Environment]::SetEnvironmentVariable("ACCOUNT",$account)
        [Environment]::SetEnvironmentVariable("TOKEN",[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$token")))
        [Environment]::SetEnvironmentVariable("ENDPOINT",$endPoint)

    }
}