Script creates multiple csv file instead of one

Hello,
I wrote a script that essentially pulls up all the AWS Security Groups which contains port 22 or 3389 open to the world. The script runs good, however the output is split as a different file for each violation it finds. Here is my Code block:

foreach ($sg in $sgs) {
            try {
                #Description of Public Endpoints
                $permission = ($sg.IpPermission | Where-Object { $_.FromPort -eq 3389 -or 22 }).Ipv4Ranges
                if ($permission.CidrIp -eq "0.0.0.0/0" ) {
                    Write-Host "There is a rule for 3389 or 22 open to the world in: "$sg.GroupName" in $vpc"
                    $AccountName = Get-IAMAccountAlias
                    $DateTimeStamp = (Get-Date).ToString("yyyyMMdd_HHmmss")
                    $outfilepath = "C:\temp\OutFile_$AccountName_$DateTimeStamp.csv"
                    New-Object -TypeName PSCustomObject -Property @{
                        SecurityGroupName = $sg.GroupName
                        VpcName             = (Get-EC2Vpc -VpcId $vpc | Where-Object {$_.Tags.Key -eq "Name"}).Tags.Value
                        AccountName       = $AccountName
                    } | Export-Csv $outfilepath -NoTypeInformation -Append
                    
                }

Pretty sure it is due to foreach block which goes through every security group and creates csv output. How do I ensure all the values are put in a single outfile file rather than multiples?
Sample Output:

1 Like

One way would be to assign the output of the foreach loop to a variable, without the piped export command. Then just export the variable.

$output = foreach { ... }
$output | Export-Csv $outfilepath -NoTypeInformation -Append

Hi,

This partially worked. However, I got 2 files this time, instead of 3. Improvement is, while the first file actually appends with all of $output, another file gets created which has $output[0]. Not sure why the 2nd file gets created. Any insights?
Blue tick has correct output; Red ticket is not required and shouldn’t be created.

You should share the complete code or at least the relevant part of it. Even the code you posted initially is incomplete. Without seeing your actual code it would be guessing what’s not helpful most of the time. :wink:

Apart from that: please do not post pictures if it actually does not serve an purpose. If you tell us you get two files instead of the expected one we will believe you. Or you could have posted the output of a Get-ChildItem formatted as code. That would have been enough.
Thanks in advance. :+1:t4:

I didn’t see that you name the CSV inside the loop, it was late when I responded. :joy:

You can’t have 1 file and name it based in multiple account names. Choose one or the other.

Also, what happens in the catch block and when the if statement is false? You should catch those in your output as well, to know that the $sg was processed.

@Sabrthor were you able to do it ? Can you share the latest code you have.