Average Rating:

5.00 by 3 users.

55 Downloads, 257 Views

1 Comment(s)

Beginner10.ps1

By: Sadie Trest 22 Apr 2011 04:34 AM UTC in the category: Beginner Event 10

Description:

This script creates a reusable function that executes either the default command or a user supplied command a specified number of times, averages the amount of time each command took to run and returns a string containing the result in milliseconds.
# -----------------------------------------------------------------------------
# Script: Beginner10.ps1
# Author: Sadie Trest
# Date:   April 21, 2011
# Keywords: 2011 Scripting Games, Beginner, Event 10
# comments: This script creates a function to reuseable to execute a command
#			a specified number of times, averages the amount of time each 
#			command took to run and returns the value in milliseconds.
# -----------------------------------------------------------------------------	
 <#
  .Synopsis
	This script creates function that executes a command a specified number
	of times, averages the amount of time each command took to run and returns
	the value in milliseconds.
	
  .Description
	This script creates a reusable function that executes either the default 
	command or a user supplied command a specified number of times, averages
	the amount of time each command	took to run and returns a string 
	containing the result in milliseconds.
	
  .Parameter Repeats
	Number of times to execute command.  Example: 5
	Default: 5
	
  .Parameter Scriptblock
	A command to pass to a scriptblock 	Example: "get-process | select-object name"
	Default: "Start-sleep -Seconds 5"
	
  .Example
	Get-EggTimer
	
	Description
    ------------
	Executes the default command "Start-Sleep -Seconds 5" five times.
	
	Sample output:  Average time of 5 runs of 5 seconds is 5006.01448 milliseconds
	
  .Example
	Get-EggTimer -Repeat 3
	or
	Get-EggTimer -r 3
	
	Description
    ------------
	Executes the default command "Start-Sleep -Seconds 5" three times.
	
	Sample output:  Average time of 3 runs of 5 seconds is 5006.29931 milliseconds
	
  .Example
	Get-EggTimer -Scriptblock "get-process | select-object name"
	or
	Get-EggTimer -S "get-process | select-object name"	
	
	Description
    ------------
	Executes the "get-process | select-object name" the default number of times (five).
	
	Sample output:  Average time of 5 runs is is 19.45304 milliseconds
	
  .Example
	Get-EggTimer -Scriptblock "get-process | select-object name" -Repeat 22
	or
	Get-EggTimer -s "get-process | select-object name" -R 22
	
	Description
    ------------
	Executes the "get-process | select-object name" 22 times.
	
	Sample-output:  Average time of 22 runs is 15.15595 milliseconds
 #>
	
#Get-TotalCommandTime may have been a better name... but I liked Get-EggTimer
Function Get-EggTimer {
   [CmdletBinding()]
    Param
    (
        [parameter(Mandatory=$false,ValueFromPipeline=$false)]
        [Alias("R")]
        [int]$Repeat=5, 	 #set default number of repetitions
		
		[parameter(Position=1,Mandatory=$false,ValueFromPipeline=$false)]
        [Alias("S")]
        [string]$Scriptblock  
    )
	Process 
	{
		#check to see if a scriptblock string was supplied
		if ($Scriptblock -eq "") 
		{
			#set default script block as a string
			$Scriptblock = "Start-Sleep -Seconds 5"
			$default = $true
		}
		else
		{
			$default = $false
		}
		#Convert the scriptblock and attempt to execute command the specified number or times
		Try
		{		
			#convert scriptblock string to scriptblock type
			$ScriptblockCommand = [scriptblock]::Create($Scriptblock)

			#use range operator to loop through the specified number of iterations 
			$TotalCommandTime = 1..$Repeat | foreach-object {measure-command -Expression $ScriptblockCommand}
			
			#get the average command processing time in milliseconds 
			$AverageCommandTime = ($TotalCommandTime | measure-object -Average TotalMilliseconds).Average
		
			#check to see if the default command was used
			if ($default)
			{
				#use this format if the default command was used
				$EggTimer = "Average time of $Repeat runs of 5 seconds is $("{0:F5}" -f $AverageCommandTime) milliseconds"
			}
			else
			{	
				#otherwise use this format 
				$EggTimer = "Average time of $Repeat runs is $("{0:F5}" -f $AverageCommandTime) milliseconds"
			}
		}
		Catch
		{
			#if any part of running the command or generating the results fail,
			#set the function value to $error
			$EggTimer = $Error
		}
		 
		#return the function value
		Return $EggTimer
		
	}
}
Top

Comments:

4/22/2011 4:46 AM
very nice script. I like your error handling, and use of an approved verb for your function name. Very nice comment based help. In a script like this, it is especially important to use something like write-progress to let the user know something is going on.