Sometimes, running remote code with the PowerShell cmdlet Invoke-Command can take a long time to run—and not for any reason having to do with your code itself. Connecting to older computers means that long bits of code take longer to run. Computers on the other side of slow WAN links will always be slower in responding than something close and well connected.

Mike Kanakos

Mike is a Windows IT pro located in the Research Triangle Park area of North Carolina with 13+ years of experience as an admin and 20 years in the field. He specializes in Active Directory, Azure AD, Group Policy, and automation via PowerShell. You can follow Mike's blog at networkadm.in or on Twitter at @MikeKanakos.

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.

Pinging multiple servers

Pinging multiple servers

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 as job

Invoke Command as job

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.

Job summary

Job summary

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.

Receiving jobs

Receiving jobs

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.

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.

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

2+

Users who have LIKED this post:

  • avatar
Share
Articles in series

Invoke-Command: Common challenges

1 Comment
  1. Leos Marek 3 days ago

    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 🙂

    0

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2020

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account