Multithreading in Powershell | Run jobs in parallel

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello engineers in this video we will examine how we can use multi-threads on partial in order to make our scripts execution time quicker if you enjoy my content make sure to subscribe on my channel parcel concurrently runs commands and scripts through jobs which is the equivalent to threads there are multiple ways to achieve parallel execution partial and we will examine two of them the first method would be to use for each object along with a parallel parameter which you can see as a comment right here this method will work only in the newest version of partial which is version 7 and it is not compatible with parcel 5. the second method would be the start job which you can see in the second comment section and can be used with the older version of partial for our example we will use the test connection which brings a given host in the provided script I also use the stopwatch class in order to get the total elapsed time for the X execution of the script at the beginning of the script I set an object which is called computers and inside this object I have included three hosts those hosts will be used in a loop which I do with for it and the test Net Connection will be performed for each of those hosts before this operation I use the stopwatch class in order to count the time for this operation the last step would be to Echo the elapsed time that this operation needed in order to run we will now switch to our parcel command line and we will run this script in a Serial way when we execute our script we will get in the output the pin comats that are performed for each of the codes that we have provided in the computer object as you can see the total elapsed time for this serial execution of my script would be around 10 seconds we will now go and execute the script in a parallel way to verify how much time the new version will need in order to run our script the first method that I will try would be the for each object which can be used with parcel 7. this method also Loops in the computer's object that we have defined and uses the parallel parameter for this Loop that we perform and also we specify that this parallel execution should be executed as Jobs then we use in a pipe they receive job and wait parameters and then we print the total elapsed time for this particular run we will try now to run this particular script and validate how much did this run needed by executing the script we can see that the pin commands are executed in parallel in this particular run and the total elapsed time would be around 3.5 seconds as you can see the pin command can be X executed in parallel in multiple threads that have been created from partial and right now we have a quicker execution time for this particular job we will now switch to the parcel 5 version which uses the start job command as you can see I have defined three different jobs and every job will bring one unique host like before we will first create a stopwatch in order to count the time this execution needed then we will use the get job and way job in order to wait for our job to finish and then we will use the receive job in order to get the output for these particular jobs at the end we will print the total elapsed time for this script execution by switching back to our terminal we can go and execute this new version of our script as you can see the time that this execution needed is around the same that we had with parcel 7 again again our script used three particular pings on each of those hosts that we have provided as an input and finally we got the same results as we did in Powershell 7. all the pings for both execution succeeded and the end result would be that we reduced our execution time from 10 seconds to about 4 seconds thanks for watching I hope you enjoyed this video and let me know in the comments below if you have ever used multi-threads on parcel Scripts
Info
Channel: geralexgr
Views: 1,720
Rating: undefined out of 5
Keywords:
Id: DW4TfEeSEiw
Channel Id: undefined
Length: 4min 30sec (270 seconds)
Published: Tue Jul 04 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.