{"id":130,"date":"2019-07-07T20:39:17","date_gmt":"2019-07-07T19:39:17","guid":{"rendered":"http:\/\/192.168.8.14\/?p=130"},"modified":"2020-03-29T16:19:05","modified_gmt":"2020-03-29T15:19:05","slug":"powershell-tutorial-jobs-part-1","status":"publish","type":"post","link":"https:\/\/www.jasonstreet.com\/?p=130","title":{"rendered":"Powershell tutorial, Jobs part 1"},"content":{"rendered":"\n<p>Powershell code runs synchronously. This means that one line is processed at a time until the end of the script is reached.<\/p>\n\n\n\n<p>This is fine for most scripts but there will be situations where this type of execution is not ideal. A port scanner for example would take forever if it ran synchronously.<\/p>\n\n\n\n<p> When creating a  script that may potentially take a long time to run, you have other  options. You can instead choose to execute code  asynchronously with a concept  called jobs. In PowerShell, a job is a  piece of code that is executed in the  background. It&#8217;s code that starts  but then immediately returns control to  PowerShell to continue  processing other code. Jobs are great for performance  and when a script  doesn&#8217;t depend on the results of prior code execution. <\/p>\n\n\n\n<p>Lets take a look at the following piece of code.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\n$IPToScan = &quot;192.168.0.2&quot;\n\n  Start-Job -Name PingTest -Scriptblock {\n\n    param($TargetIP)\n\n    test-connection $TargetIP -Count 1 -ErrorAction SilentlyContinue &gt; $Null\n    if ($?)\n    {\n      return $TargetIP + &quot; is OK&quot;\n    }else{\n      return $TargetIP + &quot; is dead&quot;\n    }\n  } -Arg $IPToScan\n<\/pre><\/div>\n\n\n<p>Here I am creating and running a job called PingTest.<br>The code for Ping Test is every thing in the scriptblock (Lines 4 &#8211; 13)<\/p>\n\n\n\n<p>My PingTest scriptblock will take the argument $IPToScan as defined in line 14.<\/p>\n\n\n\n<p>Inside the scriptblock that argument is referenced as  $TargetIP<\/p>\n\n\n\n<p>I Ping that IP  $TargetIP  in line 7 but the test-connection command is set to return nothing (-ErrorAction SilentlyContinue &gt; $Null) I do not want the command to return anything.<\/p>\n\n\n\n<p>Next I test whether the test-connection command was successful or not using the $? variable (there are many ways to do this). Depending on the result is what I want the job to return. I did not really need to use the return command but it makes the line easy to understand.<\/p>\n\n\n\n<p>So if I run the code I get the following<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"712\" height=\"262\" src=\"http:\/\/192.168.8.14\/wp-content\/uploads\/2019\/07\/Capture001-1.jpg\" alt=\"\" class=\"wp-image-135\" srcset=\"https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/07\/Capture001-1.jpg 712w, https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/07\/Capture001-1-300x110.jpg 300w\" sizes=\"auto, (max-width: 712px) 100vw, 712px\" \/><\/figure>\n\n\n\n<p>The last part of the above is the output of the script. It shows the job is running in the back ground and has an ID of 1. Powershell jobs run with an odd number 1,3,5, etc.<\/p>\n\n\n\n<p>If I wait a couple of seconds and then run the command get-job. I get the following.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"711\" height=\"85\" src=\"http:\/\/192.168.8.14\/wp-content\/uploads\/2019\/07\/Capture003.jpg\" alt=\"\" class=\"wp-image-136\" srcset=\"https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/07\/Capture003.jpg 711w, https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/07\/Capture003-300x36.jpg 300w\" sizes=\"auto, (max-width: 711px) 100vw, 711px\" \/><\/figure>\n\n\n\n<p>The job State has changed to completed.<br>I can now run the command Receive-job with the job number to the $jobResult variable.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"635\" height=\"89\" src=\"http:\/\/192.168.8.14\/wp-content\/uploads\/2019\/07\/Capture004.jpg\" alt=\"\" class=\"wp-image-137\" srcset=\"https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/07\/Capture004.jpg 635w, https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/07\/Capture004-300x42.jpg 300w\" sizes=\"auto, (max-width: 635px) 100vw, 635px\" \/><\/figure>\n\n\n\n<p>But if I now do a get-job again I see something different.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"724\" height=\"83\" src=\"http:\/\/192.168.8.14\/wp-content\/uploads\/2019\/07\/Capture005.jpg\" alt=\"\" class=\"wp-image-138\" srcset=\"https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/07\/Capture005.jpg 724w, https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/07\/Capture005-300x34.jpg 300w\" sizes=\"auto, (max-width: 724px) 100vw, 724px\" \/><\/figure>\n\n\n\n<p>The HasMoreData attribute has changed from true to False.<br>So if I do a Recive-job 1 again&#8230;.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"291\" height=\"93\" src=\"http:\/\/192.168.8.14\/wp-content\/uploads\/2019\/07\/Capture006.jpg\" alt=\"\" class=\"wp-image-139\"\/><\/figure>\n\n\n\n<p>I don&#8217;t get any output. <br>If I want to keep the job output then I sent it to a variable like I did when I run:<br>$jobResult = receive-job 1<br>Or I could have used the -keep switch<br>$jobResult = receive-job 1 -keep<\/p>\n\n\n\n<p>But regardless of whether I keep the data in the job or copy it out to a variable and work with it, I will need to remove the job.<br>To do that I issue the command remove-job 1<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"384\" height=\"85\" src=\"http:\/\/192.168.8.14\/wp-content\/uploads\/2019\/07\/Capture007.jpg\" alt=\"\" class=\"wp-image-140\" srcset=\"https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/07\/Capture007.jpg 384w, https:\/\/www.jasonstreet.com\/wp-content\/uploads\/2019\/07\/Capture007-300x66.jpg 300w\" sizes=\"auto, (max-width: 384px) 100vw, 384px\" \/><\/figure>\n\n\n\n<p>That is Powershell jobs in a nutshell.<br>In <a href=\"http:\/\/192.168.8.14\/?p=127\">Part 2<\/a> I will show how to manage jobs.<br>In Part 3 I will dig more in to getting data out of jobs <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Powershell code runs synchronously. This means that one line is processed at a time until the end of the script is reached. This is fine for most scripts but there will be situations where this type of execution is not ideal. A port scanner for example would take forever if it ran synchronously. When creating&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4,27],"tags":[26,13,22],"class_list":["post-130","post","type-post","status-publish","format-standard","hentry","category-powershell","category-tutorial","tag-jobs","tag-powershell","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=130"}],"version-history":[{"count":1,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/130\/revisions"}],"predecessor-version":[{"id":232,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/130\/revisions\/232"}],"wp:attachment":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}