Skip to content

Instantly share code, notes, and snippets.

@huzzeytech
Created October 2, 2018 15:10
Show Gist options
  • Save huzzeytech/b0e001b9160f8c144feb8157f378ca80 to your computer and use it in GitHub Desktop.
Save huzzeytech/b0e001b9160f8c144feb8157f378ca80 to your computer and use it in GitHub Desktop.
Sample auth with domain user in PowerShell to Secret Server
$application = "https://vault.thycotic.fun"
# Define the user credentials
$username = "domain\username";
$password = "password"; # Read-Host -Prompt "Enter your password: "
Function Get-Token
{
[CmdletBinding()]
Param(
[Switch] $UseTwoFactor
)
$creds = @{
username = $username
password = $password
grant_type = "password"
};
$headers = $null
If ($UseTwoFactor) {
$headers = @{
"OTP" = (Read-Host -Prompt "Enter your OTP for 2FA: ")
}
}
try
{
$response = Invoke-RestMethod "$application/oauth2/token" -Method Post -Body $creds -Headers $headers;
$token = $response.access_token;
return $token;
}
catch
{
$result = $_.Exception.Response.GetResponseStream();
$reader = New-Object System.IO.StreamReader($result);
$reader.BaseStream.Position = 0;
$reader.DiscardBufferedData();
$responseBody = $reader.ReadToEnd() | ConvertFrom-Json
Write-Host "ERROR: $($responseBody.error)"
return;
}
}
Get-Token
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment