How to generate an access token using PowerShell
Gord Russell wrote auth.ps1 (thanks!) to generate an access token using Microsoft PowerShell. It implements the Invoke-RestMethod to send an HTTPS POST request to the VSS RESTful web service, then PowerShell deserializes the JavaScript Object Notation (JSON) content into objects.
Â
By calling the script as $TK = .\auth.ps1, $TK is populated with a PS object containing the token.
Step-by-step guide
The following steps explain how auth.ps1 works:
Request username and password:
$user = Read-Host -Prompt 'User name' $pass = Read-Host -Prompt 'password' -AsSecureString
Convert secureString to plain text:
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass) $UnsecurePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
Generate request Headers with Authentication: Basic:
$pair = $user + ":" + $UnsecurePassword # Encode the string to the RFC2045-MIME variant of Base64, except not limited to 76 char/line. $bytes = [System.Text.Encoding]::ASCII.GetBytes($pair) $base64 = [System.Convert]::ToBase64String($bytes) # Create the Auth value as the method, a space, and then the encoded pair Method Base64String $basicAuthValue = "Basic $base64"
Make the POST request to /auth/request-token
After populating $TK, it can be used for any of the API calls by: