ps/Modules/Cole.PowerShell.Developer/Public/Initialize-AWSCredentials.ps1

48 lines
1.8 KiB
PowerShell
Raw Permalink Normal View History

2023-05-30 22:51:22 -07:00
function Initialize-AWSCredentials {
<#
.SYNOPSIS
Use this to initialize the AWS Credentials file on your machine
#>
[CmdletBinding()]
[OutputType([void])]
param (
[Parameter(Mandatory = $true)]
$Username = "$($env:username)-cli",
[Parameter(Mandatory = $false)]
$CredentialsFilePath = '~/.aws/credentials',
[Parameter(Mandatory = $false)]
$ConfigFilePath = '~/.aws/credentials'
)
$logLead = (Get-LogLeadName)
if (Test-Path $CredentialsFilePath) {
throw "$logLead : The file at [$CredentialsFilePath] already exists. Not going to reinitialize."
}
if (Test-Path $ConfigFilePath) {
throw "$logLead : The file at [$ConfigFilePath] already exists. Not going to reinitialize."
}
Write-Host "$logLead : Proceeding with username [$Username]."
$qrPngPath = (Expand-Path '~/Desktop/AWS_MFA_QR.png')
$newIdentityRaw = (aws iam create-virtual-mfa-device --virtual-mfa-device-name $Username --outfile $qrPngPath --bootstrap-method QRCodePNG --no-verify-ssl)
$newIdentity = (ConvertFrom-Json ($newIdentityRaw | Out-String))
$virtualMFADeviceSerialNumber = $newIdentity.VirtualMFADevice.SerialNumber
Start-Process $qrPngPath
# get input #1
$code1 = Read-Host "Please enter the first MFA device generated value"
# get input #2
$code2 = Read-Host "Please enter the second MFA device generated value"
(aws iam enable-mfa-device --user-name $Username --serial $virtualMFADeviceSerialNumber --authentication-code1 $code1 --authentication-code2 $code2 --no-verify-ssl)
$RoleName = (Get-AWSConfigRoleNameForUser)
New-AWSCredentialsFile -FilePath $CredentialsFilePath
New-AWSConfigFile -FilePath $ConfigFilePath -virtualMFADeviceSerialNumber $virtualMFADeviceSerialNumber -RoleName $RoleName
}