Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Monday, June 23, 2014

Using Calculated Properties to get Command Execution Time

We are finishing up day 1 here in my PowerShell class in Hunt Valley, MD.  Just finished up calculated properties and had a request to expand the demonstration that I used a bit.  We were using the Get-History cmdlet to explore calculated properties.  Here is the code and output that we had:

Get-History |

    Select-Object -Property CommandLine,

    @{N="ExecutionTime";

        E={$_.EndExecutionTime - $_.StartExecutionTime}} |

    Sort-Object -Property ExecutionTime -Descending |

    Select-Object -First 5

CommandLine                              ExecutionTime                          

-----------                              -------------                          

Show-Command                             00:01:48.0548277                       

Update-Help                              00:00:27.2509743                       

Get-Process | Select-Object -Property... 00:00:07.7695634                       

Get-Service -Name BITS | Stop-Service... 00:00:05.8634245                       

get-eventlog                             00:00:05.4523953            

 

The question came up if there is an easier way to display the ExecutionTime property.  Our first step was to pipe this information to Get-Member to see the object type.

Get-History |

    Select-Object -Property CommandLine,

    @{N="ExecutionTime";

        E={$_.EndExecutionTime - $_.StartExecutionTime}} |

    Sort-Object -Property ExecutionTime -Descending |

    Select-Object -First 5 |

    Get-Member

    TypeName: Selected.System.Management.Automation.PSCustomObject

 

Name          MemberType   Definition                                   

----          ----------   ----------                                   

Equals        Method       bool Equals(System.Object obj)               

GetHashCode   Method       int GetHashCode()                            

GetType       Method       type GetType()                               

ToString      Method       string ToString()                            

CommandLine   NoteProperty System.String CommandLine=Show-Command       

ExecutionTime NoteProperty System.TimeSpan ExecutionTime=00:01:48.0548277

 

I highlighted in red the object type.  Our next step was to extract the value of this property and send it to Get-Member to see what properties we had to work in the object type System.TimeSpan. As an alternate to this next step, just go to MSDN and look up the .NET object System.TimeSpan.  

Get-History |

    Select-Object -Property CommandLine,

    @{N="ExecutionTime";

        E={$_.EndExecutionTime - $_.StartExecutionTime}} |

    Sort-Object -Property ExecutionTime -Descending |

    Select-Object -ExpandProperty ExecutionTime |

    Get-Member

    TypeName: System.TimeSpan

 

Name              MemberType Definition                                         

----              ---------- ----------                                         

Add               Method     timespan Add(timespan ts)                          

CompareTo         Method     int CompareTo(System.Object value), int CompareTo...

Duration          Method     timespan Duration()                                

Equals            Method     bool Equals(System.Object value), bool Equals(tim...

GetHashCode       Method     int GetHashCode()                                  

GetType           Method     type GetType()                                     

Negate            Method     timespan Negate()                                  

Subtract          Method     timespan Subtract(timespan ts)                     

ToString          Method     string ToString(), string ToString(string format)...

Days              Property   int Days {get;}                                    

Hours             Property   int Hours {get;}                                   

Milliseconds      Property   int Milliseconds {get;}                            

Minutes           Property   int Minutes {get;}                                 

Seconds           Property   int Seconds {get;}                                 

Ticks             Property   long Ticks {get;}                                  

TotalDays         Property   double TotalDays {get;}                            

TotalHours        Property   double TotalHours {get;}                           

TotalMilliseconds Property   double TotalMilliseconds {get;}                    

TotalMinutes      Property   double TotalMinutes {get;}                         

TotalSeconds      Property   double TotalSeconds {get;}    

 

We decided that the property TotalSeconds would be the best one to use in our scenario. In the Expression of the calculated property in line 4, we encapsulate our original expression inside of parenthesis. This allowed us to dot out the property of TotalSeconds from the object with minimal adjustment to our code.

Get-History |

    Select-Object -Property CommandLine,

    @{N="ExecutionTime";

        E={($_.EndExecutionTime - $_.StartExecutionTime).TotalSeconds}} |

    Sort-Object -Property ExecutionTime -Descending |

    Select-Object -First 5

CommandLine                                                         ExecutionTime

-----------                                                         -------------

Show-Command                                                          108.0548277

Update-Help                                                            27.2509743

Get-Process | Select-Object -Property...                                7.7695634

Get-Service -Name BITS | Stop-Service...                                5.8634245

get-eventlog                                                            5.4523953

 

And there you have it.  If you have several ideas on how to accomplish a task, you can use this method to determine which one is most efficient.  You may need to utilized TotalMilliseconds in a test environment for greater precision.  If your task includes reaching out to clients on the network, the network traffic and client availability will have an effect on the times reported.  It is best to test on a local system to determine the most efficient command.

No comments: