Forum Discussion
JeremyTBradshaw
Aug 28, 2023Steel Contributor
Tee-Object / -OutVariable into existing array (i.e., add into that array - is this doable?)
Hello, I have some Exchange reporting scripts where I'd like to offer the option to export CSV automatically but always send the output to standard output stream as well. I though about using Te...
sgnewman
Copper Contributor
This is how I solved this:
using namespace System.Collections.Generic;
[List[string]]$msg = [List[string]]::new();
$logFile = "c:\logs\log-$((get-date).ToString('yyyyMMdd-hhmmss')).log"
1..20 | %{
"Message $_" | Tee-Object -FilePath $logFile -Append | Select-Object @{n='void';e={[void]$Warnings.Add($_);}} | Out-Null
}
$msg
Basically I just use\abuse the select-object's expression to append to a collection...I THINK this fits what you're aiming to do?
Scott
JeremyTBradshaw
May 14, 2024Steel Contributor
Thanks for the code sample and checking with me. I ended up doing different things - https://github.com/JeremyTBradshaw/PowerShell/blob/main/Get-MailboxFolderPermissionReport.ps1
Essentially there are 2 modes to pick between - Summary or Detailed. In either mode, there's an optional CSV export which, regardless of the mode, will output CSVs for both Summary and Detailed output. So if exporting CSV, you get 2 CSVs no matter what, while screen gets just the chosen mode's output.
Definitely over engineered to the Tee's (Tee-Object pun intended), but this is sometimes more fun than other mundane things in a work day.
Essentially there are 2 modes to pick between - Summary or Detailed. In either mode, there's an optional CSV export which, regardless of the mode, will output CSVs for both Summary and Detailed output. So if exporting CSV, you get 2 CSVs no matter what, while screen gets just the chosen mode's output.
Definitely over engineered to the Tee's (Tee-Object pun intended), but this is sometimes more fun than other mundane things in a work day.