- Export and import to and from Excel with the PowerShell module ImportExcel - Thu, Apr 21 2022
- Getting started with the PSReadLine module for PowerShell - Thu, Feb 24 2022
- SecretsManagement module for PowerShell: Save passwords in PowerShell - Tue, Dec 22 2020
So, what can you do to help in those situations? Run PowerShell code as jobs.
There isn’t much you can do to speed up a slow computer. You can optimize your code to make it run faster once it gets there, but at some point, the end node’s ability to process commands will limit you. But that doesn’t mean you need to wait on your code to complete.
One easy option is to submit your Invoke-Command as a job. That will tell the computer to run it in the background and give you back the console so you can work on other tasks. Following up from the example in the previous article in this series, those sessions are still available to use.
Here’s an example of code that I would rather not wait to finish. I will ping Google from all nine servers.
Invoke-Command -Session $sessions -ScriptBlock {ping google.com}
I’ve chopped off the bulk of the output because it just runs on for some time. This isn’t elegant code, but it illustrates a point. If I had to run this code, I wouldn’t want to wait for responses from many computers. Instead, I can run the same code with one extra parameter and be free to move on to other tasks. This tells PowerShell to run the same command but run it in the background.
Invoke-Command -Session $sessions -ScriptBlock {ping google.com} -AsJob
Now, I get a job summary instead of output. I run the Get-Job cmdlet to see the status of the current job. Notice it says RemoteJob.
get-job
I see that RemoteJob finished. To get the results, I run the cmdlet Receive-Job to transfer the job results from the queue to my console. I can add the -keep parameter to save the results for future review. If I don’t add -Keep, the results are deleted from the job queue and only exist in my console session. They cannot be recalled again.
receive-job 23 -keep
The results show that it displays the same output as when we ran the command in the console prompt earlier. Except this time, I didn’t need to wait for all the pings to finish; the results display instantly.
Subscribe to 4sysops newsletter!
Summary
In this series of three parts, we have reviewed how we can deal with situations that can cause problems when connecting to remote computers using Invoke-Command. These three tips only scratch the surface of what you can do to optimize code to help you get things done as efficiently as possible. Look for more tips on remoting and executing code in bulk here at 4sysops. I would love to know your thoughts. Please use the comments section below to reach out with your questions and ideas.
Interesting series here. In the end, if we speak about the speed of execution, it should not matter if you run in at job or interactively, the time needed is still the same. The only difference is that you can do some other things while waiting 🙂