Prof. Powershell

More Jobs

The -AsJob parameter offers an alternative approach to remoting, one that's more interactive.

Last time I talked about running PowerShell commands as background jobs on your computer. The Start-Job cmdlet doesn't have a parameter that will create the job on a remote computer. Instead, you have two possibilities. First, you can create the remote job using Invoke-Command and retrieve the results locally. Or you can use an interactive remote session and use cmdlets that contain the new -AsJob parameter. When using this technique, job results stay on the remote computer until you retrieve them:

PS Y:\> invoke-command -ScriptBlock {dir $env:temp -recurse} -ComputerName Win2k801 -asJob

Id  Name  State    HasMoreData  Location  Command
--  ----  -----    -----------  --------  -------
3   Job3  Running  True         win2k801  dir $env:temp -recurse

In this example I've created a background job using a remote computer session to Win2K801. Later, I can check the job status:

PS Y:\> get-job -Name job3

Id  Name  State      HasMoreData  Location  Command
--  ----  -----      -----------  --------  -------
3   Job3  Completed  True         win2k801  dir $env:temp -recurse

When I'm ready, I'll use Receive-Job to retrieve the results which I'll save to a variable. Using –keep retains the job results:

PS Y:\> $data=Receive-Job -Name Job3 -keep
PS Y:\> $data | measure-object Length -sum

Count    : 4
Average  :
Sum      : 524301
Maximum  :
Minimum  :
Property : Length

The other approach is to open a shared interactive session as I've demonstrated in earlier lessons:

PS Y:\> Enter-PSSession -ComputerName Win2k801
[win2k801]: PS C:\Users\administrator.MYCOMPANY\Documents>

I can use a cmdlet that supports the -AsJob parameter like this:

[win2k801]: PS C:\Users\administrator.MYCOMPANY\Documents> get-wmiobject CIM_DataFile -filter "Extension='txt'" -asjob

Id  Name   State    HasMoreData  Location   Command
--  ----   -----    -----------  --------   -------
1   Job1 m Running  False        localhost  Get-WMIObject

To keep things straight, remember that I now have a background job on Win2k802 running my WMI query. This is important because when I retrieve the results, they are not transferred to my machine. They remain:

[win2k801]: PS C:\Users\administrator.MYCOMPANY\Documents> $files=Receive-job -name job1 -keep
[win2k801]: PS C:\Users\administrator.MYCOMPANY\Documents> $files.count
698

Even if I run a command like this:

[win2k801]: PS C:\Users\administrator.MYCOMPANY\Documents> $files | out-file c:\textfiles.log

The log file is created on the remote computer's C:\ drive, not mine.

You can also use Invoke-Command in an interactive remote session which supports -AsJob for even more scripting and automation power.

Note: This information is based on pre-release software and is subject to change.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular

Upcoming Training Events

0 AM
Live! 360 Orlando
November 17-22, 2024
TechMentor @ Microsoft HQ
August 11-15, 2025