{"id":1395,"date":"2014-05-13T00:01:00","date_gmt":"2014-05-13T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/05\/13\/what-happens-to-powershell-scheduled-job-after-reboot\/"},"modified":"2014-05-13T00:01:00","modified_gmt":"2014-05-13T00:01:00","slug":"what-happens-to-powershell-scheduled-job-after-reboot","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/what-happens-to-powershell-scheduled-job-after-reboot\/","title":{"rendered":"What Happens to PowerShell Scheduled Job After Reboot?"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, continues his discussion about Windows PowerShell scheduled jobs.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Last night was the Expo floor meet-and-greet at TechEd in Houston. It was a crazy time, and we had literally thousands of people stopping by to say &quot;hi,&quot; and to meet up with other Windows PowerShell peeps. It was an awesome opportunity to hang out with so many people who are interested in using Windows PowerShell. The cool thing is that many people had already read yesterday&rsquo;s post about Windows PowerShell scheduled jobs, and several had questions. So I dashed back to my hotel room last night and went to work on today&rsquo;s post.<\/p>\n<p style=\"margin-left:30px\"><b>Note<\/b>&nbsp; This is the second in a series of posts about the Windows PowerShell scheduled jobs feature. You should read the first post, <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/introduction-to-powershell-scheduled-jobs\/\">Introduction to PowerShell Scheduled Jobs<\/a>, before you read today&rsquo;s post.<\/p>\n<p>Yesterday I created a Windows PowerShell scheduled job with a time trigger, and it ran successfully. Then, I shut down my laptop. Now, we are back at the hotel, I power up my laptop, and guess what? When I use the <b>Get-ScheduledJob<\/b> cmdlet, I see that the Windows PowerShell scheduled jobs still exist. They are shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-13-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-13-14-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p style=\"margin-left:30px\"><b>Note&nbsp; <\/b>To use any of the <b>*job<\/b> cmdlets (for example, <b>Get-Job<\/b> or <b>Receive-Job<\/b>), the PSScheduledJob module must load into the current session. This happens when you use a cmdlet such as <b>Get-ScheduledJob<\/b>. You can also specifically import the module by using <b>Import-Module PSScheduledJob<\/b>.<\/p>\n<p>I can also check to see if the job completed and if there is any data left. To do this, I use the <b>Get-Job<\/b> cmdlet. I can then use the name or the job ID. The easiest thing to do is to give the job a decent name, and use the name. This is because the job ID from <b>Get-ScheduledJob<\/b> and the job ID from <b>Get-Job<\/b> are different numbers. When I get the job, I pipe it to the <b>Format-List<\/b> cmdlet as shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-13-14-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-13-14-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>One thing that I like to do is see how long the scheduled job takes to run. This is useful to help to develop some sort of a track record for comparison. I can use <b>the New-TimeSpan<\/b> cmdlet and select the <b>PSBeginTime <\/b>and the <b>PSEndTime <\/b>properties to obtain this information. Here is the command I use:<\/p>\n<p style=\"margin-left:30px\">New-TimeSpan -Start (get-job -name gps).psbegintime -End (get-job -name gps).psendtime<\/p>\n<p>The command returns a <b>TimeSpan<\/b> object. I generally simply return the entire object because, at times, I do not know how long the job runs. Pay really close attention when reading the object because the first portion contains the number of days, hours, minutes, seconds, and milliseconds the job takes to complete. I have to add these parts together to get a total time. Or I can go to the Total* section. For example, this job took a total of 2.2812358 seconds.<\/p>\n<p style=\"margin-left:30px\">Days&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"margin-left:30px\">Hours&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"margin-left:30px\">Minutes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"margin-left:30px\">Seconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 2<\/p>\n<p style=\"margin-left:30px\">Milliseconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 281<\/p>\n<p style=\"margin-left:30px\">Ticks&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 22812358<\/p>\n<p style=\"margin-left:30px\">TotalDays&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 2.64031921296296E-05<\/p>\n<p style=\"margin-left:30px\">TotalHours&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0.000633676611111111<\/p>\n<p style=\"margin-left:30px\">TotalMinutes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0.0380205966666667<\/p>\n<p style=\"margin-left:30px\">TotalSeconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 2.2812358<\/p>\n<p style=\"margin-left:30px\">TotalMilliseconds : 2281.2358<\/p>\n<p>Even though I rebooted my computer, I can still look at the job results. This is because I used the <b>&ndash;Keep<\/b> parameter when viewing the results. This is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-13-14-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-13-14-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>One of the cool things I can do is run the job directly. This is useful, for example, when I have a scheduled job that was set up to run on a schedule, and I want an impromptu run. A classic illustration of this situation is when I have a scheduled backup that runs each morning, but I have made a significant number of changes to a system (or I am getting ready to make a significant number of changes), and I would like to run an impromptu backup without modifying my regularly scheduled backup.<\/p>\n<p>To run a scheduled job as a one-off, all I need to do is to use the <b>Start-Job<\/b> cmdlet and specify the name of the job as shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Start-Job -DefinitionName gps<\/p>\n<p style=\"margin-left:30px\">Id&nbsp;&nbsp;&nbsp;&nbsp; Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PSJobTypeName&nbsp;&nbsp; State&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HasMoreData&nbsp;&nbsp;&nbsp;&nbsp; Location<\/p>\n<p style=\"margin-left:30px\">&#8212;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8212;-&nbsp;&nbsp; &#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8211;<\/p>\n<p style=\"margin-left:30px\">5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PSScheduledJob&nbsp; Running&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; True&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; localhost<\/p>\n<h2>Deleting stuff<\/h2>\n<p>To remove job triggers, I can use the <b>Remove-JobTrigger<\/b> cmdlet. One way to do this is to remove the job trigger by the name of the trigger itself. To find this, I can use the <b>Get-ScheduledJob<\/b> cmdlet and pipe the results to <b>Get-JobTrigger<\/b>. I can also use the ID of the trigger, or I can pipe a scheduled job definition object to the cmdlet. Here are a couple of examples:<\/p>\n<p style=\"margin-left:30px\">Remove-JobTrigger -Id 1<\/p>\n<p style=\"margin-left:30px\">Get-ScheduledJob | Get-JobTrigger<\/p>\n<p>Keep in mind that the input object is a <b>ScheduledJobDefinition<\/b> object that comes from <b>Get-ScheduledJob<\/b>, and not a <b>ScheduledJobTrigger<\/b> object. Therefore, the following command fails:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-13-14-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-13-14-04.png\" alt=\"Image of error message\" title=\"Image of error message\" \/><\/a><\/p>\n<p>To delete Windows PowerShell scheduled jobs, you use the <b>Unregister-ScheduledJob<\/b> cmdlet.<\/p>\n<p style=\"margin-left:30px\"><b>Note&nbsp;<\/b> It is a bit confusing that the PSScheduledJob module uses <b>Register-ScheduledJob<\/b> and <b>Unregister-ScheduledJob<\/b> instead of the New and Remove verbs. But with scheduled jobs, you are actually registering and unregistering, so the verbs make sense from a technical perspective.<\/p>\n<p>If I want to delete all of my Windows PowerShell scheduled jobs, I use the following command:<\/p>\n<p style=\"margin-left:30px\">Get-ScheduledJob | Unregister-ScheduledJob<\/p>\n<p>There you have more info about using Windows PowerShell scheduled jobs. Scheduled Jobs Week will continue tomorrow when I will talk about more cool stuff.<\/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, continues his discussion about Windows PowerShell scheduled jobs. Microsoft Scripting Guy, Ed Wilson, is here. Last night was the Expo floor meet-and-greet at TechEd in Houston. It was a crazy time, and we had literally thousands of people stopping by to say &quot;hi,&quot; and to meet up with other [&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,4,45],"class_list":["post-1395","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-scheduled-tasks","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, continues his discussion about Windows PowerShell scheduled jobs. Microsoft Scripting Guy, Ed Wilson, is here. Last night was the Expo floor meet-and-greet at TechEd in Houston. It was a crazy time, and we had literally thousands of people stopping by to say &quot;hi,&quot; and to meet up with other [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1395","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=1395"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1395\/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=1395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=1395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=1395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}