Skip to content

Instantly share code, notes, and snippets.

@rezarahimian
Last active December 3, 2019 01:40
Show Gist options
  • Save rezarahimian/76d69a19f9416cc2b5eb9fdc57b16da4 to your computer and use it in GitHub Desktop.
Save rezarahimian/76d69a19f9416cc2b5eb9fdc57b16da4 to your computer and use it in GitHub Desktop.
[CmdletBinding()]
PARAM(
[Parameter(Mandatory=$true)][String] $CredentialPath,
[Parameter(Mandatory=$true)][String] $Scope
)
try
{
$Toekn = $null
$Credential = [System.Management.Automation.PSCredential](Import-Clixml -Path $CredentialPath)
$EndPointURL = 'https://prd1.private.local/openam/token'
Write-Verbose -Message ('Connecting to "{0}"...' -f $EndPointURL)
Write-Verbose -Message ('Using "{0}" scope to create access token...' -f $Scope)
$Payload = @{
"token_type"="USERNAME";
"username"="svc_prd";
"password"=[System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Credential.Password))
"scope"=$Scope
} | ConvertTo-Json
Write-Verbose -Message ('Creating access token for "{0}"...' -f $Credential.UserName)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$Token = Invoke-RestMethod -Method Post -Uri $EndPointURL -Body $Payload -ContentType 'application/json' -Verbose
Write-Verbose -Message ('Created access token successfully : {0}' -f ($Token | Out-String))
}
catch
{
Write-Verbose -Message $_.Exception.Message
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment