ps/Modules/Cole.PowerShell.Developer/Public/Get-AWSAccessKey.ps1

61 lines
2.0 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Get-AWSAccessKey {
<#
.SYNOPSIS
Update the AWS access key and secret in a reasonable fashion
.PARAMETER RoleToReplaceFor
The role you are replacing the key value for. Example: [teamcity-packer] or [Prod]
.PARAMETER Key
The value given by the Access Key ID for AWS when choosing a new IAM Access Key
.PARAMETER Secret
The value given by the secret for AWS when choosing a new IAM Access Key
.PARAMETER ComputerName
Denotes the computers you wish to change the value on
#>
param (
[Parameter(Mandatory = $true, Position = 0)]
[ValidateNotNullOrEmpty()]
[Alias('ProfileName')]
[string]$RoleToReplaceFor,
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[Alias('Servers')]
[string[]]$ComputerName = (Get-CachedInstances -ProfileName temp-prod -TeamCity).Hostname
)
if (-not $RoleToReplaceFor.StartsWith("[")) {
$RoleToReplaceFor = "[$RoleToReplaceFor"
}
if (-not $RoleToReplaceFor.EndsWith("]")) {
$RoleToReplaceFor = "$RoleToReplaceFor]"
}
Write-Host "$logLead : Replacing key for profile $RoleToReplaceFor with key: $AccessKeyId"
Invoke-Command -ComputerName $ComputerName -ArgumentList ($RoleToReplaceFor) -ScriptBlock {
param ($sb_role)
$userPaths = @("C:\Users\ci.migrate`$\.aws\credentials", "C:\Users\jumpbox.jenkins\.aws\credentials")
foreach ($path in $userPaths) {
if (-not (Test-Path -Path $path)) {
continue
}
try {
$nextLine = $false
(Get-Content -Path $path) | ForEach-Object {
if ($nextLine) {
Write-Host "$($env:computername) : $_"
$nextLine = $false
return
} else {
if ($_ -eq $sb_role) {
$nextLine = $true
}
}
}
} catch {}
}
}
}