PowerShell v 2.0 introduces the concept of a background job. Implemented as a PsJob, a job runs a command or expression asynchronously and "in the background" without interacting with the console. Jobs are started by the Start-PSJob command. You can run these background jobs on a local or remote computer.You can check he status of the job by using Get-PSJob (or by getting the job and using the job's JobStateInfo property).
The PSJob features rely on the new remoting features of PowerShell V2, which is one reason why you have to load WinRM prior to installing PowerShell V2.
Here is a demonstration of the background jobs feature. I've taken a leaf from Jeffrey Snover's book and have used his demo-script and have created txt files with the demos. Here's my starting job demo:
<Demo [job-1.txt] Started>
[0]PS D:\foo\v2 > #
[1]PS D:\foo\v2 > # Examine PowerShell V2 jobs feature
[2]PS D:\foo\v2 > # demonstrates start-psjob, get-psjob, receive-psjob, remove-psjob
[3]PS D:\foo\v2 > #
[4]PS D:\foo\v2 > # create a job
[5]PS D:\foo\v2 > $job = start-psjob "get-process" -name demo
[6]PS D:\foo\v2 > #
[7]PS D:\foo\v2 > # get all jobs on the system
[8]PS D:\foo\v2 > # notice the sessionid they are all odd
[9]PS D:\foo\v2 > # our job is at the end of the list
[10]PS D:\foo\v2 > get-psjob
SessionId Name State HasMoreData Command
--------- ---- ----- ----------- -------
1 Completed True get-process
3 demo Completed True get-process
5 demo Completed False get-process
7 demo Completed True get-process
9 demo Completed True get-process
11 demo Completed True get-process
23 demo Completed True get-process
25 demo Completed True get-process
29 demo Completed True get-process
31 demo Running True get-process
[11]PS D:\foo\v2 > #the job's state field (not well named)
[12]PS D:\foo\v2 > $job.jobstateinfo
State Reason
----- ------
Running
[13]PS D:\foo\v2 > #
[14]PS D:\foo\v2 > #
[15]PS D:\foo\v2 > # Look at out job - hopefully it's finished running
[16]PS D:\foo\v2 > $Job
SessionId Name State HasMoreData Command
--------- ---- ----- ----------- -------
31 demo Completed True get-process
[17]PS D:\foo\v2 > # Now go and get the job output
[18]PS D:\foo\v2 > receive-psjob $Job
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
240 8 74584 54476 230 619.81 4888 AcroRd32
116 5 1220 1480 32 1.03 3092 alg
43 2 1320 852 29 562.53 3828 ApntEx
93 3 1744 1816 37 1,875.88 3960 Apoint
282 9 8876 11748 70 367.50 4356 Connect
1267 10 15420 7120 64 655.44 1592 csrss
74 4 1288 1776 30 10.30 2308 ctfmon
140 6 4900 1572 178 7.03 252 EvtEng
683 10 10500 5092 148 3.28 6104 EXCEL
969 26 142368 116180 294 4,901.31 3328 explorer
779 25 460976 405548 736 ...68.50 2788 firefox
502 13 9320 14888 120 354.69 4832 FrameworkService
115 4 2724 1796 36 1.08 1564 ftp95pro
75 2 596 820 19 0.81 1300 GhostStartService
249 7 3948 612 59 2.56 2612 GoogleToolbarNotifier
53 2 1676 980 22 5.53 3796 hidfind
0 0 0 28 0 0 Idle
842 24 37884 19536 178 1,225.72 2900 iexplore
134 4 1896 2216 43 3.70 1144 IFXSPMGT
111 3 2452 1436 36 1.78 1484 IFXTCS
486 21 7112 5432 67 577.66 1512 inetinfo
736 16 10336 4056 56 160.22 1672 lsass
91 4 1720 4760 37 0.16 2016 McScript_InUse
374 100 39488 45000 128 507.33 4012 Mcshield
35 2 1040 3368 27 0.19 5600 Mctray
120 3 1448 2092 34 49.97 232 MDM
297 8 19524 16180 66 592.89 5608 MsMpEng
1040 42 56544 35100 244 2,413.48 3632 msnmsgr
154 6 5836 1064 58 367.59 4436 naPrdMgr
115 4 3792 1760 42 2.20 4144 notepad
54 3 1452 996 31 0.42 5236 notepad
50 3 1404 1560 32 1.58 5588 notepad
327 3 2352 2812 25 69.28 576 nvsvc32
5173 74 145860 111148 548 1,435.66 4432 OUTLOOK
566 5 11428 15408 54 10.64 1132 PKTray
692 13 13456 3728 176 93.25 1044 POWERPNT
346 10 28428 2028 171 2.16 2716 powershell
21 1 172 132 4 0.48 1476 smss
[19]PS D:\foo\v2 > #
[20]PS D:\foo\v2 > #look at list of jobs
[21]PS D:\foo\v2 > get-psjob
SessionId Name State HasMoreData Command
--------- ---- ----- ----------- -------
1 Completed True get-process
3 demo Completed True get-process
5 demo Completed False get-process
7 demo Completed True get-process
9 demo Completed True get-process
11 demo Completed True get-process
23 demo Completed True get-process
25 demo Completed True get-process
29 demo Completed True get-process
31 demo Completed False get-process
[22]PS D:\foo\v2 > # remove our job from the list
[23]PS D:\foo\v2 > remove-psjob $job
[24]PS D:\foo\v2 > # see it's gone
[25]PS D:\foo\v2 > get-psjob
SessionId Name State HasMoreData Command
--------- ---- ----- ----------- -------
1 Completed True get-process
3 demo Completed True get-process
5 demo Completed False get-process
7 demo Completed True get-process
9 demo Completed True get-process
11 demo Completed True get-process
23 demo Completed True get-process
25 demo Completed True get-process
29 demo Completed True get-process
1 comment:
every time I try to run the start-psjob command, I get an error saying "Windows PowerShell remoting features are not supported on Windows Vista RTM."
Does start-psjob use the remoting features?
Seth
Post a Comment