{"id":50953,"date":"2010-03-18T00:01:00","date_gmt":"2010-03-18T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/03\/18\/hey-scripting-guy-how-can-i-receive-all-windows-powershell-jobs\/"},"modified":"2010-03-18T00:01:00","modified_gmt":"2010-03-18T00:01:00","slug":"hey-scripting-guy-how-can-i-receive-all-windows-powershell-jobs","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-receive-all-windows-powershell-jobs\/","title":{"rendered":"Hey, Scripting Guy! How Can I Receive All Windows PowerShell Jobs?"},"content":{"rendered":"<p><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a><\/p>\n<p>&nbsp;\n<img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"><\/p>\n<p class=\"MsoNormal\">Hey, Scripting Guy! I like the idea of using Windows PowerShell jobs in a script. The problem is that I have multiple jobs I want to run. I would like to be able to wait until all the jobs have completed, and then receive the jobs all at once. Is this possible? I looked around at the job cmdlets and I do not see an option to receive all completed jobs. If you cannot do this, it is fine, but I just thought I would ask. <\/p>\n<p class=\"MsoNormal\">&#8212; KB<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\">Hello KB, <\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. It has been a quiet week in Charlotte, North Carolina. No snow has fallen, no rainstorms, just a little fog and some gentle afternoon showers. Of course, the rain showers happened on the day I headed into town to meet with a Microsoft Technical Account Manager (TAM) who had bought some copies of my Windows PowerShell 2.0 Best Practices book for his customers. He was grubbing for autographs, and it gave me a chance to get out of the house. The nice thing about going to the Microsoft Office in Charlotte is I always run into people I know, and it gives me a chance to spread the word about the upcoming 2010 Scripting Games. It was a great day, even if it did rain a little bit. <\/p>\n<p class=\"MsoNormal\">In between meetings with various TAMs and hanging out in the Hard Drive (our caf&eacute;), I was able to check the <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\"><span><font face=\"Segoe\">scripter@microsoft.com<\/font><\/span><\/a> e-mail inbox. KB, I recalled seeing a script written by James, one of the test engineers on the Windows PowerShell team, and I have adapted his script to meet your specific needs (thanks, James). <\/p>\n<p class=\"MsoNormal\">The ReceiveArrayOfJobs.ps1 script begins by creating an empty array named <b>$arrayJobs<\/b>. I then add three jobs to the array. The first job is the <b>Get-Service<\/b> command; the second is the <b>Get-Process<\/b> command, which pauses execution for a second. Last, a bogus WMI command is added to the array. There is no WMI class named <b>Win32_sofware<\/b> (this command will generate an error, and is used to illustrate that the script will handle jobs that complete with an error as well as jobs that end correctly). This section of the script is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$arrayJobs = @()<br \/>$arrayJobs += Start-Job { Get-Service }<br \/>$arrayJobs += Start-Job { Get-Process ; Start-Sleep -Seconds 1}<br \/>$arrayJobs += Start-Job { Get-wmiObject win32_sofware }<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">In a real-world example, you would probably be running jobs on remote computers. In addition, you probably would not want to hard-code the actual jobs you want to run. You might prefer to read a text file that contains a number of jobs, or use some other sort of device to avoid typing in literal values. This section of the script would be a good one to modify before implementing the script on your network. <\/p>\n<p class=\"MsoNormal\">After the array of jobs has been created, it is time to monitor the jobs to see if they have completed. To do this, the first thing is to assign the value <b>$false<\/b> to the variable <b>$complete<\/b>. The <b>$complete<\/b> variable will be used to determine when the jobs are all completed. This is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$complete = $false<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">If the <b>$complete<\/b> variable is not equal to <b>$true<\/b> (or simply stated while it is not complete), the ReceiveArrayOfJobs.ps1 script checks the array of jobs to see if the status of the job is &lsquo;running.&rsquo; The <b>while<\/b> check is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">while (-not $complete) {<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">If the job is still running, the array of jobs has not completed. To check the state of the array of jobs, the <b>$arrayJobs<\/b> variable is pipe to the <b>Where-Object<\/b> cmdlet. Inside the <b>Where-Object<\/b> cmdlet, the <b>state<\/b> property of the current object on the pipeline is examined to see if its value matches the string &lsquo;running.&rsquo; If a match is found, it is assigned to the <b>$arrayJobsInProgress<\/b> variable. This continues to happen as long as a match is found for the <b>state<\/b> property. This section of the script is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$arrayJobsInProgress = $arrayJobs | <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Where-Object { $_.State -match &#8216;running&#8217; }<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">If no jobs have the state of <b>running<\/b>, a value will not be assigned to the <b>$arrayJobsInProgress<\/b> variable. The &ldquo;all jobs have completed&rdquo; string is displayed on the Windows PowerShell console, and the <b>$complete<\/b> variable is set to <b>$true<\/b>. This will cause the <b>While<\/b> loop to complete, because it only runs as long as the <b>$complete<\/b> variable is equal to <b>$false<\/b>. This section of the script is seen here: <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">if (-not $arrayJobsInProgress) { &#8220;All Jobs Have Completed&#8221; ; $complete = $true } <\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">After the <b>While<\/b> loop is exited, the Windows PowerShell jobs stored in the <b>$arrayJobs<\/b> variable are pipelined to the <b>Receive-Job<\/b> cmdlet and the results from all of the jobs are displayed in the Windows PowerShell console. The complete ReceiveArrayOfJobs.ps1 script is shown here. <\/p>\n<p class=\"CodeBlockScreenedHead\"><strong>ReceiveArrayOfJobs.ps1<\/p>\n<p><\/strong><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$arrayJobs = @()<br \/>$arrayJobs += Start-Job { Get-Service }<br \/>$arrayJobs += Start-Job { Get-Process ; Start-Sleep -Seconds 1}<br \/>$arrayJobs += Start-Job { Get-wmiObject win32_sofware }<\/p>\n<p>$complete = $false<br \/>while (-not $complete) {<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>$arrayJobsInProgress = $arrayJobs | <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Where-Object { $_.State -match &#8216;running&#8217; }<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>if (-not $arrayJobsInProgress) { &#8220;All Jobs Have Completed&#8221; ; $complete = $true } <br \/>}<\/p>\n<p>$arrayJobs | Receive-Job<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">When the ReceiveArrayOfJobs.ps1 script is run in the Windows PowerShell ISE, the output is displayed that is shown in the following image. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of script output in Windows PowerShell ISE\" alt=\"Image of script output in Windows PowerShell ISE\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0318\/hsg-03-18-10-01.jpg\" width=\"600\" height=\"425\"><a href=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0318\/hsg-03-18-10-01.jpg\"><\/a><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">KB, that is all there is to receiving all Windows PowerShell jobs. This also concludes Windows PowerShell Jobs Week. Tomorrow, we will open the virtual mailbag, and answer those questions that do not require a long explanation. That&rsquo;s right, it is time once again for the sensation sweeping the scripting nation: Quick-Hits Friday!<\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\"><font face=\"Segoe\">Twitter<\/font><\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\"><font face=\"Segoe\">Official Scripting Guys Forum<\/font><\/a>. See you tomorrow. Until then, peace.<\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/span><\/b><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Hey, Scripting Guy! I like the idea of using Windows PowerShell jobs in a script. The problem is that I have multiple jobs I want to run. I would like to be able to wait until all the jobs have completed, and then receive the jobs all at once. Is this possible? I looked [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,85,3,4,45],"class_list":["post-50953","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-jobs","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Hey, Scripting Guy! I like the idea of using Windows PowerShell jobs in a script. The problem is that I have multiple jobs I want to run. I would like to be able to wait until all the jobs have completed, and then receive the jobs all at once. Is this possible? I looked [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/50953","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=50953"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/50953\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=50953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=50953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=50953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}