{"id":1346,"date":"2014-05-16T00:01:00","date_gmt":"2014-05-16T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/05\/16\/troubleshooting-powershell-scheduled-jobs\/"},"modified":"2014-05-16T00:01:00","modified_gmt":"2014-05-16T00:01:00","slug":"troubleshooting-powershell-scheduled-jobs","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/troubleshooting-powershell-scheduled-jobs\/","title":{"rendered":"Troubleshooting PowerShell Scheduled Jobs"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, talks about troubleshooting Windows PowerShell scheduled jobs.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This has been an awesome week at TechEd 2014 in Houston. I can avoid the &quot;Houston we have a problem&quot; joke for the simple reason that the Scripting Wife and I are on our way home to Charlotte, N.C. We saw a tremendous number of Windows PowerShell fans this week, and I can tell you that being the Scripting Guy is a lot of fun when representing a technology as awesome as Windows PowerShell. I mean, dude (or dudette), Windows PowerShell rocks, and it helps solve some difficult problems.<\/p>\n<p>There were three Windows PowerShell resources that I was excited to talk to people about when they came to the Scripting Guys booth at TechEd:<\/p>\n<ul>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-introducing-script-browser-and-script-analyzer\/\" target=\"_blank\">Script Browser and Script Analyzer<\/a>&nbsp;<\/li>\n<li><a href=\"https:\/\/github.com\/lzybkr\/PSReadLine\" target=\"_blank\">PSReadLine<\/a><\/li>\n<li><a href=\"http:\/\/www.powershellmagazine.com\/\" target=\"_blank\">PowerShell Magazine<\/a><\/li>\n<\/ul>\n<p>Check them out. All three of these resources can help make your life easier and better. Of course, there is the Hey, Scripting Guy! Blog&mdash;but hey, you are already here.<\/p>\n<p>Today I want to talk a bit about troubleshooting scheduled jobs. Here are some things to help you when you have to troubleshoot.<\/p>\n<h3>Import the module&nbsp;&nbsp;<\/h3>\n<p>To get the results of a Windows PowerShell scheduled job, you use the <b>Get-Job<\/b> cmdlet. But if you use that cmdlet and it shows no results from any scheduled jobs, don&rsquo;t panic. It probably means that you have not imported the PSScheduledJob module.<\/p>\n<p>Often I do not need to import it because it automatically loads when I use any cmdlet from the module, such as <b>Get-ScheduledJob<\/b>. But if I open a fresh Windows PowerShell console and immediately type <b>Get-Job<\/b>, I might not see the results I want. So remember, first import the module as shown here (<strong>ipmo<\/strong> is an alias for<strong> Import-Module<\/strong>):<\/p>\n<p style=\"margin-left:30px\">ipmo PSScheduledJob<\/p>\n<h3>Look in the scheduled job Output directory&nbsp;&nbsp;<\/h3>\n<p>If you are not finding the results you seek, it is time to look in the scheduled job Output directory. This is located at the following path:<\/p>\n<p style=\"margin-left:30px\">home\\AppData\\Local\\Microsoft\\Windows\\PowerShell\\ScheduledJob<\/p>\n<p>On my computer, this resolves to the following:<\/p>\n<p style=\"margin-left:30px\">C:\\Users\\ed\\AppData\\Local\\Microsoft\\Windows\\PowerShell\\ScheduledJobs<\/p>\n<p>Under ScheduledJobs, you will find a folder for the scheduled job and an Output folder as shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-16-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-16-14-01.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>In the Output folder, there should be a results.xml file and a time-stamped folder for each execution of the scheduled job. If someone clears the job history, these folders are deleted. If there are no folders and XML files in the output directory, the job results cannot be displayed.<\/p>\n<h3>Use &ndash;Keep with Receive-Job&nbsp;&nbsp;<\/h3>\n<p>To receive the results from a scheduled job (assuming you do not write to an output file or some other file on a disk storage mechanism), you use the <b>Receive-Job<\/b> cmdlet, like for any other Windows PowerShell job.<\/p>\n<p>But if you use <b>Receive-Job<\/b> and nothing comes back, it is possible that you have (or someone else has) already used <b>Receive-Job<\/b> to receive the output, and the <b>&ndash;Keep <\/b>parameter was not used. As a best practice, I always use <b>&ndash;Keep<\/b> with <b>Receive-Job<\/b> to keep myself from inadvertently deleting the job output.<\/p>\n<h3>Check the ExecutionHistoryLength value<\/h3>\n<p>If job results are still not forthcoming for a job, it is possible that the number of job instances exceeds the <b>ExecutionHistoryLength<\/b> value for that particular job. The default value for a Windows PowerShell scheduled job is 32. To double-check to see what this value is, you can use the following command:<\/p>\n<p style=\"margin-left:30px\">(Get-ScheduledJob getservice).ExecutionHistoryLength<\/p>\n<p><b>&nbsp; &nbsp; &nbsp;Note<\/b>&nbsp;&nbsp;Yesterday I talked about setting this value in <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/advanced-powershell-scheduled-jobs\/\" target=\"_blank\">Advanced PowerShell Scheduled Jobs<\/a>.<\/p>\n<h3>Check if Start-Job was used<\/h3>\n<p>The Windows PowerShell scheduled job may have been executed (immediately) by using <b>Start-Job<\/b>. <b>Start-Job<\/b> runs a normal Windows PowerShell background job, and the execution history and results are not stored to a disk.<\/p>\n<h3>Check if the job is disabled<\/h3>\n<p>If a Windows PowerShell scheduled job does not run, it might be disabled. By default, all newly created Windows PowerShell scheduled jobs are enabled. But it might have been created as disabled, or it might have been disabled at a later date.<\/p>\n<p>Use the <b>Get-ScheduledJob<\/b> cmdlet to see if the scheduled job is enabled. The <b>Disable-ScheduledJob<\/b> cmdlet disables a Windows PowerShell scheduled job, and the <b>Enable-ScheduledJob<\/b> cmdlet enables a scheduled job. The following command illustrates enabling a scheduled job:<\/p>\n<p style=\"margin-left:30px\">Get-ScheduledJob | Enable-ScheduledJob<\/p>\n<p>The following image shows checking and enabling a scheduled job:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-16-14-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-16-14-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h3>Check if the job failed<\/h3>\n<p>It is possible that the scheduled job failed. To see if it failed, use the <b>Get-WinEvent<\/b> cmdlet, for example:<\/p>\n<p style=\"margin-left:30px\">Get-WinEvent -LogName Microsoft-Windows-TaskScheduler\/Operational |<\/p>\n<p style=\"margin-left:30px\">Where {$_.Message -like &quot;*fail*&quot;}<\/p>\n<h3>Check the permissions<\/h3>\n<p>Permissions are one common cause for scheduled jobs failing. Scheduled jobs run with the permissions of the user who created the job or the permissions of the user who is specified by the <b>Credential<\/b> parameter in the <b>Register-ScheduledJob<\/b> or <b>Set-ScheduledJob<\/b> command. If that user does not have permissions to run the commands, the job fails.<\/p>\n<p>That is all there is to troubleshooting Windows PowerShell scheduled jobs.&nbsp; Join me tomorrow when I will have a guest post about Best Practices for enterprise Windows PowerShell scripting.<\/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><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about troubleshooting Windows PowerShell scheduled jobs. Microsoft Scripting Guy, Ed Wilson, is here. This has been an awesome week at TechEd 2014 in Houston. I can avoid the &quot;Houston we have a problem&quot; joke for the simple reason that the Scripting Wife and I are on our way [&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-1346","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 troubleshooting Windows PowerShell scheduled jobs. Microsoft Scripting Guy, Ed Wilson, is here. This has been an awesome week at TechEd 2014 in Houston. I can avoid the &quot;Houston we have a problem&quot; joke for the simple reason that the Scripting Wife and I are on our way [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1346","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=1346"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1346\/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=1346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=1346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=1346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}