
Write-Host ""
Write-Host "*********************************************************"
Write-Host "Press Enter to open a browser and login to Azure DevOps"
Write-Host "Create a Personal Access Token and copy it to clipboard"
Write-Host "Set Expiration 90 days. Code Read."
Write-Host "*********************************************************"
Write-Host ""
Read-Host " "

Start-Process "https://dev.azure.com/capita-ess/_usersSettings/tokens"

Write-Host ""
Write-Host "*********************************************************"
Write-Host "Paste in Personal Access Token."
Write-Host "The screen will be cleared afterwards."
Write-Host "Press close in the browser dialog"
Write-Host "*********************************************************"
Write-Host ""

$PAT_KEY = Read-Host "Enter your Personal Access Token: "

$Env:PAT_KEY_B64 = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("`:$PAT_KEY"))
[Environment]::SetEnvironmentVariable('PAT_KEY_B64', $PAT_KEY_B64, 'User')

Clear-Host

if ($null -eq $Env:PAT_KEY_B64 -OR ($null -eq $Env:PAT_KEY))
{
    Write-Error "PAT Save failed"
}
else
{
    Write-host "PAT Save successful"
}
