Powershell unrolling is driving me crazy.
I have the following code to retrieve email addresses from an exchange recipient. I'm using the ArrayList because it is suggested by many people when you want the ability to remove items from the array.
$aliases = New-Object System.Collections.ArrayList
$smtpAddresses = (Get-Recipient $this.DN).EmailAddresses | ?{$_.Prefix.ToString() -eq 'smtp' }
foreach ($smtpAddress in $smtpAddresses) {
$aliases.Add($smtpAddress.SmtpAddress)
}
return $aliases
The value of $aliases is correct at the end of the function (i.e. will contain x email addresses and is type ArrayList) but after returning it becomes System.Object[] and has 2x entries. There x Int32's followed by x Strings (i.e. {0, 1, bob@here, bob@there} ). Why does this happen and how to I keep my ArrayList intact? Am I wrong for using ArrayList?
Out of curiosity, with all the questions/problems resulting from PS unrolling, what is its purpose? The big benefit of powershell is that you work directly with objects instead of their textual projections, unfortunately, I never know what kind of object I'm working with - and even when I check, it doesn't seem to hold its shape for more than a few lines of code.
-- Edit The function is called as part of a PSObject
$get_aliases = { ... }
$obj | Add-Member -MemberType ScriptProperty -Name Aliases -Value $get_aliases -SecondValue $set_aliases