Functions/Update-AwsSessionToken.ps1
function Update-TAWSSessionToken { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [string] $TokenCode, [string] $AwsAccount='314566904004', [string] $UserName='boyce.yang', [string] $Region = 'us-east-1', [string] $ProfileName = "TAWSSessionToken" ) $ErrorActionPreference = 'Stop' if (!$TokenCode -or $TokenCode -notmatch '\d{6}') { $TokenCode = Read-Host -Prompt 'Please enter your MFA token code' } $MFADevice = "arn:aws:iam::$($AwsAccount):mfa/$UserName" $AWSSessionToken = Get-STSSessionToken -SerialNumber $MFADevice -TokenCode $TokenCode -ProfileName $ProfileName -DurationInSeconds 129600 $ProfileLocation = "$($env:USERPROFILE)\.aws\credentials" Initialize-AWSDefaultConfiguration -AccessKey $($AWSSessionToken.AccessKeyId) -SecretKey $($AWSSessionToken.SecretAccessKey) -SessionToken $($AWSSessionToken.SessionToken) -Region $Region -ProfileLocation $ProfileLocation Write-Host "Update the profile name [$ProfileName] temp key in the file [$ProfileLocation] with $MFADevice" } |