{"id":3449,"date":"2013-09-17T10:45:22","date_gmt":"2013-09-17T14:45:22","guid":{"rendered":"http:\/\/jdhitsolutions.com\/blog\/?p=3449"},"modified":"2013-09-16T15:56:06","modified_gmt":"2013-09-16T19:56:06","slug":"a-better-powershell-get-scheduled-job-results","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/scripting\/3449\/a-better-powershell-get-scheduled-job-results\/","title":{"rendered":"A Better PowerShell Get Scheduled Job Results"},"content":{"rendered":"<p><a href=\"http:\/\/jdhitsolutions.com\/blog\/2013\/09\/get-most-recent-powershell-scheduled-job-result\/\" title=\"Get Most Recent PowerShell Scheduled Job Result\" target=\"_blank\">Yesterday <\/a>I posted a quick update on my code to get the most recent scheduled job result in PowerShell. I had been using a simple script. But the more I thought about it, the more I realized I really did need to turn it into a function with more flexibility. When creating a PowerShell based tool you need to think about who might be using it. Even though I only wanted the last result for all enabled jobs, there might be situations where I wanted say the last 2 or 3. Any maybe I wanted to only get results for a specific job. Or maybe all jobs. The bottom line is that I needed more flexibility. Now I have the Get-ScheduledJobResult function.<\/p>\n<pre class=\"lang:ps decode:true \" >#requires -version 3.0\r\n\r\nFunction Get-ScheduledJobResult {\r\n\r\n&lt;#\r\n.Synopsis\r\nGet scheduled job result.\r\n.Description\r\nThis command will retrieve the last job result for a given scheduled job. By\r\ndefault, when you create a scheduled job, PowerShell will retain the last 32 job\r\nresults. This command can help you get the most recent job result. The default\r\nbehavior is to retrieve the newest job for all enabled scheduled jobs. But you\r\ncan specify scheduled jobs by name, indicate if you want to see all jobs, and \r\nalso the number of recent jobs.\r\n\r\nThe command uses a custom type definition based on the Job object.\r\n.Parameter All\r\nDisplay all scheduled jobs. The default is enabled only.\r\n\r\n.Example\r\nPS C:\\&gt; Get-ScheduledJobResult \r\nName  : Download PowerShell v3 Help\r\nID    : 87\r\nState : Completed\r\nRun   : 00:01:40.9564095\r\nStart : 9\/16\/2013 6:00:09 AM\r\nEnd   : 9\/16\/2013 6:01:50 AM\r\n\r\nName  : Daily Work Backup\r\nID    : 55\r\nState : Completed\r\nRun   : 00:00:53.8172548\r\nStart : 9\/15\/2013 11:55:05 PM\r\nEnd   : 9\/15\/2013 11:55:59 PM\r\n.Example\r\nPS C:\\&gt; Get-ScheduledJobResult download* -Newest 5 | Select ID,State,Location,Start,End,Run,HasMoreData | Out-GridView -title \"Download Help Job\"\r\n\r\nGet the newest 5 job results for the Download* scheduled job, selecting a few \r\nproperties and displaying the results in Out-Gridview.\r\n.Notes\r\nLast Updated: 9\/16\/2013\r\nVersion     : 0.9\r\n\r\n  ****************************************************************\r\n  * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *\r\n  * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK.  IF   *\r\n  * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *\r\n  * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING.             *\r\n  ****************************************************************\r\n.Link\r\nhttp:\/\/jdhitsolutions.com\/blog\/2013\/09\/a-better-powershell-get-scheduled-job-results\r\n.Link\r\nGet-Job\r\nGet-ScheduledJob\r\n\r\n.Outputs\r\nCustom ScheduledJob Object\r\n#&gt;\r\n\r\n[cmdletbinding()]\r\nParam(\r\n[Parameter(Position=0)]\r\n[ValidateNotNullorEmpty()]\r\n[string]$Name=\"*\",\r\n[validatescript({$_ -gt 0})]\r\n[int]$Newest=1,\r\n[switch]$All\r\n)\r\n\r\n#only show results for Enabled jobs\r\nTry {\r\n  Write-Verbose \"Getting scheduled jobs for $name\"\r\n  $jobs = Get-ScheduledJob -Name $name -ErrorAction Stop -ErrorVariable ev\r\n}\r\nCatch {\r\n    Write-Warning $ev.errorRecord.Exception \r\n}\r\n\r\nif ($jobs) {\r\n#filter unless asking for all jobs\r\n  Write-Verbose \"Processing found jobs\"\r\n  if ($All) {\r\n    Write-Verbose \"Getting all jobs\"\r\n  }\r\n  else {\r\n    Write-Verbose \"Getting enabled jobs only\"\r\n    $jobs = $jobs | where Enabled\r\n  }\r\n\r\nWrite-Verbose \"Getting newest $newest job results\"\r\n$data = $jobs | foreach { \r\n #get job and select all properties to create a custom object\r\n Get-Job $_.name -newest $Newest | Select * | foreach {\r\n    #insert a new typename\r\n    $_.psobject.typenames.Insert(0,'My.ScheduledJob.Result')\r\n    #write job to the pipeline\r\n    $_\r\n  }\r\n} \r\n \r\n#write result to the pipeline\r\n$data | Sort -Property PSEndTime -Descending\r\n\r\n} #if $jobs\r\n\r\nWrite-Verbose \"Finished\"\r\n} #end function\r\n\r\n#define an alias\r\nSet-Alias -Name gsjr -Value Get-ScheduledJobResult\r\n<\/pre>\n<p>The function takes parameters that I can pass to <a href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkID=223923\" title=\"get online help\" target=\"_blank\">Get-ScheduledJob<\/a> and <a href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkID=113328\" title=\"get online help\" target=\"_blank\">Get-Job<\/a>. For the most part the core functionality remains the same: I get the X number of most recent jobs for scheduled jobs. The difference is that now these values are controlled by parameters. The other benefit to my revision is error handling. Before I had a single pipelined expression, but now I use a Try\/Catch block to get the scheduled job by name, using * as the default. You'll also notice I'm using my own error variable. If there is an error, I can handle it more gracefully. I'll let you test it with a bogus scheduled job name to see what happens.<\/p>\n<p>But perhaps the biggest change is that I define my own object type, based on the Job object. <\/p>\n<pre class=\"lang:ps decode:true \" >$data = $jobs | foreach { \r\n #get job and select all properties to create a custom object\r\n Get-Job $_.name -newest $Newest | Select * | foreach {\r\n    #insert a new typename\r\n    $_.psobject.typenames.Insert(0,'My.ScheduledJob.Result')\r\n    #write job to the pipeline\r\n    $_\r\n  }\r\n} <\/pre>\n<p>For every job result insert a new typename. Because I might have multiple objects I need to insert the typename for each one. As I was working on this I originally was inserting my typename into the Microsoft.PowerShell.ScheduledJob.ScheduledJob object. But my custom formatting wasn't working property, so I found that by selecting all properties, which has the effect of creating a Selected.Microsoft.PowerShell.ScheduledJob.ScheduledJob my changes worked.<\/p>\n<p>Inserting the typename is only part of the process. In the script file that defines the function, I included code to take advantage of <a href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkID=113421\" title=\"get online help\" target=\"_blank\">Update-TypeData<\/a>. In PowerShell 3.0 we no longer need to deal with XML files. Now type updates can be done on-the-fly. So instead of creating my custom properties with Select-Object and custom hash tables, I add them as alias properties. I so something similar to create the Run property.<\/p>\n<pre class=\"lang:ps decode:true \" >#define default property set for my custom type\r\n$typename = 'My.ScheduledJob.Result'\r\n\r\n#define some alias properties\r\n$paramHash= @{\r\nTypeName = $typename\r\nMemberType = \"AliasProperty\"\r\nMemberName = \"Start\"\r\nValue = \"PSBeginTime\"\r\n}\r\nUpdate-TypeData @paramHash -force\r\n\r\n$paramHash.MemberName=\"End\"\r\n$paramHash.Value=\"PSEndTime\"\r\nUpdate-TypeData @paramHash -Force\r\n\r\n#define a custom property\r\n$paramHash.MemberName=\"Run\"\r\n$paramHash.Value={$this.PSEndTime - $this.PSBeginTime}\r\n$paramHash.memberType=\"ScriptProperty\"\r\nUpdate-TypeData @paramHash -Force\r\n\r\n#set default display properties\r\nUpdate-TypeData -TypeName $typename -DefaultDisplayPropertySet Name,ID,State,Run,Start,End -Force<\/pre>\n<p>The last part of my revision is to define the default display property set. The effect is that when I run my function, if I don't specify any other formatting, by default I'll see the properties I want.<\/p>\n<pre class=\"lang:batch decode:true \" >\r\nPS C:\\&gt; Get-ScheduledJobResult\r\nName  : Download PowerShell v3 Help\r\nID    : 89\r\nState : Completed\r\nRun   : 00:01:40.9564095\r\nStart : 9\/16\/2013 6:00:09 AM\r\nEnd   : 9\/16\/2013 6:01:50 AM\r\n\r\nName  : Daily Work Backup\r\nID    : 57\r\nState : Completed\r\nRun   : 00:00:53.8172548\r\nStart : 9\/15\/2013 11:55:05 PM\r\nEnd   : 9\/15\/2013 11:55:59 PM\r\n\r\nName  : Thunderbird Backup\r\nID    : 94\r\nState : Completed\r\nRun   : 00:51:08.2574182\r\nStart : 9\/10\/2013 11:59:00 PM\r\nEnd   : 9\/11\/2013 12:50:08 AM<\/pre>\n<p>And I still have access to all of the other properties as well.<\/p>\n<pre class=\"lang:ps decode:true \" >PS C:\\&gt; Get-ScheduledJobResult download* -Newest 5 | Select ID,State,Location,Start,End,Run,HasMoreData,Command | Out-GridView -title \"Download Help Job\"<\/pre>\n<p><a href=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/get-scheduledjob-latest-ogv.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/get-scheduledjob-latest-ogv-1024x327.png\" alt=\"get-scheduledjob-latest-ogv\" width=\"625\" height=\"199\" class=\"aligncenter size-large wp-image-3450\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/get-scheduledjob-latest-ogv-1024x327.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/get-scheduledjob-latest-ogv-300x95.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/get-scheduledjob-latest-ogv-624x199.png 624w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/get-scheduledjob-latest-ogv.png 1061w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><\/p>\n<p>Now I have a tool, complete with an alias, with defaults that work for me, but if I need to see something else I can adjust the output based on my parameters.  If you want to try this, save the function and type information to the same script file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday I posted a quick update on my code to get the most recent scheduled job result in PowerShell. I had been using a simple script. But the more I thought about it, the more I realized I really did need to turn it into a function with more flexibility. When creating a PowerShell based&#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,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"A Better #PowerShell Get Scheduled Job Results","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[60,359,8],"tags":[224,438,534,383,540,439],"class_list":["post-3449","post","type-post","status-publish","format-standard","hentry","category-best-practices","category-powershell-3-0","category-scripting","tag-function","tag-job","tag-powershell","tag-scheduled-job","tag-scripting","tag-update-typedata"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>A Better PowerShell Get Scheduled Job Results &#8226; The Lonely Administrator<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jdhitsolutions.com\/blog\/scripting\/3449\/a-better-powershell-get-scheduled-job-results\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A Better PowerShell Get Scheduled Job Results &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"Yesterday I posted a quick update on my code to get the most recent scheduled job result in PowerShell. I had been using a simple script. But the more I thought about it, the more I realized I really did need to turn it into a function with more flexibility. When creating a PowerShell based...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/scripting\/3449\/a-better-powershell-get-scheduled-job-results\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2013-09-17T14:45:22+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/get-scheduledjob-latest-ogv-1024x327.png\" \/>\n<meta name=\"author\" content=\"Jeffery Hicks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:site\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeffery Hicks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/3449\\\/a-better-powershell-get-scheduled-job-results\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/3449\\\/a-better-powershell-get-scheduled-job-results\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"A Better PowerShell Get Scheduled Job Results\",\"datePublished\":\"2013-09-17T14:45:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/3449\\\/a-better-powershell-get-scheduled-job-results\\\/\"},\"wordCount\":509,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/3449\\\/a-better-powershell-get-scheduled-job-results\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2013\\\/09\\\/get-scheduledjob-latest-ogv-1024x327.png\",\"keywords\":[\"Function\",\"Job\",\"PowerShell\",\"scheduled job\",\"Scripting\",\"Update-TypeData\"],\"articleSection\":[\"Best Practices\",\"Powershell 3.0\",\"Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/3449\\\/a-better-powershell-get-scheduled-job-results\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/3449\\\/a-better-powershell-get-scheduled-job-results\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/3449\\\/a-better-powershell-get-scheduled-job-results\\\/\",\"name\":\"A Better PowerShell Get Scheduled Job Results &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/3449\\\/a-better-powershell-get-scheduled-job-results\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/3449\\\/a-better-powershell-get-scheduled-job-results\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2013\\\/09\\\/get-scheduledjob-latest-ogv-1024x327.png\",\"datePublished\":\"2013-09-17T14:45:22+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/3449\\\/a-better-powershell-get-scheduled-job-results\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/3449\\\/a-better-powershell-get-scheduled-job-results\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/3449\\\/a-better-powershell-get-scheduled-job-results\\\/#primaryimage\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2013\\\/09\\\/get-scheduledjob-latest-ogv.png\",\"contentUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2013\\\/09\\\/get-scheduledjob-latest-ogv.png\",\"width\":1061,\"height\":339},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/3449\\\/a-better-powershell-get-scheduled-job-results\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Best Practices\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/best-practices\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"A Better PowerShell Get Scheduled Job Results\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/\",\"name\":\"The Lonely Administrator\",\"description\":\"Practical Advice for the Automating IT Pro\",\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\",\"name\":\"Jeffery Hicks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"caption\":\"Jeffery Hicks\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"A Better PowerShell Get Scheduled Job Results &#8226; The Lonely Administrator","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jdhitsolutions.com\/blog\/scripting\/3449\/a-better-powershell-get-scheduled-job-results\/","og_locale":"en_US","og_type":"article","og_title":"A Better PowerShell Get Scheduled Job Results &#8226; The Lonely Administrator","og_description":"Yesterday I posted a quick update on my code to get the most recent scheduled job result in PowerShell. I had been using a simple script. But the more I thought about it, the more I realized I really did need to turn it into a function with more flexibility. When creating a PowerShell based...","og_url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/3449\/a-better-powershell-get-scheduled-job-results\/","og_site_name":"The Lonely Administrator","article_published_time":"2013-09-17T14:45:22+00:00","og_image":[{"url":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/get-scheduledjob-latest-ogv-1024x327.png","type":"","width":"","height":""}],"author":"Jeffery Hicks","twitter_card":"summary_large_image","twitter_creator":"@JeffHicks","twitter_site":"@JeffHicks","twitter_misc":{"Written by":"Jeffery Hicks","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/scripting\/3449\/a-better-powershell-get-scheduled-job-results\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/scripting\/3449\/a-better-powershell-get-scheduled-job-results\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"A Better PowerShell Get Scheduled Job Results","datePublished":"2013-09-17T14:45:22+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/scripting\/3449\/a-better-powershell-get-scheduled-job-results\/"},"wordCount":509,"commentCount":1,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/scripting\/3449\/a-better-powershell-get-scheduled-job-results\/#primaryimage"},"thumbnailUrl":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/get-scheduledjob-latest-ogv-1024x327.png","keywords":["Function","Job","PowerShell","scheduled job","Scripting","Update-TypeData"],"articleSection":["Best Practices","Powershell 3.0","Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jdhitsolutions.com\/blog\/scripting\/3449\/a-better-powershell-get-scheduled-job-results\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/scripting\/3449\/a-better-powershell-get-scheduled-job-results\/","url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/3449\/a-better-powershell-get-scheduled-job-results\/","name":"A Better PowerShell Get Scheduled Job Results &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/scripting\/3449\/a-better-powershell-get-scheduled-job-results\/#primaryimage"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/scripting\/3449\/a-better-powershell-get-scheduled-job-results\/#primaryimage"},"thumbnailUrl":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/get-scheduledjob-latest-ogv-1024x327.png","datePublished":"2013-09-17T14:45:22+00:00","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/scripting\/3449\/a-better-powershell-get-scheduled-job-results\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/scripting\/3449\/a-better-powershell-get-scheduled-job-results\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jdhitsolutions.com\/blog\/scripting\/3449\/a-better-powershell-get-scheduled-job-results\/#primaryimage","url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/get-scheduledjob-latest-ogv.png","contentUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/get-scheduledjob-latest-ogv.png","width":1061,"height":339},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/scripting\/3449\/a-better-powershell-get-scheduled-job-results\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Best Practices","item":"https:\/\/jdhitsolutions.com\/blog\/category\/best-practices\/"},{"@type":"ListItem","position":2,"name":"A Better PowerShell Get Scheduled Job Results"}]},{"@type":"WebSite","@id":"https:\/\/jdhitsolutions.com\/blog\/#website","url":"https:\/\/jdhitsolutions.com\/blog\/","name":"The Lonely Administrator","description":"Practical Advice for the Automating IT Pro","publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jdhitsolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9","name":"Jeffery Hicks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","caption":"Jeffery Hicks"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":3445,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell-3-0\/3445\/get-most-recent-powershell-scheduled-job-result\/","url_meta":{"origin":3449,"position":0},"title":"Get Most Recent PowerShell Scheduled Job Result","author":"Jeffery Hicks","date":"September 16, 2013","format":false,"excerpt":"I think I've posted this before, but in any event even if I did, I've tweaked this code a bit. When you create a scheduled job in PowerShell 3.0, by default PowerShell will keep results on disk for the last 32 times the job ran. This can make it a\u2026","rel":"","context":"In &quot;Powershell 3.0&quot;","block_context":{"text":"Powershell 3.0","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-3-0\/"},"img":{"alt_text":"get-scheduledjob-latest2","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/get-scheduledjob-latest2-1024x233.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/get-scheduledjob-latest2-1024x233.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/get-scheduledjob-latest2-1024x233.png?resize=525%2C300 1.5x"},"classes":[]},{"id":8564,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8564\/cleaning-up-powershell-jobs\/","url_meta":{"origin":3449,"position":1},"title":"Cleaning Up PowerShell Jobs","author":"Jeffery Hicks","date":"September 17, 2021","format":false,"excerpt":"I am a heavy user of PowerShell jobs. Not only background jobs but also scheduled jobs. They are a critical element in my daily workflow. Every time a job runs, especially scheduled jobs, a job artifact remains which you can see using Get-Job. For scheduled jobs, I try to keep\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/remove-job-result.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/remove-job-result.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/remove-job-result.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2833,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2833\/get-scheduled-job-results\/","url_meta":{"origin":3449,"position":2},"title":"Get Scheduled Job Results","author":"Jeffery Hicks","date":"March 5, 2013","format":false,"excerpt":"One of my favorite features in PowerShell 3.0 is the ability to run a PowerShell job as a scheduled task. I can easily setup a PowerShell background job to run a script but have it registered as a scheduled task. All you need is PowerShell 3.0. The job results are\u2026","rel":"","context":"In &quot;Powershell 3.0&quot;","block_context":{"text":"Powershell 3.0","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-3-0\/"},"img":{"alt_text":"talkbubble-v3","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/05\/talkbubble-v3-150x150.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":2297,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2297\/sql-saturday-129-session-material\/","url_meta":{"origin":3449,"position":3},"title":"SQL Saturday 129 Session Material","author":"Jeffery Hicks","date":"May 14, 2012","format":false,"excerpt":"I spoke this past weekend at a SQL Saturday event in Rochester, NY. My first SQL Saturday event and it was a lot of fun. A great turnout and some very interested attendees. I did three PowerShell sessions on jobs, scheduled jobs\/tasks and an intro to workflows. The latter talk\u2026","rel":"","context":"In &quot;Conferences&quot;","block_context":{"text":"Conferences","link":"https:\/\/jdhitsolutions.com\/blog\/category\/conferences\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/talkbubble-150x150.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":337,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/337\/summary-judgment\/","url_meta":{"origin":3449,"position":4},"title":"Summary Judgment","author":"Jeffery Hicks","date":"August 21, 2009","format":false,"excerpt":"While working on a new article for REDMOND magazine about PowerShell 2.0, I wanted to get some cmdlet information. I wanted an easy way to see a list of cmdlets for a given verb or noun. Of course that is easily done with Get-Command. However this only gives my the\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3648,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3648\/remove-all-but-most-recent-powershell-job\/","url_meta":{"origin":3449,"position":5},"title":"Remove All but Most Recent PowerShell job","author":"Jeffery Hicks","date":"April 21, 2014","format":false,"excerpt":"I like sending little PowerShell tweet tips, but this one is a bit too long for Twitter. Here is a one-liner to remove all but the most recent job result for each job name. get-job | group Name | foreach { $_.group | sort PSEndTime -descending | Select -skip 1\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/3449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=3449"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/3449\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=3449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=3449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=3449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}