Forum Discussion
read elements of an array simultaneously
Hi,
You can achieve this by "join" method.
$testarray = ("a", "b", "c", "d")
$testarray -join " "
#For comma separated values
$testarray -join ","
Thank you for your reply, I will try to be clearer on what I intent to do. $testarray (with join it will have field separators) will print array but will not help me do a simultaneous operation on all elements of an array. What I wrote is just sample where if I replace print statement with anything else it should work on all elements simultaneously
- VasilMichevFeb 20, 2019MVP
What exactly are you trying to do?
- rahulrunsFeb 21, 2019Copper Contributor
Hi Vasil,
I have a function that performs some operation on each element of an array. Now these operations are independent and not required to be done simultaneously. I am trying to work out a method that how function can act on all these elements at same time- Kevin_MorganFeb 21, 2019Iron ContributorFor this case, you have to use asynchronous function call. I think you can do it by using Start-Job.
$function = {
param($a)
Write-Host $a
}
$testarray = ("a", "b", "c", "d")
$testarray | ForEach-Object {
Start-Job -ScriptBlock $function -ArgumentList $_
}#Run below command to remove jobs and get resultGet-Job | % { Receive-Job $_.Id; Remove-Job $_.Id }
You can also explore how to achieve this from below threads.https://stackoverflow.com/questions/12766174/how-to-execute-a-powershell-function-several-times-in-parallelhttps://blogs.technet.microsoft.com/uktechnet/2016/06/20/parallel-processing-with-powershell/