Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- }
- }
- }
Add Comment
Please, Sign In to add comment