{"id":508,"date":"2008-07-24T03:00:09","date_gmt":"2008-07-24T11:00:09","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/2008\/07\/24\/more-fun-with-history\/"},"modified":"2016-10-27T13:31:58","modified_gmt":"2016-10-27T20:31:58","slug":"more-fun-with-history","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2008\/07\/24\/more-fun-with-history\/","title":{"rendered":"More Fun with History"},"content":{"rendered":"<p>I blogged a few days ago about taking advantage of PowerShell&#8217;s history. As I was looking at the history object I noticed the ExecutionStatus property. Most of the time it should be &#8220;completed&#8221;. But what about commands that failed? Why not search my history buffer?<\/p>\n<table border=\"0\">\n<tbody>\n<tr>\n<td>\n<pre style=\"border: 1px solid #666666; background-color: blue;\"><span style=\"color: #ffffff;\">PS C:\\&gt; h (1..(h -count 1).id) | where {$_.ExecutionStatus -notmatch \"completed\"}  Id CommandLine  -- -----------  21 dir c:\\test\\test* | &amp;$sb  27  &amp;$executioncontext.InvokeCommand.NewScriptBlock($sb)  28 dir | &amp;$executioncontext.InvokeCommand.NewScriptBlock($sb) 126 gwmi win32_bios -computer server01<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Remember that \u2018h\u2019 is an alias for Get-History. The expression (h -count 1).id will return the id number for the last history item.<\/p>\n<table border=\"0\">\n<tbody>\n<tr>\n<td>\n<pre style=\"border: 1px solid #666666; background-color: blue;\"><span style=\"color: #ffffff;\">PS C:\\&gt; h (1..(h -count 1).id) | where {$_.ExecutionStatus -notmatch \"completed\"} |&gt;&gt; format-list ID,CommandLine,*time&gt;&gt;\r\n\r\nId                 : 21CommandLine        : dir c:\\test\\test* | &amp;$sbStartExecutionTime : 7\/18\/2008 10:14:42 AMEndExecutionTime   : 7\/18\/2008 10:14:46 AM\r\n\r\nId                 : 27CommandLine        :  &amp;$executioncontext.InvokeCommand.NewScriptBlock($sb)StartExecutionTime : 7\/18\/2008 10:16:04 AMEndExecutionTime   : 7\/18\/2008 10:16:13 AM\r\n\r\nId                 : 28CommandLine        : dir | &amp;$executioncontext.InvokeCommand.NewScriptBlock($sb)StartExecutionTime : 7\/18\/2008 10:16:22 AMEndExecutionTime   : 7\/18\/2008 10:16:24 AM\r\n\r\nId                 : 126CommandLine        : gwmi win32_bios -computer server01StartExecutionTime : 7\/18\/2008 12:52:49 PMEndExecutionTime   : 7\/18\/2008 12:53:36 PM<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>I can also get a quick summary of how many expressions worked and how many failed:<\/p>\n<table border=\"0\">\n<tbody>\n<tr>\n<td>\n<pre style=\"border: 1px solid #666666; background-color: blue;\"><span style=\"color: #ffffff;\">PS C:\\&gt; h (1..(h -count 1).id) | Group ExecutionStatus\r\n\r\nCount Name                      Group----- ----                      -----  130 Completed                 {1, 2, 3, 4...}    4 Stopped                   {21, 27, 28, 126}<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Granted you may not need this kind of information on a daily basis but because PowerShell is all about objects its very easy to get if you need it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I blogged a few days ago about taking advantage of PowerShell&#8217;s history. As I was looking at the history object I noticed the ExecutionStatus property. Most of the time it should be &#8220;completed&#8221;. But what about commands that failed? Why not search my history buffer?<\/p>\n","protected":false},"author":3,"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":[25],"tags":[150,239,238,28],"class_list":["post-508","post","type-post","status-publish","format-standard","hentry","category-windows-powershell","tag-cmdlet","tag-get-history","tag-history","tag-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/508","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=508"}],"version-history":[{"count":17,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/508\/revisions"}],"predecessor-version":[{"id":12764,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/508\/revisions\/12764"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}