powershellforeach-object

Output from Powershell function shows only after script finishes


after many hours of trying numerous approaches I finally gave up. My problem is trivial but it gives me really hard time.

I have this function:

function SearchSharedMailboxes {
param (
    [parameter(Mandatory=$true)] $objectList,
    [parameter(Mandatory=$true)] [string]$objectName
)
    
    $matchingList = [System.Collections.ArrayList]::new()

    if ($objectList.Count -le 0) {
        
    $decision = Read-Host "No match found, search again Y/N?"

        if ($decision -eq "Y") {

            GetSharedMailboxes

        } else {

            Exit-PSSession

        }

    } else {

        Write-Verbose "Matches found:"
        
        $i=0
        $objectList | Foreach-Object {

             [PSCustomObject]@{
                ID = $i
                UserPrincipal = $_.UserPrincipalName
                RecipientTypeDetails = $_.RecipientTypeDetails
                PrimarySMTPAddress = $_.PrimarySMTPAddress
                
            }
            $i++

        }

    }
    
}

I expect to see output in the console like this:

ID UserPrincipal                       RecipientTypeDetails PrimarySMTPAddress
-- -------------                       -------------------- ------------------
 0 Name1                               SharedMailbox        addres1@domain.com
 1 Name2                               SharedMailbox        addres2@domain.com
 2 Name3                               SharedMailbox        addres3@domain.com

The thing is that the output shows fine but if I call another function before the final bracket the output shows no more. So to demonstrate:

     $i=0
        $objectList | Foreach-Object {

             [PSCustomObject]@{
                ID = $i
                UserPrincipal = $_.UserPrincipalName
                RecipientTypeDetails = $_.RecipientTypeDetails
                PrimarySMTPAddress = $_.PrimarySMTPAddress
                
            }
            $i++

        }

    }

    ObjectAssigning
    
}

With "ObjectAssigning" function call like above, the output shows no more UNTIL the script ends. So when the "ObjectAssigning" method finishes and therefore whole script ends, it produces output like before. However, I need the output to show up while the script is running, not after it finishes. I tried assigning my custom PSObject to variable and calling the variable, I also tried something like

$variableWithCustomPSObjAssigned | Select-Object -Property *

but it gives exactly the same results - output shows after the script finishes. I also tried to let "SearchSharedMailboxes" function to finish and then calling "ObjectAssigning" from elsewhere but with the same results. Trying to use older approach like this also did not resolve the problem:

   $objectList | Foreach-Object {

            $item = New-Object -TypeName PSObject
            $item | Add-Member -MemberType NoteProperty -Name ID -Value $i
            $item | Add-Member -MemberType NoteProperty -Name UserPrincipal -Value $_.UserPrincipalName
            $item | Add-Member -MemberType NoteProperty -Name RecipientTypeDetails -Value $_.RecipientTypeDetails
            $item | Add-Member -MemberType NoteProperty -Name PrimarySMTPAddress -Value $_.PrimarySMTPAddress
            $i++
        }

I would appreciate any help regarding this matter.


Solution

  • It sounds like the problem is yet another variation of the infamous 300-millisecond delay that (possibly implicitly applied, as in your case) Format-Table calls employ in order to determine suitable column widths:
    A long-running, blocking command that is invoked before this delay has elapsed (ObjectAssigning, in your case) can indefinitely delay the tabular output.

    Workarounds:

    If it is acceptable to have your function produce to-display output only - i.e. to no longer output data - you can force synchronous to-display output by piping to Out-Host (you may also pipe to Format-Table, but its use is implied by your output objects, and - unlike Out-Host - it would still produce success-stream output, albeit in useless form: what Format-* cmdlets output to the pipeline are formatting instructions):[1]

    # ...
    
            $objectList | Foreach-Object {
    
                 [PSCustomObject]@{
                    ID = $i
                    UserPrincipal = $_.UserPrincipalName
                    RecipientTypeDetails = $_.RecipientTypeDetails
                    PrimarySMTPAddress = $_.PrimarySMTPAddress
                }
                $i++
    
            } | Out-Host
    
    # ...
    

    If you do need data output, the solutions are nontrivial, unfortunately:


    [1] You can, however, combine a Format-* call with Out-Host (e.g. ... | Format-List | Out-Host) if you want to use non-default formatting.