Export TrustedSendersAndDomains to csv using powershell

Viewed 11

Hi Stackoverflow community, this is my first time posting a question.

I'm also new to powershell but I was able to put together a small script based on other scripts I found and it kind of works. My goal is to query all mailboxes and output a column with the user and one with the entry for their trustedsendersanddomains. I would like it to repeat the user when the user has multiple entries and display none in case they don't have any. It would look something like this:

csv

This is what I have. (for testing purposes I'm setting 3 actual users but the commented part works)

$Users = ("user1", "user2", "user3")
#$Users = Get-Mailbox | select -ExpandProperty Alias
$objfull = @()

Foreach ($User in $Users)
        {
         $Junk = Get-MailboxJunkEmailConfiguration $User
         $Emails = Get-MailboxJunkEmailConfiguration $User | select -ExpandProperty TrustedSendersAndDomains
         $obj = New-Object System.Object
              Foreach ($Email in $Emails)
                      {
                         $obj | Add-Member NoteProperty User $User
                         $obj | Add-Member NoteProperty Trusted_Senders $Junk.TrustedSendersAndDomains
                         $objfull += $obj
                      }     
          
         }
$objfull | Export-Csv C:\Users\Leo\export.csv -NoTypeInformation

While it will throw error for users without entries this works in repeating the user, but the trusted sender output is the whole array and it's space separated, not comma.

This other one I was testing with will write to terminal the way I want the csv to be.

$Users = ("user1", "user2", "user3")
#$Users = Get-Mailbox | select -ExpandProperty Alias
$objfull = @()

Foreach ($User in $Users)
        {
         $Junk = Get-MailboxJunkEmailConfiguration $User
         $Emails = Get-MailboxJunkEmailConfiguration $User | select -ExpandProperty TrustedSendersAndDomains
         $obj = New-Object System.Object
              Foreach ($Email in $Emails)
                      {
                         Write-Host $Email, $User
                      }     
          
         }
#$objfull | Export-Csv C:\Users\Leo\export.csv -NoTypeInformation

I appreciate the help in advance!

0 Answers
Related