Created
October 2, 2018 15:10
-
-
Save huzzeytech/b0e001b9160f8c144feb8157f378ca80 to your computer and use it in GitHub Desktop.
Sample auth with domain user in PowerShell to Secret Server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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