You cannot use the output of the foreach statement in the pipeline because foreach is not a cmdlet. Try to wrap the foreach statement in &{}. Like:
&{ foreach { ... } } | Export-CSV ...
You cannot use the output of the foreach statement in the pipeline because foreach is not a cmdlet. Try to wrap the foreach statement in &{}. Like:
&{ foreach { ... } } | Export-CSV ...