Skip to content

Instantly share code, notes, and snippets.

@junkangli
Last active May 19, 2019 16:45
Show Gist options
  • Save junkangli/253716ed794fbf9dfaef0263916e6cf7 to your computer and use it in GitHub Desktop.
Save junkangli/253716ed794fbf9dfaef0263916e6cf7 to your computer and use it in GitHub Desktop.
PowerShell script to be used together with Okta AWS CLI Assume Role Tool to use the OKTA_PROFILE parameter configured to set the AWS profile name.
function Update-OktaConfigFile {
Param(
[Bool]$Uncomment,
[Int]$MatchLineNumber
)
$content = Get-Content -Path $PSScriptRoot\config.properties
$content |
ForEach-Object {
if ($_.ReadCount -eq $MatchLineNumber -or $_.ReadCount -eq $MatchLineNumber+1) {
if ($Uncomment) {
$_ -Replace "#", ""
} else {
"#" + $_
}
} else {
$_
}
} | Set-Content -Path $PSScriptRoot\config.properties
}
$profile = Read-Host -Prompt "Enter profile name"
$match = Select-String -Path $PSScriptRoot\config.properties -Pattern $profile
Update-OktaConfigFile -Uncomment $true -MatchLineNumber $match.LineNumber
$oktaConfig = Get-Content -Path $PSScriptRoot\config.properties -Raw | ConvertFrom-StringData
$oktaProfile = $oktaConfig.OKTA_PROFILE
Write-Host "Setting profile: $oktaProfile" -BackgroundColor black -ForegroundColor green
okta-aws $oktaProfile sts get-caller-identity
Update-OktaConfigFile -Uncomment $false -MatchLineNumber $match.LineNumber
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment