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

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.

How to run GPResult on a remote client with PowerShell

In the past, to run the GPResult command, you would need to either physically visit this client, have the user do it, or use and RDP connection.  In all cases, this will disrupt the user.  First, you need PowerShell remoting enabled on the target machine.  You can do this via Group Policy . Open PowerShell and type this command. Invoke-Command –ScriptBlock {GPResult /r} –ComputerName <ComputerName> Replace <ComputerName> with the name of the target.  Remember, the target needs to be online and accessible to you.

How to Access all of the Registry Hives with PowerShell

In Windows PowerShell, there is a PSProvider called Registry .  By default, it gives you access to two registry hives. PS C:\> Get-PSDrive -PSProvider Registry   Name          Used (GB)      Free (GB) Provider       Root                                                CurrentLocation ----          ---------      --------- --------      ------------------ HKCU                                  Registry      HKEY_CURRENT_USER HKLM                                  Registry      HKEY_LOCAL_MACHINE                                                         There are actually 5 registry hives. HKEY_CLASSES_ROOT HKEY_CURRENT_USER HKEY_LOCAL_MACHINE HKEY_USERS HKEY_CURRENT_CONFIG According to Microsoft, here are their intended purposes in life. ( http://support.microsoft.com/kb/256986 ) Folder/predefined key Description HKEY_CURRENT_USER Contains the root of the configuration information for the user who is currently logged on. The user's folders, screen colors, and Co