{"id":1353,"date":"2014-05-15T00:01:00","date_gmt":"2014-05-15T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/05\/15\/advanced-powershell-scheduled-jobs\/"},"modified":"2014-05-15T00:01:00","modified_gmt":"2014-05-15T00:01:00","slug":"advanced-powershell-scheduled-jobs","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/advanced-powershell-scheduled-jobs\/","title":{"rendered":"Advanced PowerShell Scheduled Jobs"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, talks about advanced aspects of Windows PowerShell scheduled jobs.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today I want to look at some of the more advanced aspects of Windows PowerShell scheduled jobs. Windows PowerShell scheduled jobs do not have to be complicated. But because Windows PowerShell scheduled jobs are very flexible, there are many options.<\/p>\n<h2>Running a scheduled job manually<\/h2>\n<p>There are two ways to manually run a scheduled job instead of running it as an actual scheduled job with a trigger. The first way is to use the <b>&ndash;RunNow<\/b> parameter when creating a new scheduled job with the <b>Register-ScheduledJob<\/b> cmdlet. In the following command, I get a list of service information on my Surface Pro&nbsp;2, and I pipe the output to a text file in the C:\\fso directory. I give the scheduled job a name, and I elect to run the job immediately, instead at a later time via a trigger:<\/p>\n<p style=\"margin-left:30px\">Register-ScheduledJob -Name GetGSV -ScriptBlock {GSV &gt; c:\\fso\\gsv.txt} &ndash;RunNow<\/p>\n<p>I then check the status of the job by using the <b>Get-Job<\/b> cmdlet. I pipe the object to the <b>Format-List<\/b> cmdlet so I can see all the details:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-15-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-15-14-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>The other way to run a scheduled job immediately is to use the <b>Start-Job<\/b> cmdlet. An example of this is shown here:<\/p>\n<p style=\"margin-left:30px\">Start-Job -DefinitionName getgsv<\/p>\n<p>Jobs that are started by using the <b>Start-Job<\/b> cmdlet are standard Windows PowerShell background jobs, not instances of the scheduled job. Like all background jobs, these jobs start immediately. They are not subject to job options or affected by job triggers. Their output is not saved in the Output directory of the scheduled job directory.<\/p>\n<p>Renaming a scheduled job is fairly easy. I use the <b>&ndash;name<\/b> parameter of the <b>Set-ScheduledJob<\/b> cmdlet. In the following example, I rename the <b>GetGSV<\/b> scheduled job to <b>GetService<\/b>. Notice that at this time, there is only a single scheduled job, so there was no reason to specify parameters for the <b>Get-ScheduledJob<\/b> cmdlet because it is only returning a single job.<\/p>\n<p style=\"margin-left:30px\">Get-ScheduledJob | Set-ScheduledJob -Name GetService<\/p>\n<p>The following image shows where I use <b>Get-ScheduledJob<\/b> to find scheduled jobs, and I rename the job. Then I again use <b>Get-ScheduledJob<\/b> to see what jobs are scheduled. You will see that the name change took place.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-15-14-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-15-14-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>To change the command (or the script) that a scheduled job runs, I use exactly the same technique. In the following example, I change the script block to use the full cmdlet name, and then I select the <b>Status<\/b>, <b>Name<\/b>, and <b>DisplayName<\/b> of the services. I then sort the results and pipe them to a text file. I also change it so that I am not appending to the file, but overwriting the file. The command is shown here:<\/p>\n<p>Get-ScheduledJob | Set-ScheduledJob -ScriptBlock {Get-Service | select status, name, displayname | sort status &gt; c:\\fso\\gsv.txt}<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-15-14-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-15-14-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>I might want to generate a report about the history of my scheduled job. To do this, I once again use Windows PowerShell and the <b>Get-Job<\/b> cmdlet. This time, however, I get the <b>PSBeginTime<\/b> property and the <b>PSEndTime<\/b> property, and I calculate the elapsed time for the job. I then display the output in a table. The command is shown here:<\/p>\n<p style=\"margin-left:30px\">Get-Job getservice |<\/p>\n<p style=\"margin-left:30px\">ft ID, PSBeginTime, PSEndTime, @{L=&#039;totaltime&#039;;E={$_.psendtime &#8211; $_.psbegintime}} &ndash;auto<\/p>\n<p>The command and the output from the command is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-15-14-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-15-14-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>I can clear the execution history of scheduled job by using the <b>&ndash;ClearExecutionHistory<\/b> parameter from the <b>Set-ScheduledJob<\/b> cmdlet as shown here:<\/p>\n<p style=\"margin-left:30px\">Get-ScheduledJob getservice | Set-ScheduledJob -ClearExecutionHistory<\/p>\n<p>I can also configure the execution history value at the time of scheduled job creation. (By default, it keeps 32 history items for the job.) To do this, I use the <b>&ndash;MaxResultCount<\/b> parameter. This is the same parameter name I use with <b>Set-ScheduledJob<\/b> if I want to change the number of job items stored in the history. This technique is shown here:<\/p>\n<p style=\"margin-left:30px\">Register-ScheduledJob -Name GetService &nbsp;-ScriptBlock {gsv &gt; c:\\fso\\gsv.txt} -MaxResultCount 12<\/p>\n<p>That is all there is to using advanced scheduled jobs. Windows PowerShell Scheduled Job Week will continue tomorrow when I will talk about troubleshooting scheduled jobs.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about advanced aspects of Windows PowerShell scheduled jobs. Microsoft Scripting Guy, Ed Wilson, is here. Today I want to look at some of the more advanced aspects of Windows PowerShell scheduled jobs. Windows PowerShell scheduled jobs do not have to be complicated. But because Windows PowerShell scheduled jobs [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[31,32,3,45],"class_list":["post-1353","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-scheduled-tasks","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about advanced aspects of Windows PowerShell scheduled jobs. Microsoft Scripting Guy, Ed Wilson, is here. Today I want to look at some of the more advanced aspects of Windows PowerShell scheduled jobs. Windows PowerShell scheduled jobs do not have to be complicated. But because Windows PowerShell scheduled jobs [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1353","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=1353"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1353\/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=1353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=1353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=1353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}