***NOTE Rev 4 10/21/2011***

Added a progress bar

***NOTE Rev 3 11/30/2012***

added write-error to show errors with cmdlet calls

added Measure param to give timing info

added ability to use alias

***NOTE Rev2***

 Script has been updated to handle a single item and also closes out the runspaces differently.

 

This function allows you to run any cmdlet/function/scriptblock asynchronously.

This has been tested in V2 and V3.

You just provide the data set (-set) such as a list of servers of configuration settings etc.

The param that the set belongs to (-setparam) such as ComputerName.

You have the ability to provide any other params with a hash via -params (see examples.)

The number of threads or jobs (concurrent instances) can be controlled via ThreadCount.

 

 

PowerShell
Edit|Remove
$sb = [scriptblock] {param($systemgwmi win32_operatingsystem -ComputerName $system | select csname,caption} 
  
$servers = Get-Content servers.txt 
 
$rtn = Invoke-Async -Set $server -SetParam system  -ScriptBlock $sb
 

This will pull the computer name and installed OS from a series of systems.

 

 

 

PowerShell
Edit|Remove
$servers = Get-Content servers.txt 
 
$rtn = Invoke-Async -Set $servers -SetParam computername -Params @{count=1} -Cmdlet Test-Connection -ThreadCount 50
 

This will ping a series of systems 1 times.

 

 

NOTE:

I have seen, especially with ping, that this consumes a lot of memory. This is due to the way Test-Connection disposes of itself. If this presents a problem run [GC]::Collect() which will force the garbage collector to run and free that memory. If that is not run, the memory will free in time. It is not advised that you manually run the collector.