Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. <#
  2. .SYNOPSIS
  3. Assumes an AWS role based on the name configured in your ~/.aws/credentials file
  4.  
  5. .DESCRIPTION
  6. Assumes an AWS role based on the name configured in your ~/.aws/credentials file
  7.  
  8. .PARAMETER profile
  9. The name of the profile to assume (the item in your credentials file that has source_profile set)
  10.  
  11. .PARAMETER RoleSessionName
  12. The name of the role to assume for that profile. Eg Admin
  13.  
  14. .EXAMPLE
  15. Call this script with the parameters separated by spaces
  16.  
  17. #>
  18.  
  19. [CmdletBinding()]
  20. Param(
  21. [Parameter(Position=1)]
  22. [string]$profile = "profile NOT SET",
  23. [Parameter(Position=2)]
  24. [string]$RoleSessionName = "RoleSessionName NOT SET",
  25. [Parameter(Position=3)]
  26. [string]$ARN = "ARN NOT SET"
  27. )
  28.  
  29. $baseProfile = aws configure get "${profile}.source_profile"
  30. echo "profile:$profile"
  31. echo "RoleSessionName:$RoleSessionName"
  32. echo "baseProfile:$baseProfile"
  33. echo "ARN:$ARN"
  34.  
  35. $roleArn="arn:aws:iam::"+$ARN+":role/"+$RoleSessionName
  36. Set-AWSCredential -ProfileName $baseProfile
  37. Remove-Item -Path Env:AWS_ACCESS_KEY_ID
  38. Remove-Item -Path Env:AWS_SECRET_ACCESS_KEY
  39. Remove-Item -Path Env:AWS_SESSION_TOKEN
  40.  
  41. $Creds = (Use-STSRole -region ap-southeast-2 -RoleArn $roleArn -RoleSessionName $RoleSessionName).Credentials
  42.  
  43. Set-Item -Path Env:AWS_ACCESS_KEY_ID -Value $Creds.AccessKeyId
  44. Set-Item -Path Env:AWS_SECRET_ACCESS_KEY -Value $Creds.SecretAccessKey
  45. Set-Item -Path Env:AWS_SESSION_TOKEN -Value $Creds.SessionToken
  46. Remove-Item -Path Env:AWS_PROFILE
  47.  
  48.  
  49.  
  50. Get-ChildItem Env:AWS_*
  51.  
  52. echo "-----copy below to auth in linux/bash-------`n`n"
  53. Write-Host ("export AWS_ACCESS_KEY_ID={0}`nexport AWS_SECRET_ACCESS_KEY={1}`nexport AWS_SESSION_TOKEN={2}`n" -f $Creds.AccessKeyId,$Creds.SecretAccessKey,$Creds.SessionToken)
  54.  
  55. echo "Finished, press any key"
  56. $KeyPress = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement