{"id":2681,"date":"2011-04-04T13:30:58","date_gmt":"2011-04-04T13:30:58","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2011\/04\/04\/scaling-and-queuing-powershell-background-jobs\/"},"modified":"2019-02-18T13:05:48","modified_gmt":"2019-02-18T20:05:48","slug":"scaling-and-queuing-powershell-background-jobs","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/scaling-and-queuing-powershell-background-jobs\/","title":{"rendered":"Scaling and Queuing PowerShell Background Jobs"},"content":{"rendered":"<p>A couple of months ago I had asked the PowerShell MVPs for suggestions on blog topics. Karl Prosser, one of our awesome MVPs, brought up the topic of scaling and queuing background jobs.<\/p>\n<p>The scenario is familiar: You have a file containing a bunch of input that you want to process and you don&rsquo;t want to overburden your computer by starting up hundreds of instances of PowerShell at once to process them.<\/p>\n<p>After playing around for about an hour on Friday afternoon, here is what I came up with&hellip; This example assumes you have a text file containing the names of many event logs and you want to get the content of each log.<\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Console\"><span style=\"color: #006400;font-size: x-small\"># How many jobs we should run simultaneously<\/span><\/span><\/span><span style=\"font-family:;color:\"><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Lucida Console\"><span style=\"font-size: x-small\"><span style=\"font-family:;color:\"><span style=\"color: #ff4500\">$maxConcurrentJobs<\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #a9a9a9\">=<\/span><\/span><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #800080\">3<\/span><\/span><span style=\"color:\"><span style=\"color: #000000\">;<\/span><\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Console\"><span style=\"color: #000000;font-size: x-small\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Console\"><span style=\"color: #006400;font-size: x-small\"># Read the input and queue it up<\/span><\/span><\/span><span style=\"font-family:;color:\"><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Lucida Console\"><span style=\"font-size: x-small\"><span style=\"font-family:;color:\"><span style=\"color: #ff4500\">$jobInput<\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #a9a9a9\">=<\/span><\/span><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #0000ff\">get-content<\/span><\/span><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #8a2be2\">.\\input.txt<\/span><\/span><span style=\"color:\"><\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Lucida Console\"><span style=\"font-size: x-small\"><span style=\"font-family:;color:\"><span style=\"color: #ff4500\">$queue<\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #a9a9a9\">=<\/span><\/span><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #008080\">[System.Collections.Queue]<\/span><\/span><span style=\"color:\"><span style=\"color: #a9a9a9\">::<\/span><\/span><span style=\"color: #000000\"><span style=\"color:\">Synchronized(<\/span> <span style=\"color:\">(<\/span><\/span><span style=\"color:\"><span style=\"color: #0000ff\">New-Object<\/span><\/span><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #8a2be2\">System.Collections.Queue<\/span><\/span><span style=\"color: #000000\"><span style=\"color:\">)<\/span> <\/span><span style=\"color:\"><span style=\"color: #000000\">)<\/span><\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Lucida Console\"><span style=\"font-size: x-small\"><span style=\"font-family:;color:\"><span style=\"color: #00008b\">foreach<\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #000000\">(<\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #ff4500\">$item<\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #00008b\">in<\/span><\/span><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #ff4500\">$jobInput<\/span><\/span><span style=\"color:\"><span style=\"color: #000000\">)<\/span><\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Console\"><span style=\"color: #000000;font-size: x-small\">{<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Console\"><span style=\"font-size: x-small\"><span><span style=\"color: #000000\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"color:\"><span style=\"color: #ff4500\">$queue<\/span><\/span><span style=\"color:\"><span style=\"color: #a9a9a9\">.<\/span><\/span><span style=\"color:\"><span style=\"color: #000000\">Enqueue(<\/span><\/span><span style=\"color:\"><span style=\"color: #ff4500\">$item<\/span><\/span><span style=\"color:\"><span style=\"color: #000000\">)<\/span><\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Console\"><span style=\"color: #000000;font-size: x-small\">}<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Console\"><span style=\"color: #000000;font-size: x-small\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Console\"><span style=\"color: #000000;font-size: x-small\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Console\"><span style=\"color: #006400;font-size: x-small\"># Function that pops input off the queue and starts a job with it<\/span><\/span><\/span><span style=\"font-family:;color:\"><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Lucida Console\"><span style=\"font-size: x-small\"><span style=\"font-family:;color:\"><span style=\"color: #00008b\">function<\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #8a2be2\">RunJobFromQueue<\/span><\/span><span style=\"color:\"><\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Console\"><span style=\"color: #000000;font-size: x-small\">{<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Console\"><span style=\"font-size: x-small\"><span><span style=\"color: #000000\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"color:\"><span style=\"color: #00008b\">if<\/span><\/span><span style=\"color: #000000\"><span style=\"color:\">(<\/span> <\/span><span style=\"color:\"><span style=\"color: #ff4500\">$queue<\/span><\/span><span style=\"color:\"><span style=\"color: #a9a9a9\">.<\/span><\/span><span style=\"color: #000000\"><span style=\"color:\">Count<\/span> <\/span><span style=\"color:\"><span style=\"color: #a9a9a9\">-gt<\/span><\/span><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #800080\">0<\/span><\/span><span style=\"color:\"><span style=\"color: #000000\">)<\/span><\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Console\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color:\">{<\/span><\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Console\"><span style=\"font-size: x-small\"><span><span style=\"color: #000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"color:\"><span style=\"color: #ff4500\">$j<\/span><\/span><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #a9a9a9\">=<\/span><\/span><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #0000ff\">Start-Job<\/span><\/span><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #000080\">-ScriptBlock<\/span><\/span><span style=\"color: #000000\"> <span style=\"color:\">{<\/span><\/span><span style=\"color:\"><span style=\"color: #00008b\">param<\/span><\/span><span style=\"color:\"><span style=\"color: #000000\">(<\/span><\/span><span style=\"color:\"><span style=\"color: #ff4500\">$x<\/span><\/span><span style=\"color: #000000\"><span style=\"color:\">);<\/span> <\/span><span style=\"color:\"><span style=\"color: #0000ff\">Get-WinEvent<\/span><\/span><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #000080\">-LogName<\/span><\/span><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #ff4500\">$x<\/span><\/span><span style=\"color: #000000\"><span style=\"color:\">}<\/span> <\/span><span style=\"color:\"><span style=\"color: #000080\">-ArgumentList<\/span><\/span><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #ff4500\">$queue<\/span><\/span><span style=\"color:\"><span style=\"color: #a9a9a9\">.<\/span><\/span><span style=\"color:\"><span style=\"color: #000000\">Dequeue()<\/span><\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:\"><span style=\"font-size: x-small\"><span style=\"font-family: Lucida Console\"><span><span style=\"color: #000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"color:\"><span style=\"color: #0000ff\">Register-ObjectEvent<\/span><\/span><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #000080\">-InputObject<\/span><\/span><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #ff4500\">$j<\/span><\/span><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #000080\">-EventName<\/span><\/span><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #8a2be2\">StateChanged<\/span><\/span><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #000080\">-Action<\/span><\/span><span style=\"color: #000000\"> <span style=\"color:\">{<\/span> <\/span><span style=\"color:\"><span style=\"color: #0000ff\">RunJobFromQueue<\/span><\/span><span style=\"color: #000000\"><span style=\"color:\">;<\/span> <\/span><span style=\"color:\"><span style=\"color: #0000ff\">Unregister-Event<\/span><\/span><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #ff4500\">$eventsubscriber<\/span><\/span><span style=\"color:\"><span style=\"color: #a9a9a9\">.<\/span><\/span><span style=\"color: #000000\"><span style=\"color:\">SourceIdentifier;<\/span> <\/span><span style=\"color:\"><span style=\"color: #0000ff\">Remove-Job<\/span><\/span><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #ff4500\">$eventsubscriber<\/span><\/span><span style=\"color:\"><span style=\"color: #a9a9a9\">.<\/span><\/span><span style=\"color: #000000\"><span style=\"color:\">SourceIdentifier<\/span> <span style=\"color:\">}<\/span> <\/span><span style=\"color:\"><span style=\"color: #a9a9a9\">|<\/span><\/span><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #0000ff\">Out-Null<\/span><\/span><\/span><span style=\"color:\"><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Console\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color:\">}<\/span><\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Console\"><span style=\"color: #000000;font-size: x-small\">}<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Console\"><span style=\"color: #000000;font-size: x-small\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Console\"><span style=\"color: #000000;font-size: x-small\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Console\"><span style=\"color: #006400;font-size: x-small\"># Start up to the max number of concurrent jobs <\/span><\/span><\/span><span style=\"font-family:;color:\"><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Console\"><span style=\"color: #006400;font-size: x-small\"># Each job will take care of running the rest<\/span><\/span><\/span><span style=\"font-family:;color:\"><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Lucida Console\"><span style=\"font-size: x-small\"><span style=\"font-family:;color:\"><span style=\"color: #00008b\">for<\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #000000\">(<\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #ff4500\">$i<\/span><\/span><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #a9a9a9\">=<\/span><\/span><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #800080\">0<\/span><\/span><span style=\"color: #000000\"><span style=\"color:\">;<\/span> <\/span><span style=\"color:\"><span style=\"color: #ff4500\">$i<\/span><\/span><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #a9a9a9\">-lt<\/span><\/span><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #ff4500\">$maxConcurrentJobs<\/span><\/span><span style=\"color: #000000\"><span style=\"color:\">;<\/span> <\/span><span style=\"color:\"><span style=\"color: #ff4500\">$i<\/span><\/span><span style=\"color:\"><span style=\"color: #a9a9a9\">++<\/span><\/span><span style=\"color: #000000\"> <\/span><span style=\"color:\"><span style=\"color: #000000\">)<\/span><\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Console\"><span style=\"color: #000000;font-size: x-small\">{<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:\"><span style=\"font-size: x-small\"><span style=\"font-family: Lucida Console\"><span><span style=\"color: #000000\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"color:\"><span style=\"color: #0000ff\">RunJobFromQueue<\/span><\/span><\/span><span style=\"color:\"><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13pt;list-style-type: disc;margin: 0in 0in 10pt\"><span style=\"line-height: 12pt;font-family:;color:\"><span style=\"font-family: Lucida Console\"><span style=\"color: #000000;font-size: x-small\">}<\/span><\/span><\/span><\/p>\n<p>The English version of this script is:<\/p>\n<ul>\n<li>Given a file input.txt containing the name of many event logs, queue up each line of input <\/li>\n<li>Kick off a small number of jobs to process one line of input each. Each job just gets the content of a particular log. <\/li>\n<li>When a job finishes (determined by the StateChanged Event), start a new job with the next piece of input from the queue <\/li>\n<li>Clean up the jobs corresponding to the event subscription so at the end we only have jobs containing event data <\/li>\n<\/ul>\n<p>The &ldquo;Synchronized&rdquo; code you see when defining the queue is just for good measure to make sure that only one job can access it at a time. <\/p>\n<p>Have something you want to see on the PowerShell blog? Leave a comment&hellip; Can&rsquo;t promise we&rsquo;ll get to everything but it&rsquo;s nice to see what everyone is interested in.<\/p>\n<p>&nbsp;<\/p>\n<p>Travis Jones <br \/>Windows PowerShell PM <br \/>Microsoft Corporation<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A couple of months ago I had asked the PowerShell MVPs for suggestions on blog topics. Karl Prosser, one of our awesome MVPs, brought up the topic of scaling and queuing background jobs. The scenario is familiar: You have a file containing a bunch of input that you want to process and you don&rsquo;t want [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[124,160,161,4,219,270],"class_list":["post-2681","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-community","tag-event-log","tag-events","tag-howto","tag-mvp","tag-powershell-v2"],"acf":[],"blog_post_summary":"<p>A couple of months ago I had asked the PowerShell MVPs for suggestions on blog topics. Karl Prosser, one of our awesome MVPs, brought up the topic of scaling and queuing background jobs. The scenario is familiar: You have a file containing a bunch of input that you want to process and you don&rsquo;t want [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/2681","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=2681"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/2681\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=2681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=2681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=2681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}