Average Rating:

4.00 by 2 users.

50 Downloads, 193 Views

1 Comment(s)


By: Matt_G 22 Apr 2011 02:17 AM UTC in the category: Beginner Event 10


Test the average time it takes to execute a command
# ---------------------------------------------------------------------------------------------
#	Script Name:		Test-AvgCmdTime
#	File Name:			Test-AvgCmdTime.psm1
#	Author(s):			Matt Gohmann
#						Powershell Novice
#	Date Created:		4/21/11
#	Date Last Updated:	4/21/11
#	Requirements:		PowerShell v2
#	Instructions:		Import the module, execute commands.
#	Notes:				2011 Scripting Games 
#						Beginner Event 10
# ---------------------------------------------------------------------------------------------

function Test-AvgCmdTime
	Used to test the average time it takes to execute a command. 
	Used to test the average time it takes to execute a command, well script block to be exact.
	By default it tests the average time it takes to sleep 5 seconds for cycles.
	Test-AvgCmdTime -ScriptBlock {test-connection localhost -count 3} -Cycles 20
	In this example the test-connection command is being tested for 20 cycles.
	[Parameter(Mandatory=$false, HelpMessage="Enter a scriptblock. Ex: {ping yahoo.com}", ValueFromPipeline=$true)][scriptblock]$ScriptBlock={Start-Sleep 5},
	[Parameter(Mandatory=$false, HelpMessage="How many times do you want to run the scriptblock?", ValueFromPipeline=$false)][INT]$Cycles=5
	for($i=0;$i -lt $Cycles;$i++)
		Write-Progress -activity "Please wait while your test is run" -status "Progress: " -PercentComplete (($i / $Cycles)  * 100)
		$total+=((Measure-Command -Expression $ScriptBlock) | Select-Object TotalMilliseconds).TotalMilliseconds
	$Result = $total/$Cycles
	Write-host "Average time of $Cycles cycles is $Result milliseconds."


4/23/2011 10:25 PM
Nice job on creating it as a function with a default value for the Script Block. The Comment based help is good, maybe (For fun, nothing to do with the Scripting Games) Add in some more examples.