Average Rating:

4.50 by 2 users.

51 Downloads, 179 Views

2 Comment(s)

Beginner_10_2011.ps1

By: David Temple 21 Apr 2011 11:56 PM UTC in the category: Beginner Event 10

Tags:

Description:

SYNOPSIS
Finds the Average Milliseconds for a CMDLET or Oneliner.

DESCRIPTION
The Script will process a provided CMDLET or Onliner and determine the Average Millisecond Time it took for PowerShell to run it.

IT IS IMPORTANT TO SURROUND THE VALUE OF CMDLET WITH SINGLE QUOTES.

PLEASE SEE THE EXAMPLES.

After the process finishes it will report the following for example:

"Average time of 5 runs of 5 seconds is 4998.5468 milliseconds."

PARAMETERS
-Iterations
Iterations indicates how many times this process will loop through the provided CMDLET or Oneliner (Note: The default value is "5")

-CMDLET
Name of the CMDLET (with or without Parameters) and or a Oneliner. IT IS IMPORTANT TO SURROUND THE VALUE OF CMDLET WITH SINGLE QUOTES. (Note: You must enter a CMDLET Parameter. It is Mandatory and will prompt you if you forget. Again if you use a CMDLET with Parameters or you are using a Oneliner, surround the CMDLET Value with Single Quotes. See Examples)

EXAMPLE
.\Beginner_10_2011.ps1 -Iterations 5 -CMDLET 'Start-Sleep -Seconds 5'

Average time of 5 runs of 5 seconds is 5004.4389 milliseconds.

NOTE: The Single Quotes.

EXAMPLE
.\Beginner_10_2011.ps1 -CMDLET 'Get-ChildItem -Path C:\Windows\System32 -Filter "*.dll"'

Average time of 5 runs of 5 seconds is 150.44608 milliseconds.

NOTE: The Single Quotes.

EXAMPLE
.\Beginner_10_2011.ps1 -Iterations 2 -CMDLET 'Get-ChildItem C:\Windows -Filter "*.txt" -Recurse'

Average time of 2 runs of 5 seconds is 821.15165 milliseconds.

NOTE: The Single Quotes.
<#
.SYNOPSIS
   Finds the Average Milliseconds for a CMDLET or Oneliner.

.DESCRIPTION
   The Script will process a provided CMDLET or Onliner and determine the 
   Average Millisecond Time it took for PowerShell to run it.
   
   IT IS IMPORTANT TO SURROUND THE VALUE OF CMDLET WITH SINGLE QUOTES.
   
   PLEASE SEE THE EXAMPLES.

   After the process finishes it will report the following for example:
   
   "Average time of 5 runs of 5 seconds is 4998.5468 milliseconds."

.PARAMETER None
   -Iterations
     Iterations indicates how many times this process will loop through the provided 
     CMDLET or Oneliner
     (Note: The Default value is "5")
     
   -CMDLET
     Name of the CMDLET (with or without Parameters) and or a Oneliner. IT IS 
     IMPORTANT TO SURROUND THE VALUE OF CMDLET WITH SINGLE QUOTES.
     (Note: You must enter a CMDLET Parameter. It is Mandatory and will prompt 
     you if you forget. Again if you use a CMDLET with Parameters or you are using 
     a Oneliner, surround the CMDLET Value with Single Quotes. See Examples)

.EXAMPLE
   .\Beginner_10_2011.ps1 -Iterations 5 -CMDLET 'Start-Sleep -Seconds 5'
   
   Average time of 5 runs of 5 seconds is 5004.4389 milliseconds.

   NOTE: The Single Quotes.
   
.EXAMPLE
   .\Beginner_10_2011.ps1 -CMDLET 'Get-ChildItem -Path C:\Windows\System32 -Filter "*.dll"'
   
   Average time of 5 runs of 5 seconds is 150.44608 milliseconds.

   NOTE: The Single Quotes.
   
.EXAMPLE
   .\Beginner_10_2011.ps1 -Iterations 2 -CMDLET 'Get-ChildItem C:\Windows -Filter "*.txt" -Recurse'
   
   Average time of 2 runs of 5 seconds is 821.15165 milliseconds.

   NOTE: The Single Quotes.
#>

Param(
[int]$Iterations = 5,
[Parameter(Mandatory=$true)][string]$CMDLET
)

$MeasureObjects = @()

For($i = 1; $i –le $Iterations; $i++) {
     $ObjectToMeasure = (Measure-Command -Expression { (Invoke-Expression $CMDLET)}).TotalMilliseconds
     $MeasureObjects += $ObjectToMeasure
}

$MeasurementTaken = ($MeasureObjects | Measure-Object -Average).Average

Write-Host "Average time of $Iterations runs of $SleepFor seconds is $MeasurementTaken milliseconds." -ForegroundColor white -BackgroundColor red
Top

Comments:

4/22/2011 12:13 AM
nice script. One thing that I generally do not like is a script or function in which the user must adhere to certain submission requirements ... like single quote around cmdlet. It is better to accept the user input, and then format it as it should be. This can be a tricky thing to do, but it makes for a better user experience.
4/22/2011 5:56 PM
Thanks Ed. I wanted to do that. I just didn't know how.