Skip to main content

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.

Comments

Popular posts from this blog

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.