Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, April 3, 2012 12:25 PM
I have a PowerShell script that runs under a service account. This script creates a Session and uses the Invoke-Command for Remoting (to execute a script on the target machine). I add the parameter -AsJob and it works under my account when I am physically logged on the server in interactive mode. When I have a process run the script, the Job on the remote machine does not execute. I remove the -AsJob parameter and it works under the process account.
The process is a Team Foundation Server build process running under a domain account. I added the parameters -noexit and -noninteractive and then the process worked but the build process did not end; the -noexit did its job. I removed the -noexit but kept the parameters -noninteractive and -asjob and the build process worked but the remote job did not execute as prior to adding -noninteractive. I would like to keep the -AsJob parameter to speed along the build process. There is a queue of build processes and the -AsJob parameter will allow me to queue jobs on the remote machine (that is my understanding for the purpose of the -asjob parameter).
Is there way to lauch a remote process to execute on a remote machine and then exit PowerShell on the invoking machine?
Thank you for your assistance.
All replies (1)
Wednesday, April 4, 2012 3:04 PM âś…Answered
Asjob will not queue jobs. Asjob just spawns a new host so that the process can run in the background. It's a form of parallel computing. Generally it has a bit of overhead on a local system (mainly when not using a remoting command, but that's a digression). If you use -asjob on a single session multiple times, it will fail. Take the following as an example:
10:54:05 PS D:\dropbox\scripts> $session = New-PSSession win7
10:54:19 PS D:\dropbox\scripts> $scriptblock2 = {(21..30)|% {$_;sleep 1}}
10:54:19 PS D:\dropbox\scripts> $scriptblock1 = {(0..20)|% {$_;sleep 1}}
10:54:19 PS D:\dropbox\scripts> Invoke-Command -Session $session -AsJob -ScriptBlock $scriptblock1
Id Name State HasMoreData Location Command
--
2 Job2 Running True win7 (0..20)|% {sleep 1}
10:54:20 PS D:\dropbox\scripts> Invoke-Command -Session $session -AsJob -ScriptBlock $scriptblock2
Id Name State HasMoreData Location Command
--
4 Job4 Failed False win7 (21..30)|% {$_;sleep 1}
10:54:20 PS D:\dropbox\scripts>
In order to queue them, you would need to do something like:
invoke-command -session $session -asjob -scriptblock $session |wait-job
I think that defeats the purpose of using asjob for you. If you need parallel execution of jobs, but you want everything to run in order on the remote computer (like in a queue), you would create a job on the local system that runs a series of invoke-commands:
Start-Job -ScriptBlock {
$session = New-PSSession win7
$scriptblock2 = {(3..5)|% {$_;sleep 1}}
$scriptblock1 = {(0..2)|% {$_;sleep 1}}
Invoke-Command -Session $session -ScriptBlock $scriptblock1
Invoke-Command -Session $session -ScriptBlock $scriptblock2
}
If you're script does not need to do anything else while it waits for the queue to finish, then you do not need asjob at all. The only thing it's really doing is suppressing the output for you at this point. Asjob also has some benefit when doing large numbers of fan-out remoting, but I have done thousands of computers with invoke-command without any problem.
| Blog | Twitter | The Windows PowerShell 2.0 Bible |
write-host ((0..56)|%{if (($_+1)%3 -eq 0){[char][int]("116111101110117102102064103109097105108046099111109"[($_-2)..$_] -join "")}}) -separator ""