Powershell add a new line in output CSV file if multiple entries found

Viewed 31

I am doing an export of mailboxes from Office 365. Some mailboxes the have the same primarysmtpaddress are active and also Inactive. I enter code here have the following code:

$MBX = import-csv "c:\retention\LegalHold\test.csv"

$MBX | Add-Member -MemberType NoteProperty -Name Retention -Value NoPolicy
$MBX | Add-Member -MemberType NoteProperty -Name NoMailbox -Value $False
$MBX | Add-Member -MemberType NoteProperty -Name IsInactiveMailbox -Value $null
$MBX | Add-Member -MemberType NoteProperty -Name PrimarySmtpAddress -Value $null

$count=0
$fullcount = ($mbx | measure-object).count

foreach ($M in $MBX){ 
$policy = $null

$count++
$search = $m.custodian.tostring()

Write-progress -activity "Checking Mailbox $count out of $fullcount --- $search " -percentcomplete (($count / $fullcount)*100) -status "Processing"

    Try{
        $ErrorActionPreference = 'Stop'
        $Policy = Invoke-Command -scriptblock {Get-EXOMailbox $m.custodian -IncludeInactiveMailbox -properties primarysmtpaddress,inplaceholds,isinactivemailbox,LitigationHoldEnabled,LitigationHoldDuration,Office}
        #$Policy = Invoke-Command -scriptblock {Get-Mailbox $m.name -IncludeInactiveMailbox}
        #$m.MBXFound = $True
        $m.IsInactiveMailbox = $Policy.IsInactiveMailbox -join ','
        $m.PrimarySmtpAddress = $Policy.PrimarySmtpAddress -join ','

    }Catch{$m.NoMailbox = "$True"}
            
}

$MBX | export-csv C:\retention\legalhold\test_check.csv -NoTypeInformation

Instead of outputting to the CSV like this:

enter image description here

I'd like to have the mailboxes with an active and inactive mailbox on separate lines like this:

enter image description here

1 Answers

You would need to loop through the results of $Policy instead of just using -join.

$MBX = import-csv "c:\retention\LegalHold\test.csv"

$MBX | Add-Member -MemberType NoteProperty -Name Retention -Value NoPolicy
$MBX | Add-Member -MemberType NoteProperty -Name NoMailbox -Value $False
$MBX | Add-Member -MemberType NoteProperty -Name IsInactiveMailbox -Value $null
$MBX | Add-Member -MemberType NoteProperty -Name PrimarySmtpAddress -Value $null

$count=0
$fullcount = ($mbx | measure-object).count

$Output = foreach ($M in $MBX){ 
$policy = $null

$count++
$search = $m.custodian.tostring()

Write-progress -activity "Checking Mailbox $count out of $fullcount --- $search " -percentcomplete (($count / $fullcount)*100) -status "Processing"

    Try{
        $ErrorActionPreference = 'Stop'
        $Policy = Invoke-Command -scriptblock {Get-EXOMailbox $m.custodian -IncludeInactiveMailbox -properties primarysmtpaddress,inplaceholds,isinactivemailbox,LitigationHoldEnabled,LitigationHoldDuration,Office}
        #$Policy = Invoke-Command -scriptblock {Get-Mailbox $m.name -IncludeInactiveMailbox}
        #$m.MBXFound = $True
        $Policy | ForEach-Object {
            $m.IsInactiveMailbox = $_.IsInactiveMailbox
            $m.PrimarySmtpAddress = $_.PrimarySmtpAddress
            $m
        }
    }Catch{$m.NoMailbox = "$True";$m}            
}

$Output | export-csv C:\retention\legalhold\test_check.csv -NoTypeInformation
Related