Process Data (Part 2)

by Feb 3, 2016

In part 1 we showed how a PowerShell function can receive input both from a parameter and via the pipeline, and process it in real-time. This is the most efficient way as it minimizes memory consumption.

Sometimes, however, it is necessary to first collect all data, and once all data is received, process all of it in one step. Here is a function that collects all received data and starts to process it only when all data has arrived:

#requires -Version 2
function Collect-Data
{
  param
  (
    [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
    [Object]
    [AllowEmptyString()] 
    $Object
  )

  begin
  {
    $bucket = New-Object System.Collections.ArrayList
  }

  process
  {
    $null = $bucket.Add($Object)
  }
  end
  {
    $count = $bucket.Count
    Write-Host "Received $count objects." -ForegroundColor Yellow
    $bucket | Out-String
  }
}

Note how Collect-Data can receive information both via parameter and pipeline:

  
PS C:\>  Collect-Data -Object 1,2,3
Received 3 objects. 
1
2
3
 
 
PS C:\> 1..3 |  Collect-Data
Received 3 objects. 
1
2
3 
 

There are two things worth mentioning: never use a plain array to collect information. Rather, use an ArrayList object because it adds new elements much faster. And try to avoid the $input automatic variable that sometimes is used for a similar purpose. $input works with pipeline input only and ignores values submitted to parameters.

 

Throughout this month, we'd like to point you to three awesome community-driven global PowerShell events taking place this year:

Europe: April 20-22: 3-day PowerShell Conference EU in Hannover, Germany, with more than 30+ speakers including Jeffrey Snover and Bruce Payette, and 60+ sessions: www.psconf.eu.

Asia: October 21-22: 2-day PowerShell Conference Asia in Singapore. Watch latest announcements at www.psconf.asia

North America: April 4-6: 3-day PowerShell and DevOps Global Summit in Bellevue, WA, USA with 20+ speakers including many PowerShell Team members: https://eventloom.com/event/home/PSNA16

All events have limited seats available so you may want to register early.

Twitter This Tip! ReTweet this Tip!