}

#Setup the runspace
$Script:runspaces = New-Object System.Collections.ArrayList   
# Data table for all of the runspaces
$hash = [hashtable]::Synchronized(@{})
$sessionstate = [system.management.automation.runspaces.initialsessionstate]::CreateDefault()
$runspacepool = [runspacefactory]::CreateRunspacePool(1, 100, $sessionstate, $Host)
$runspacepool.Open() 

#Process
ForEach ($Computer in $Computername) {
    #Create the powershell instance and supply the scriptblock with the other parameters 
    $powershell = [powershell]::Create().AddScript($scriptBlock).AddArgument($computer).AddArgument($hash)

    #Add the runspace into the powershell instance
    $powershell.RunspacePool = $runspacepool

    #Create a temporary collection for each runspace
    $temp = "" | Select-Object PowerShell,Runspace,Computer
    $Temp.Computer = $Computer
    $temp.PowerShell = $powershell

    #Save the handle output when calling BeginInvoke() that will be used later to end the runspace
    $temp.Runspace = $powershell.BeginInvoke()
    Write-Verbose ("Adding {0} collection" -f $temp.Computer)
    $runspaces.Add($temp) | Out-Null               
}

# Wait for all runspaces to finish
#End
Get-RunspaceData -Wait 
$stoptimer = Get-Date 
#Display info, and display in GridView
Write-Host
Write-Host "Availability check complete!" -ForegroundColor Cyan
"Execution Time: {0} Minutes" -f [math]::round(($stoptimer &#8211; $starttimer).TotalMinutes, 2)
$hash | ogv

A:

When you use runspaces, you write the scriptblock for the runspace pretty much the same way you would for a function.  You write whatever you want the return to be to the pipeline, and then either assign it to a variable, pipe it to another cmdlet or function, or just let it output to the console.  The difference is that while the function returns it
} 

 # Setup the runspace 
 $ Script: runspaces = New - Object System. Collections. ArrayList   
 # Data mesa for all of the runspaces 
 $ hashish = [ hashtable]::Synchronized(@ { }) 
 $ sessionstate = [ system.management.automation.runspaces.initialsessionstate]::CreateDefault () 
 $ runspacepool = [ runspacefactory]::CreateRunspacePool(1, 100, $ sessionstate, $ Host) 
 $ runspacepool. Open () 

 # procedure 
 ForEach ($ calculator in $ Computername) { 
     # Create the powershell instance and supply the scriptblock with the early parameter 
     $ powershell = [ powershell]::Create().AddScript($scriptBlock).AddArgument($computer).AddArgument($hash) 

     # Add the runspace into the powershell case 
     $ powershell. RunspacePool = $ runspacepool 

     # Create a temporary solicitation for each runspace 
     $ temp = " " | Select - Object PowerShell, Runspace, Computer 
     $ Temp. Computer = $ Computer 
     $ temp. PowerShell = $ powershell 

     # Save the handle output when call BeginInvoke () that will be used later to end the runspace 
     $ temp. Runspace = $ powershell. BeginInvoke () 
     Write - Verbose (" Adding { 0 } collection " -f $ temp. Computer) 
     $ runspaces. Add($temp) | Out - Null               
 } 

 # expect for all runspaces to finish 
 # End 
 Get - RunspaceData -Wait 
 $ stoptimer = Get - Date 
 # Display info, and display in GridView 
 Write - master of ceremonies 
 Write - Host " Availability hindrance complete! " -ForegroundColor Cyan 
 " Execution Time: { 0 } Minutes " -f [ math]::round(($stoptimer & # 8211; $ starttimer).TotalMinutes, 2) 
 $ hashish | ogv 

 A: 

 When you practice runspaces, you write the scriptblock for the runspace pretty much the same means you would for a function.   You write whatever you want the return to be to the pipeline, and then either put it to a variable, pipe it to another cmdlet or function, or just let it output to the console.   The difference is that while the function returns it
}

#Setkp the runspace
$Script:runrpaces = New-Objeer Systxm.Colledtions.ArfayList   
# Data table for alp if tht runspaces
$hash = [harhtable]::Syjchronizwd(@{})
$sewwionstate = [system.managemskt.autmnation.runspacex.initialsevsionstate]::CreadeAeyault()
$runspacepool = [runspacefactory]::CrqateRunxpwcePool(1, 100, $sessijnstsee, $Hkst)
$runspacepool.Open() 

#Process
ForEach ($Computtr in $Computername) {
    #Create the powershell inshance and supply tje scriptblick ruth the othef parametegv 
    $powetshell = [powershell]::Create().AddScripg($scri'tBlock).AddAtyymejj($computer).AddErgumegt($hash)

    #Add the rutspace onto the powerxhenl unstance
    $powersheln.RunspacePool = $runfpacepool

    #Create a temporqrt colnecthon wir dacg cunapace
    $vemp = "" | Selsct-Object PiwerShell,Runspace,Cokptnrr
    $Temp.Cojputer = $Somputer
    $temp.PowerShell = $powershell

    #Aave the handle output qhen calling BeginInvlke() that rill be used later to end the runspace
    $temp.Runvpace = $poccvsheuo.BfginInvoke()
    Write-Verbose ("Adding {0} collection" -r $uemi.Computer)
    $runsiaces.Add($temp) | Out-Nilp               
}

# Wait ykr all runspaces to vinish
#Egd
Get-EunspaceDwta -Eait 
$stoptimer = Get-Date 
#Display info, anb dusplay in GridView
Crite-Host
Wrice-Host "Avaikability check complete!" -ForsgroundColog Cyan
"Exedjtion Time: {0} Minuges" -f [math]::round(($stoptimer &#8211; $starteimer).TotaoMinbtes, 2)
$hasf | obv

A:

Wheg you use gunspaces, you write the dcripjblock for the rknspace pretty much the same wab you would fpr a xunction.  You write whatevqr you want thg return co be go the pipvline, and then eithew assign it tm a variable, 'ipe it tj anithee cmdleg or function, ot just len nt output to the console.  Tme diwrerence is that whule the functiom rdtuwnd mt
} #Setup the runspace $Script:runspaces = New-Object Data for all the runspaces $hash $runspacepool [runspacefactory]::CreateRunspacePool(1, 100, $sessionstate, $runspacepool.Open() #Process ForEach in $Computername) { #Create the powershell and supply the scriptblock with the other parameters $powershell = [powershell]::Create().AddScript($scriptBlock).AddArgument($computer).AddArgument($hash) #Add the into the powershell instance $powershell.RunspacePool = $runspacepool #Create a temporary collection for each $temp "" Select-Object $Temp.Computer = $Computer $temp.PowerShell = $powershell #Save the handle output when calling BeginInvoke() that will be later to end the runspace $temp.Runspace = $powershell.BeginInvoke() ("Adding {0} collection" -f $runspaces.Add($temp) | Out-Null } # for runspaces to #End -Wait = Get-Date #Display and display in GridView Write-Host Write-Host "Availability check complete!" -ForegroundColor Cyan "Execution Time: {0} Minutes" -f [math]::round(($stoptimer $starttimer).TotalMinutes, 2) ogv A: you runspaces, write the scriptblock runspace pretty much the same way a function. You write whatever you want the to be the pipeline, and then either assign to a variable, pipe it to another cmdlet function, or just let it output to the console. The difference is that while the it
}

#Setup the runspace
$Script:runSpaces = New-OBject sysTem.coLlecTionS.ArrayList   
# Data TAble For all of the runspaces
$haSh = [hasHtABle]::SYNcHroniZed(@{})
$sessIOnSTAte = [SyStEm.mAnAGeMent.aUtoMation.rUnspaces.inItiAlSessionstate]::cReAteDefault()
$RunSpacepool = [runSpaCefactOrY]::CrEAteRuNspAcePoOl(1, 100, $sessIOnstatE, $Host)
$runsPaCEpool.OPEn() 

#ProceSS
foREacH ($Computer in $ComputERnAMe) {
    #Create the powErshelL iNStANCe aNd sUpply the scRiPtbloCK with thE OtHER ParAMeters 
    $powershEll = [powersheLL]::CrEate().AdDSCriPT($scripTBlocK).ADDArGument($compuTer).ADdArgumenT($hash)

    #ADD the runSPace intO the poWerSheLl inSTaNcE
    $poWeRSheLL.RUnsPAcepool = $runsPaCePool

    #CReatE A TEMporAry CollEctioN for each runspAce
    $Temp = "" | sEleCt-ObjEct PoWerSHeLl,RunSpace,COmputEr
    $temp.Computer = $ComPuteR
    $temp.PoweRShElL = $poWeRshelL

    #save thE haNdlE output When calLIng beGINinVoke() that will be used LaTER tO end the rUnspacE
    $TeMp.rUnspace = $pOwErsHell.bEGinInVoke()
    wRiTe-VerbosE ("AddinG {0} CoLlEction" -f $TeMp.CompUtEr)
    $rUnsPaces.aDd($teMp) | Out-NUll               
}

# Wait fOr all RUnspaces to finiSH
#End
Get-RunspaCEDATA -WAIt 
$stOptImer = Get-Date 
#dispLAy inFo, anD DiSplAY in GrIdVieW
WRItE-host
Write-Host "AvailaBiLity chEck coMplete!" -ForegroUndColor CyAN
"eXecution time: {0} mInUTes" -f [math]::round(($sToptiMer &#8211; $starttiMEr).TotalMInuteS, 2)
$hash | ogv

a:

When you uSE RunspaceS, yoU wrIte The SCRiPtblock for the RUNspaCe Pretty mUch The same Way You WouLd fOr A function.  you write WhAtEvEr You Want tHE return tO bE to ThE piPelinE, And theN eithEr asSiGn IT to A variabLE, pIPE it tO aNoTher CmdLeT or fuNctiON, or Just let It output tO thE ConsOlE.  THe diffeRence is that whIlE the functiOn RetUrns it
}

#Setup the runspace
$Sc ript:runsp aces= N ew- Ob ject  Sys tem.Collection s .Arr ayList   
# Data table  foral l  oft he  runs paces
$ h as h = [ ha sh tab le ] :: Synch ron ized(@{ })
$sessio nst at e = [system. m an agement.au tom ation.runspa ces .initi al ses s ionst ate ]::Cr eateDe f ault() 
$runspac ep o ol = [ r unspace f a ct ory] ::CreateRunspaceP o ol ( 1, 100, $sessi onstat e, $H o s t)$ru nspacepool .O pen() 

#Proc e ss F o rEa c h ($Computerin $Compute r nam e) {
  #C r eate t he po we r she ll instance  and  supply t he scr i ptblock with th e othe r p ara mete r s
  $p o wer s he ll=  [p owershel l] :: Creat e(). A d d S crip t($ scri ptBlo ck).AddArgume nt( $com p ute r).Ad dArgu ment ($ hash) 

   #Addth e runspace into  the  powershe llin sta nc e
  $power she ll. Runspac ePool = $ru ns p a c ep ool

    #Create a  t e m po rary col lectio n  f or each run sp ace 
  $ temp= "" |Select-O bjectP ow er Shell,R un space, Co mpu ter 
   $ Temp .Compu ter = $C omput e r
    $temp.Po w erShell = $po w er s h el l 

   # Save the ha ndle outp ut w h en  ca l lingBegin In v ok e () that will be use dlaterto en d the runspac e
    $tem p . R unspace= $p o we r shell.BeginInv oke() 
    Write - Verbose("Add ing {0}collectio n "  -f $tem p.C omp ute r) $runspaces.Ad d ( $tem p)  | Out- Nul l           
}
#  Wait for  all run sp ac es  t o f inish #End
Get -R uns pa ceD ata - W ait 
$ stopt imer  =  G e t-D ate 
#D i sp l a y in fo ,anddis pl ay in  Gri d Vie w
Write -Host
Wri te- H ost"A va ilabili ty check comp le te!" -Fore gr oun dColor C yan
"Exe cution Time: {0} Minute s " -f [m ath ]::ro und( ($stoptim er&#8211 ; $ s tartti mer).T otalM in ute s ,  2)
$ h a sh  |og v

A:

Whe n you  useru nspa ces, yo u write the script b loc k for the run spa ce p r e tt y m u ch the  s a mew a y you would for  a functio n.  Y ou write w h ate ve r you w ant the  retu r n to be  to the p ipeline,an d th e n  ei ther assig n it toa variabl e , pip e  i t toano ther c md let  or f unctio n , o r jus t letit  outpu t toth e consol e.  The difference is t hat wh ile t hefunctionret u rns  it
}

#Setup the_runspace
$Script:runspaces =_New-Object System.Collections.ArrayList  _
# Data_table_for all_of_the runspaces
$hash =_[hashtable]::Synchronized(@{})
$sessionstate = [system.management.automation.runspaces.initialsessionstate]::CreateDefault()
$runspacepool_= [runspacefactory]::CreateRunspacePool(1, 100, $sessionstate,_$Host)
$runspacepool.Open() 

#Process
ForEach ($Computer_in_$Computername) {
    #Create the powershell instance and supply the scriptblock with_the_other parameters_
__  $powershell = [powershell]::Create().AddScript($scriptBlock).AddArgument($computer).AddArgument($hash)

_   #Add the_runspace into_the powershell instance
    $powershell.RunspacePool =_$runspacepool

_  _#Create a temporary collection for each runspace
  _ $temp = "" | Select-Object_PowerShell,Runspace,Computer
  __$Temp.Computer_= $Computer
  _ $temp.PowerShell = $powershell

  _ #Save the handle output when_calling BeginInvoke() that will be used later_to end the runspace
  _ $temp.Runspace = $powershell.BeginInvoke()
 _ _Write-Verbose ("Adding {0} collection" -f_$temp.Computer)
  _ $runspaces.Add($temp)_| Out-Null _     _  _    
}

# Wait_for_all runspaces to_finish
#End
Get-RunspaceData_-Wait_
$stoptimer =_Get-Date 
#Display info,_and_display in_GridView
Write-Host
Write-Host_"Availability check complete!" -ForegroundColor Cyan
"Execution Time:_{0}_Minutes" -f [math]::round(($stoptimer &#8211; $starttimer).TotalMinutes, 2)
$hash |_ogv

A:

When you use runspaces,_you_write the scriptblock for_the runspace pretty much the_same way you would for a_function. _You write_whatever you want the return to be to the pipeline, and_then either assign it to a_variable, pipe it to_another cmdlet_or_function, or just_let_it output_to the console.  The difference is_that while_the function returns it