<# Copy AWS keys from IAM files (cp3-XXXX) into matching ServerInstallNotes files. Set-ExecutionPolicy RemoteSigned -Scope CurrentUser .DESCRIPTION For each project number: - Read P:\...\CustomersIAM\cp3-XXXX - Extract aws_access_key_id and aws_secret_access_key - Find \\10.10.1.17\Projects\202 Artemis\Installs\XXXX - *\ - Update *ServerInstallNotes* file in that folder Skips any ServerInstallNotes file that already contains BOTH values. #> param( # Root folder where the cp3-XXXX IAM key files live [string]$IamRoot = 'P:\202 Artemis\5 - Software\CustomersIAM', # Change drive letter to whatever yours is # Root folder where the project install folders live [string]$InstallsRoot = '\\10.10.1.17\Projects\202 Artemis\Installs', # Project numbers to process, e.g. 0246,0247,0250 [string[]]$ProjectNumbers ) if (-not $ProjectNumbers -or $ProjectNumbers.Count -eq 0) { $inputString = Read-Host "Enter project numbers (e.g. 0246,0247,0250)" $ProjectNumbers = $inputString.Split(',') | ForEach-Object { $_.Trim() } | Where-Object { $_ } } foreach ($num in $ProjectNumbers) { Write-Host "----------`nProcessing project $num ..." # 1) Find IAM file for this project $iamFile = Get-ChildItem -Path $IamRoot -ErrorAction SilentlyContinue | Where-Object { -not $_.PSIsContainer -and ($_.Name -like "cp3-$num*" -or $_.Name -like "$num*") } | Select-Object -First 1 if (-not $iamFile) { Write-Warning " No IAM file found for cp3-$num in $IamRoot. Skipping." continue } $iamContent = Get-Content -Path $iamFile.FullName -Raw # Extract aws_access_key_id if ($iamContent -notmatch 'aws_access_key_id\s*=\s*(\S+)') { Write-Warning " Could not find aws_access_key_id in $($iamFile.Name). Skipping." continue } $awsId = $matches[1] # Extract aws_secret_access_key if ($iamContent -notmatch 'aws_secret_access_key\s*=\s*["'']?([^"''\r\n]+)') { Write-Warning " Could not find aws_secret_access_key in $($iamFile.Name). Skipping." continue } $awsSecret = $matches[1] Write-Host " Found keys in $($iamFile.Name)." # 2) Find the project folder in Installs $projectFolder = Get-ChildItem -Path $InstallsRoot -Directory -ErrorAction SilentlyContinue | Where-Object { $_.Name -like "*$num*" } | Select-Object -First 1 if (-not $projectFolder) { Write-Warning " No project folder starting with '$num -' found under $InstallsRoot. Skipping." continue } Write-Host " Project folder: $($projectFolder.FullName)" # 3) Find the Server Install Notes file $serverNotes = Get-ChildItem -Path $projectFolder.FullName -File -ErrorAction SilentlyContinue | Where-Object { $_.Name -like "*ServerInstallNotes*.txt" } | Select-Object -First 1 if (-not $serverNotes) { Write-Warning " No *ServerInstallNotes* file found in $($projectFolder.FullName). Skipping." continue } Write-Host " Server notes file: $($serverNotes.FullName)" # 4) Read server notes $notesText = Get-Content -Path $serverNotes.FullName -Raw # 5) Skip if both values already present if ($notesText -match [Regex]::Escape($awsId) -and $notesText -match [Regex]::Escape($awsSecret)) { Write-Host " Keys already present in server notes. Skipping update." continue } # 6) Replace the AWS block in the Server Install Notes $updatedText = $notesText # Patterns $patternBoth = '(?m)^AWS Access Key ID.*\r?\nAWS Secret Access Key.*$' $patternId = '(?m)^AWS Access Key ID.*$' $patternSecret = '(?m)^AWS Secret Access Key.*$' $replacementBlock = "AWS Access Key ID - $awsId`r`nAWS Secret Access Key - $awsSecret" if ($updatedText -match $patternBoth) { # Normal case: ID and Secret on two consecutive lines $updatedText = [regex]::Replace($updatedText, $patternBoth, $replacementBlock, 1) } elseif ($updatedText -match $patternId -and $updatedText -match $patternSecret) { # Both exist but not adjacent (weird edge case) $updatedText = [regex]::Replace($updatedText, $patternSecret, '', 1) $updatedText = [regex]::Replace($updatedText, $patternId, $replacementBlock, 1) } elseif ($updatedText -match $patternId) { # Only ID line exists $updatedText = [regex]::Replace($updatedText, $patternId, $replacementBlock, 1) } elseif ($updatedText -match $patternSecret) { # Only Secret line exists $updatedText = [regex]::Replace($updatedText, $patternSecret, $replacementBlock, 1) } else { # No AWS section at all – append at end as a fallback $updatedText += "`r`n$replacementBlock" } # 7) Write back to file Set-Content -Path $serverNotes.FullName -Value $updatedText -Encoding UTF8 Write-Host " Updated AWS keys in server notes for project $num.`n" } Write-Host "Done."