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.