{"id":1060,"date":"2012-08-20T09:00:50","date_gmt":"2012-08-20T13:00:50","guid":{"rendered":"http:\/\/enterpriseadmins.org\/blog\/?p=1060"},"modified":"2012-08-18T15:23:25","modified_gmt":"2012-08-18T19:23:25","slug":"powershell-measure-historical-command","status":"publish","type":"post","link":"https:\/\/enterpriseadmins.org\/blog\/scripting\/powershell-measure-historical-command\/","title":{"rendered":"Powershell &#8211; Measure Historical Command"},"content":{"rendered":"<p>Have you ever been working in a Powershell console and have a command run longer than expected?  Did you wish that you had wrapped that command in a Measure-Command so that you could view execution time?  This has happened to me several times, so recently I started working on a Measure-HistoricalCommand function that would use Get-History to create a New-TimeSpan from the StartExecutionTime and EndExecutionTime properties.  I would like to say thanks to <a href=\"http:\/\/twitter.com\/mtboren\">@mtboren<\/a> from <a href=\"http:\/\/vnugglets.com\/\">vnugglets.com<\/a> who helped add some functionality and speed up this function.  <\/p>\n<pre><code class=\"language-powershell\">\r\nFunction Measure-HistoricalCommand {\r\n\tparam(\r\n\t\t## for getting info about the last X commands\r\n\t\t[parameter(Mandatory=$false,ParameterSetName=\"ByCommandCount\")][alias(\"Count\")][Int]$Last=1,\r\n\t\t## for getting info about a specific command ID\r\n\t\t[parameter(Mandatory=$true,ParameterSetName=\"ByCommandId\")][alias(\"Id\")][Int]$CommandID\r\n\t) ## end param\r\n\r\n\tprocess {\r\n\t\t## create the Get-History command expression to invoke; use -Id or -Count based on the params passed to this function\r\n\t\t$strGetHistoryExpr = \"Get-History \" + $(if ($CommandID) {\"-Id $CommandID\"} else {\"-Count $Last\"})\r\n\t\tInvoke-Expression $strGetHistoryExpr | %{\r\n\t\t\t$ts = New-TimeSpan -Start $_.StartExecutionTime -End $_.EndExecutionTime\r\n\t\t\t## again, take out the array, just return the object straight away\r\n\t\t\t#$myResults += New-Object PSObject -Property @{\r\n\t\t\tNew-Object -TypeName PSObject -Property @{\r\n\t\t\t\tID = $_.ID\r\n\t\t\t\tCommandLine = $_.CommandLIne\r\n\t\t\t\tEndExectuionTime = $_.EndExecutionTime\r\n\t\t\t\tStartExecutionTime = $_.StartExecutionTime\r\n\t\t\t\tTimeSpan = $ts\r\n\t\t\t\tTotalSec = $ts.TotalSeconds\r\n\t\t\t} ## end new-psobject\r\n\t\t} ## end History foreach-object\r\n\t} ## end process\r\n} ## end fn\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever been working in a Powershell console and have a command run longer than expected? Did you wish that you had wrapped that command in a Measure-Command so that you could view execution time? This has happened to &hellip; <a href=\"https:\/\/enterpriseadmins.org\/blog\/scripting\/powershell-measure-historical-command\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":6,"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":[3],"tags":[],"class_list":["post-1060","post","type-post","status-publish","format-standard","hentry","category-scripting"],"_links":{"self":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1060","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/comments?post=1060"}],"version-history":[{"count":3,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1060\/revisions"}],"predecessor-version":[{"id":1063,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1060\/revisions\/1063"}],"wp:attachment":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/media?parent=1060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/categories?post=1060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/tags?post=1060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}