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 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 force a DNS zone to replicate

For many implementations of DNS in a Windows environment, DNS is configured as being Active Directory integrated.  In other words, the DNS zone information is actually stored as a partition in the active directory database.  When Active Directory replicates, the zone data transfers.  For standard DNS deployments, the data is stored in a file.  You have to configure zone transfers manually in the DNS console.   The question in class was how to initiate replication manually.  Once you have properly configured a Primary and secondary DNS server and configured the Primary server to allow zone transfers, you can manually initiate a zone transfer.   Below you can see our test environment.  The image is of to RDP sessions to two different servers.  The DNS console on the left is the primary.  You can see and entry for Test2 that is not in the secondary database.  The servers are named NYC-DC2 (Primary DNS) and NYC-DC1 (Secondary DNS).  The DNS zone is named test.contoso.com . On the se

Disable SMB signing

It never fails.  Once ever couple of months I have a delegate in my class that has to keep a Windows NT4 box running.  There is nothing wrong with that.  Many applications build on Windows NT4 are solid.  Why upgrade and incur cost when no upgrade is really required?  That is generally the reason why Windows NT4 is being used.  Another reason is the vender went out of business, but the application that is required is really good and paid for. Two things to take note of.  If these Windows NT4 clients are going to be authenticating on a Windows Sever 2008 DC, then you may have a problem.  For WinNT 4.0 SP2 and earlier, SMB signing was not supported.  For WinNT4.0 SP3 and earlier, secure channel was not supported. SMB signing helps to prevent Man-in-the-middle attacks.  To open GPMC, click Start , click Run , type gpmc.msc , and then click OK . In the console tree, right-click Default Domain Controllers Policy in Domains\ Current Domain Name \Group Policy objects\Default Domain Co