Running PowerShell code as background tasks with PowerShell jobs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome back to another episode of Powershell and today we're going to go take a look at some Powershell code for running background tasks we're going to take a look at the job system within Powershell and how to store jobs for background running code retrieve the information and the output from those jobs we are also then going to go and have a look at Powershell scheduled tasks and how to actually take our code and run it at a specific time of day and even repeat that operation to be able to dump that to text files that we can then retrieve from other computers as well so please hang around and if you enjoy the content remember # like And subscribe and let's get straight into creating background tasks with Powershell [Music] jobs all right then guys let's get straight into this with the jobs system now say if you have a commandlet like for example get- Service uh let's do- name and let's do D bits okay if we run that command we will get an output to the Shell now what about if we run a much longer command like for example get- child item and let's do this C column back recurse that is now going to list every single file and folder inside this computer um that's going to take quite a while to complete and it's going to lock up my shell until it is so I'm going to cancel that out but what you might want to do with these longer running jobs is maybe run them in the background now to do that I'm going to just take this out for a second we're going to reuse this get- service and we're going to use a special command called start- job okay this is going to send this to a background process let's clear off this shell down here to keep things nice and to do this we just need to pop this inside a script block script block on start job just like that now if I run this what's actually going to happen is I'm going to get a job and you can see down here I have a job ID number and a job name now I didn't give the name of this job I could do another one and give it an actual name if I wanted to so let's do start job get service name spooler and go and give that a name of get spooler if I really wanted to put that into some double quotes run that and now I have another job if I run get Das job inside here you'll see I have two jobs running job one and get spooler with two different unique ID numbers ID number one and ID number three right so I've got no output for this yet you'll notice that these jobs have completed why have they completed well quite simply because it does not take too long to run these commands and get the results now I need some form of output now to do this what there is is there's another command called receive Das job okay and it would help if I could spell receive there we go not receive DTC diagnostic receive job here we go and now we can give a job name or we can actually give an ID for the job that we actually want to receive so if I do job ID one which was this first background job here uh this job which is related to get service bits and I go and run just this single line of code you'll see the output has been sent to the Shell now the problem with the received job is that that output was sitting in memory waiting to come across uh to go somewhere and it just dumped it to the Shell but it didn't copy it to the Shell it basically cut and pasted it to the Shell so that means if I try to receive this job again the output there's nothing there if I didn't send it somewhere to be stored this thing's gone um you can kind of get around that so if I do job three um which was the second job down here the get spooler job and I use this special little parameter just says keep it means it is never going to be removed from memory and I can keep retrieving that over and over again now the best way that you should actually do this is to receive your jobs and actually send them to some form of variable so you do something like spooler uh spooler output uh get rid of that receive there oh sorry get that receive put a space in there uh equals receive job ID3 and go and run that now when that actually runs and receives the information it's got somewhere to go it's being sent to this variable I can now retrieve this variable and do stuff and things to it so that's a very basic job system if you're running some longer jobs though things get a bit weirder so if I run start- job and I'll do that script block again but I will do this with get Dash uh child item and we will list out the entire hard drive again all right this is not going to be fun for the computer uh let's just give it a name and we'll give it a name of long because it's going to to be a long running job here okay so that's going out it's got id5 that's currently running now if I run get job uh inside the shell you'll notice that this thing is actually running and it's going to take quite a long time to complete but it doesn't matter I can get on with other stuff this is great in your scripts because you can take long running components of your scripts send them to a background job and when they're done you can come and collect those things later on when you can see the state of it is actually set to completed but if I've got something like this I could still retrieve stuff but there is a bit of an issue here if I do a receive item well sorry a receive job again tabing to DTC Diagnostics there uh with a job ID of five if this is a long running job we have to be very careful with this because if I receive job id5 and the job is still running what we have is this situation say for example your output is actually going to be numbers 1 2 3 four and your script is still running and it's just generating numbers right if I decide to receive this job now I'm going to receive these four numbers outputed to maybe some sort of variable something I want to store it in that's fine but this job is still running it's still going so these first four numbers are now deleted from memory after I've received them the job's still going and it's still now writing 5 6 7 8 9 and so on and so forth I then have to do another receive job to another variable um in my environment and then maybe afterwards combine those two variables together in reality the better way of doing this is to make sure that when you're doing this receive job for a long running job and you basically just want to kind of check the status the kind of output you're getting you got to remember that keep switch otherwise things will go very wrong and you'll have missing data everywhere so I can still see there's the out put I can contrl c that I could also uh pipe that out to whoops pipe that out to a file should I want to so I could do something like uh let's just go to CD desktop so I've got permission to do it so we could do something like back SL output.txt and I could go and send that output to output.txt uh admittedly oh sorry access is denied at the moment so it's because I'm not running as administrator on here uh I'd have to send that to a different location but either way you could send that as an output to a text file and go and stream it out if you really wanted to as that's working so that's the basic job system but the problem here is if I go and close this environment I go go and close this Con uh context at the moment and load up another Powershell ISC over here I go and run get- job again and I have no jobs because it's just related to that single Powershell session what we can do though is a different type of job um if we really want to if we want to have continuously running jobs we can do something else we can do a scheduled job scheduled or scheduled scheduled scheduled so tomato tomato I don't know um I think is that that's that's English UK English us scheduled scheduled like aluminum or aluminium don't care if you invented it it's still aluminium and color has a u in it okay um and t t does not go in the M for Christ tea microwave uhuh no we get a kettle we boil the kettle and we met I'll do a thing on tea later on it's fine so what we want to do here is we want to set up a trigger okay and there's a couple of components to this it's not just one command we have to set up a new job trigger to tell when we want this job to actually occur so we've got things like days of week random delay if you're feeling lucky uh weeks interval days interval and so on so forth but if I just choose daily here we can do daily at 3:00 a.m. okay if we want to I need to give that some caps and then what I want to do is I want to maybe set some options inside here so the options for this might be new- scheduled job option and there are a number of scheduled job options we can add like for example require Network wait to run continue with going on battery all sorts of stuff but I want this one start if on battery because scheduled tasks might be if you're running on laptop mode or battery saver mode they might not actually run um unless you've got that uh schedule job option turned on there now what we're going to do is we're going to do a little bit of a longer code we're actually going to run the job that we want to run so we're going to do register Dash scheduled oops register schedule job we're going to name this uh process job inside here and we're going to pop in a script block and within this script block we're going to run get- process that's all that's all it's going to run get- process that's fine now this schedule job I could inside here not just put code code I could go and put an existing Powershell script in here if I really wanted to that would be fine and then I could do- trigger and then the trigger is going to be dollar trigger it's the one that we set earlier in this code and the schedule job option is going to be dollar options okay that's perfectly fine if you're ever in poh ISC and you find yourself writing out these long commands that're kind of going off to the side uh there's a little trick you can do if you take a command and you split it down to a new line we've got a bit of a problem uh notice the color of that trigger there like a deep blue and then we put it down here and it goes to sort of like a light blue that's going to break the commandlet because it's trying to treat that on a new line as if it is another commandlet that's not going to work but if we come in here right at the end and we pop a back tick right here and then we go and try that again so notice I've got a back tick on the end drop that down oops sorry drop that down down there and you'll notice it's going to continue on perfectly fine it's still a deep blue it's still like a uh parameter so that's where you can kind of neaten things up now if I run that done I have a job ready to go out in reality what you normally do with a schedule job is within here you would go and output that and output that to some sort of output text file uh somewhere inside your environment uh so that you could go and collect this data later on if you wanted to now this schedule job is going to run at 3:00 a.m. that's fine it's going to go and run get process at 3:00 a.m. admittedly nothing's going to happen if it runs get process because it's just running get process but if I send it to an output file I'll be able to collect it later on remember if you are sending stuff to an output file like this it will overwrite it every single time so at 3:00 a.m. it will completely wipe that text file out and write it with new data from the next day and the next day and the next day unless you put the parameter on of append uh in fact to do that I'm going to need to do this in long form you need to this as out file and then give it a file path of where you wanted to go so out.txt and then make sure you've got this switch on the end append and if you got aend it means that you'll get your data inside that text file and it will keep adding to that text file as you go down on a list okay now that's all cool we registered that schedule job if I run it again um it's going to it's going to error out on me because there's already a job with that name let's do process job two on there uh just to run another scheduled job for the heck of it now if I do get- scheduled job you'll be able to see both of them here but you can also see them somewhere else if you go to task schedul old school task schedular there is a section down here underneath the task schedular Library we've got windows and then we've got down here we've got Powershell and schedule jobs notice there's nothing in here at the moment because it was already open but if I go and refresh this window there's my two jobs processes that I have just created so if I go and have a look at this process job click on the properties of it you'll notice there it is running that's fine there's the trigger that I specified I could go and edit this I could go and adjust it it doesn't matter uh I could also come in here and see the actions and you can see it's actually running Powershell xxe and it's going to to run my code so that's basically it really guys that's schedule jobs in Powershell and that's also the job system in Powershell quite quick and straightforward to the point I hope you enjoyed this content and next time um I don't quite know what I'm going to go into next time you'll have to stick around and find out hope you enjoyed this content guys hash like And subscribe and I'll see you next time and you know routine # like And subscribe and I hope you enjoyed this video and we'll join me next time goodbye
Info
Channel: Mike in the Cloud
Views: 1,431
Rating: undefined out of 5
Keywords: PowerShell, Background Jobs, Scheduled Tasks, Automation, Scripting, Task Scheduling, PowerShell Scripting, System Management, Asynchronous Processing, PowerShell Tutorial, Task Automation, PowerShell Best Practices
Id: qmx3X7p0BYA
Channel Id: undefined
Length: 15min 29sec (929 seconds)
Published: Fri Feb 09 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.