PowerShell Quick Tips : Start-Job

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi and welcome to this Powershell quick tip video in this video we're going to go over the start job commandlet now we're going to be using it in Powershell 7.2 but this commandlet is definitely usable and most useful in Powershell 5.1 because it allows you to mimic a little bit of doing concurrent processing in Powershell now we've already seen concurrency in Powershell 7.2 in the Powershell intermediate Series in the redone ones in Powershell 7.2 so if you haven't seen that definitely give that a check because you could do some for each Loop in parallel processing and be able to do some pretty cool stuff but what the start job commandlet really allows you to do is to give Powershell a script block and it will actually execute it in the background and keep running through your script and allow you to still keep scripting if you just want to run part of your script and still code with the intellisense active that is what start job is great for it allows you to just start up another Powershell process in the background and execute that task and you can actually check up on that job later on to see if it's done and be able to then execute more things later on and we're going to see how we can actually use start job in Powershell 7.2 and 5.1 to greatly reduce the amount of time that some of your scripts take instead of executing things sequentially using start job we're going to be seeing that and comparing those with the measure command which we've already seen that commandlet to measure how much time something takes so let's go ahead and let's actually just create a scenario here where we're going to be doing two calls um now this could be a call to active directory it could be a call to an API any type of call can sometimes take a great amount of time depending on how big your active directory is or how much data you're pulling back sometimes that can take 10 15 seconds sometimes it could take a minute two minutes three minutes or even longer depending on the data set so we're going to see how we can actually shrink that down by just not having to execute things sequentially so let's first start off by writing our sequential script how we would typically write it without start job and then we're going to look at it with the implementation of start job so first all I'm going to want to do is actually create two variables here and these are going to be for my output paths so this is where we're going to be outputting our data from our quote-unquote API calls now in this video I'm not going to be doing an API call or a ad query we are just going to do a start sleep just to mimic the amount of time that one of those calls can take this way it just saves us on a little bit of time here so let's actually just go ahead and create a second variable for our second output so if we just start that up here and we do outputs two dot text all right and then what we're going to do now is we're going to mimic our two API calls so the first API call that we're going to do we're just going to mimic it taking 10 seconds and all we're going to do is get random and we're going to then pipe that to a out file and to our file path of output One path and then we're going to mimic a second API call which is going to take 15 seconds and we're going to do the exact same thing we're just going to a get random and output that to the output 2 path now once we actually do this sequentially we already know that the file paths already execute but let's just go ahead and let's make sure that the files are there because maybe our API call did not work it's at least going to give us some type of basic error checking here so we're just going to do an if statement and we're just going to do an if statement here and we're going to have two sets of parentheses in here separated by a dash and to make sure that both of them are equally there so we're going to do a test path here and we are going to do the path on output One path and we are going to do another test path here and we are going to do that on output 2Pac we just want to make sure that really both things are there before we do this we're going to be setting up variables to get content on our path of output One path and that's why we want to do the test we want to make sure that the paths actually exist before we try to get content from them so let's just go ahead and let's set two variables output one and output two to those output the get content of those output path variables then all we're going to do is we're just going to do a right output here and all we're going to do is we're just going to Output um the two values here we're not going to do anything with them but we're just going to Output them to say that we've gotten data so if we actually run this here right now we're going to see that this should actually work perfectly fine we should get two numbers at the end they're going to be two random numbers so we're just going to make sure that everything is working correctly so the first file is already generated and the second file should generate very soon as well hmm all right there they are and there's the two values so we can see that we have two values perfectly fine and it didn't take too too long now again these calls that we're mimicking are just 10 15 seconds they could be as long as a few minutes um in actual production we never really know how long our API calls are going to take or how long our ad queries are going to take so let's just go ahead let's measure the measure command here and let's put the expression as the whole thing of code here and we're just going to delete these two files all right and let's just go ahead and let's run this code let's see how long it actually takes the computer to actually execute this this should be around 25 seconds because the rest does not take very long the main time is really these API calls that were mimicking uh take 25 seconds because we are sequentially doing them so we're actually going to be able to see that we can actually do this much faster so let's actually just wait for the output here and there it is it is 25.07 seconds for the sequential execution of this script so let's go ahead let's copy this code and we're just going to create a new file here we're going to call it script 2 dot PS1 we're going to paste our info in here we're just going to remove the measure command for now all we want to do is just modify this script so it'll actually work with the start job so the primary parts that we want to send off to the background here is going to be these two API calls that we're doing so what we're going to do is we're going to do a start job now start job is very similar to Powershell remoting to where it cannot access anything that is outside of the script block because this will be a whole different Powershell process so what we actually need to do is we need to cut paste these in here but we also need to copy paste our output path now you can Supply arguments to these jobs via the argument list parameter for us it's a very very simple script so we don't actually need to do this and if you're just using these start jobs to offload API calls that'll just write to files it is very very easy to just do it this way so then all we need to do is we do need to actually execute actually I'm not going to do that right now we're going to see just how it works if we just do these start jobs so if we just do the start jobs here put our second script block put our second API call in the job here and copy paste our second output path all right and all we want to do now is go ahead and delete these files and try to run our code now this code will work it won't give us any error but it did not give us our output and our scripts is already done now the reason why is because we've offloaded these start jobs and you're going to see the files get generated here but we never got our output this is because if we're running these jobs in the background this Powershell doesn't know to actually wait for it once it gets to this part it just thinks all right I've uploaded these processes let's keep going so what we actually need to do is I like to usually add a little while loop right after here and make sure that the jobs are actually complete and what I also like to do is if we actually ran this now we would actually get an output but it's from the old data that we ran before so what I like to do here is I like to just start off by doing a remove item and we just remove the two output paths and then this way we never have to worry about having old data that could be detected now of course when we do the next thing that we're about to do it wouldn't really make an impact but let's go ahead and let's set these start job here we're going to set the first one to be called job one and the second one to be called job two so now that we have these two jobs all I like to do is create a little while loop which is going to be lasting Infinity now of course you can do this multiple different ways you could do a while loop while two jobs are still running then it is just keeps looping so what we can actually do is actually do that example here as well so if we do a job one dot state is equal to running actually if we just don't want it to not equal completed that would actually be a better check so anything that's not completed and then we're just going to have to add a second one because we want to make sure that both jobs are done so this would actually be an or now you might be thinking shouldn't it be an and now you could use an and in some situations but the way that we are writing this it actually needs to be an or and I'm going to go over that in just a second here so what we're actually checking here is is the job one completed no okay that's good is the job to completed no that's good so we don't want to do anything that's going to go inside the while loop and it's going to keep checking now once one of them is equal to completed so this would actually equivalate to true because um sorry it would be actually equivalate to false so job State 1 is equal to completed so it's not equal to completed so this would actually equivalate to false and then we would have r or job 2 is not completed so this would actually be true so it'd be a false or true which would still equivalate to true because I still need it to be true because both jobs aren't done yet whereas if it was an and that would instantly equivalate to a false and then would exit out of our Loop and we could actually see this in an example here I can actually set it to n and we can actually see what the output would be here so let's go ahead and let's just run this as is here with our or and we should get our output properly so if we actually just run this it deletes the two files and then it goes ahead and it runs the two jobs and then it'll wait to make sure that both jobs are completed and then it'll give us an output here so the first output is out here and the second output is out as well and there it is there is our file here so we can actually see the numbers are correct so that is all good so if I actually did this to an and here what we actually see is if we run this we're going to get a slightly different result and there it is we didn't get any output and the reason that is because the first job finished and then the and made this exit the while loop and then right away we went into our if test path but the two paths weren't there because the second job was not completed yet so that is a little caveat of the start job you do have to be really really careful on the number of jobs that you start making sure that all those jobs are completed before you actually start manipulating or needing that data but you're still saving tons and tons of time on the execution because both of these are going at the same time now we can actually prove that here by doing a measure command and then doing the expression and we're just going to start the expression here and wrap the whole thing so we know that without the start job it took roughly about 25 seconds so let's go ahead and let's see what it looks like now I switched it back to an or so it should be fine now and it should output something and we'll get the actual amount of time that it takes so there's the first output it's already done and the second one is now done so it only took about 16.19 seconds and now because it's in a measure command we don't get our actual output of course but there is the amount of time that it takes so 16.19 seconds whereas if we're running that sequentially it takes 25 seconds now I can actually even increase these to more time if we wanted to if we're talking that this takes 60 seconds and this takes 30 seconds and sequentially that would take 90 seconds of execution time for this one it would actually only take 60 seconds it's whichever one is the longest that's how long it's going to really take because they are executing in parallel in two separate processes in the background they will both be done by the time that the longest one is actually done so that is the benefit of the start job hopefully this actually helps you guys in your scripts in production now again this is super handy if you have a lot of waiting time and you're only on Powershell 5.1 again partial 7.2 and partial 7 in general you have that capability of doing concurrency a lot easier in Powershell but this start job is actually very easy to put in directly into your scripts without really making too many modifications and you could get a significant boost in your processing time now if you only have one API callic you're only making one call to active directory and then you're just processing that data this is not something that's going to save you time but if you do have multiple calls to apis to active directory this is definitely the type of script that will save you a lot of time once you have multiple calls that you're waiting on if you only have one you won't really see the benefit but if you have more than one I would definitely take a look into the start job commandlet if you guys have any comments or questions please let me know in the comment section down below if you guys have any command lists that you guys would like me to look at please let me know in the comment section down below as well also if you can just please hit that subscribe button hit that like button also hit that notification Bell to be notified when that next video comes out and I will see you guys on the next video
Info
Channel: JackedProgrammer
Views: 2,244
Rating: undefined out of 5
Keywords: powershell basics, powershell, windows powershell, programming, scripting, powershell scripting, powershell scripting tutorial, powershell tutorial, how to, powershell api responses, powershell automation, powershell beginners, powershell variables, quick tips powershell, quick tips, learn powershell, powershell for beginners, powershell commands for beginners, shortcut, start-job, powershell concurrency, powershell start-job, background job, background job in powershell
Id: tjYTA0lwcjA
Channel Id: undefined
Length: 17min 15sec (1035 seconds)
Published: Mon Apr 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.