Problem:
I had various text files with lists of email addresses in that I needed to add to an AWS JSON policy but needed formatting.
Solution:
The below text will format the list and insert it in to the policy ready to be copied into AWS. It also has a menu system so that you can identify policies with different SIDs.
$content = [System.IO.File]::ReadAllText(".\email.txt") $content = $content.Trim() [System.IO.File]::WriteAllText(".\email.txt", $content) function Show-Menu { param ( [string]$Title = 'My Menu' ) Clear-Host Write-Host "================ $Title ================" Write-Host "1: Press '1' To create a AmazonSesContosoDev policy." Write-Host "2: Press '2' To create a AmazonSesContosoTest policy." Write-Host "Q: Press 'Q' to quit." } Show-Menu –Title 'My Menu' $selection = Read-Host "Please make a selection" switch ($selection) { '1' { 'You chose option #1' $inputFile = Get-Content ".\email.txt" $collate = foreach($Obj in $inputFile) { $begin = '"' $end = '",' $begin + $Obj + $end } $outputFile = ".\email.txt" Set-Content -path $outputFile -value $collate $text = Get-Content -Path ".\email.txt" $lastLine = $text.Count - 1 $text[$lastLine] = $text[$lastLine] -replace ",","" Set-Content -Value $text -Path .\email.txt -Encoding Ascii $content = [System.IO.File]::ReadAllText(".\email.txt") $content = $content.Trim() [System.IO.File]::WriteAllText(".\email.txt", $content) $policy1 = @" `n{`n "Version": "2012-10-17", "Statement": [ { "Sid": "AmazonSesContosoDev", "Effect": "Allow", "Action": "ses:SendRawEmail", "Resource": "*", "Condition": { "StringLike": { "ses:FromAddress": [ "@ $policy2 = @" `n]`n } } } ] } "@ function Insert-Content { param ( [String]$Path ) process { $( ,$_; Get-Content $Path -ea SilentlyContinue) | Out-File $Path } } $policy1 | Insert-Content .\email.txt Add-Content -value $policy2 -path .\email.txt $content = [System.IO.File]::ReadAllText(".\email.txt") $content = $content.Trim() [System.IO.File]::WriteAllText(".\email.txt", $content) } '2' { 'You chose option #2' $inputFile = Get-Content ".\email.txt" $collate = foreach($Obj in $inputFile) { $begin = '"' $end = '",' $begin + $Obj + $end } $outputFile = ".\email.txt" Set-Content -path $outputFile -value $collate $text = Get-Content -Path ".\email.txt" $lastLine = $text.Count - 1 $text[$lastLine] = $text[$lastLine] -replace ",","" Set-Content -Value $text -Path .\email.txt -Encoding Ascii $content = [System.IO.File]::ReadAllText(".\email.txt") $content = $content.Trim() [System.IO.File]::WriteAllText(".\email.txt", $content) $policy1 = @" `n{`n "Version": "2012-10-17", "Statement": [ { "Sid": "AmazonSesContosoTest", "Effect": "Allow", "Action": "ses:SendRawEmail", "Resource": "*", "Condition": { "StringLike": { "ses:FromAddress": [ "@ $policy2 = @" `n]`n } } } ] } "@ function Insert-Content { param ( [String]$Path ) process { $( ,$_; Get-Content $Path -ea SilentlyContinue) | Out-File $Path } } $policy1 | Insert-Content .\email.txt Add-Content -value $policy2 -path .\email.txt $content = [System.IO.File]::ReadAllText(".\email.txt") $content = $content.Trim() [System.IO.File]::WriteAllText(".\email.txt", $content) } '3' { 'You chose option #3' } 'q' { return } }