Guest User

Untitled

a guest
Jun 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. function Set-AwsMfaToken()
  2. {
  3. param(
  4. [Parameter(Mandatory=$True,Position=1)]
  5. [string]$TokenCode= $(throw "Please provide the MFA token"),
  6. [Parameter(Mandatory=$False,Position=2)]
  7. [string]$AwsProfile="<Name of the AWS profile>",
  8. [Parameter(Mandatory=$False,Position=3)]
  9. [string]$SerialNumber = "<ARN of the IAM principal"
  10. )
  11.  
  12. $results = &{ aws sts get-session-token --serial-number $SerialNumber --token-code $TokenCode --profile default | ConvertFrom-Json }
  13. if ($results.Credentials -ne $null)
  14. {
  15. aws configure set aws_access_key_id $results.Credentials.AccessKeyId --profile $AwsProfile
  16. aws configure set aws_secret_access_key $results.Credentials.SecretAccessKey --profile $AwsProfile
  17. aws configure set aws_session_token $results.Credentials.SessionToken --profile $AwsProfile
  18. if (-not (Test-Path -Path env:AWS_PROFILE))
  19. {
  20. New-Item -Path env:. -Name AWS_Profile -Value $AwsProfile
  21. }
  22. }
  23. }
Add Comment
Please, Sign In to add comment