Skip to content

Instantly share code, notes, and snippets.

@rroman81
Created June 19, 2018 16:52
Show Gist options
  • Save rroman81/b2cbe7fd3800c29872277b2e7e864e20 to your computer and use it in GitHub Desktop.
Save rroman81/b2cbe7fd3800c29872277b2e7e864e20 to your computer and use it in GitHub Desktop.
function Set-AwsMfaToken()
{
param(
[Parameter(Mandatory=$True,Position=1)]
[string]$TokenCode= $(throw "Please provide the MFA token"),
[Parameter(Mandatory=$False,Position=2)]
[string]$AwsProfile="<Name of the AWS profile>",
[Parameter(Mandatory=$False,Position=3)]
[string]$SerialNumber = "<ARN of the IAM principal"
)
$results = &{ aws sts get-session-token --serial-number $SerialNumber --token-code $TokenCode --profile default | ConvertFrom-Json }
if ($results.Credentials -ne $null)
{
aws configure set aws_access_key_id $results.Credentials.AccessKeyId --profile $AwsProfile
aws configure set aws_secret_access_key $results.Credentials.SecretAccessKey --profile $AwsProfile
aws configure set aws_session_token $results.Credentials.SessionToken --profile $AwsProfile
if (-not (Test-Path -Path env:AWS_PROFILE))
{
New-Item -Path env:. -Name AWS_Profile -Value $AwsProfile
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment