In order to make the functions flexible, they tell you in that book to set it up so that a parameter can be piped in, but also taken as an array. So basically, you make the advanced function to accept ValuesFromPipeline for a particular parameter, but then you also make that parameter type an array and add a ForEach-Object in your Process.
What I am running into is that if I configure my function this way, it no longer works to pipe the values in. Because then it treats them one at a time, and the ForEach-Object breaks that ArrayList into its subsequent strings for processing. Is there an easy way to prevent this?
See code example below. When I pass $OutsideArray to Get-Data as a normal parameter (Get-Data $OutsideArray), it works. When I remove the ForEach-Object in the function, it works. But when I pipe $OutsideArray in as is, it no longer works.
- function Get-Data()
- {
- [CmdletBinding()]
- param (
- [Parameter(Mandatory = $True, ValueFromPipeline = $True)]
- [System.Collections.ArrayList]$ModuleBlocks
- )
- BEGIN
- {
- }
- PROCESS
- {
- $ModuleBlocks | %{
- $test = $_
- }
- }
- END { }
- }
- $OutsideArray = New-Object System.Collections.ArrayList
- For ($i = 0; $i -lt 10; $i++)
- {
- $InsideArray = New-Object System.Collections.ArrayList
- For ($j = 0; $j -le 10; $j++)
- {
- $InsideArray.Add("$i This is a test $j") | Out-Null
- }
- $OutsideArray.Add($InsideArray) | Out-Null
- }
- $OutsideArray | Get-Data