Average Rating:

3.00 by 2 users.

53 Downloads, 182 Views

1 Comment(s)

Beginner10_2011.ps1

By: Neil Clinch 21 Apr 2011 08:01 PM UTC in the category: Beginner Event 10

Description:

Measure the average accuracy of the Start-Sleep command by performing a number of runs for a
specified duration of seconds. Then display the average amount of time in milliseconds.
#############################
# Beginner10_2011.ps1		#
# PowerShell Version 2.0	#
# Modified on: 4/19/2011	#
# Author: Neil Clinch		#
#############################

<#
.SYNOPSIS
Measure the average accuracy of the Start-Sleep command by performing a number of runs for a
specified duration of seconds. Then display the average amount of time in milliseconds.

.DESCRIPTION
Measure the average accuracy of the Start-Sleep command by performing a number of runs for a
specified duration of seconds. Then display the average amount of time in milliseconds.

.PARAMETER Runs
An integer value representing the number of measurement runs to perform. 
Defaults to 5 runs if not specified 

.PARAMETER Duration
An Integer value representing the duration in seconds of each messurement
Defaults to 5 seconds if not specified

.EXAMPLE
PS>.\Beginner10_2011.ps1 -Runs 7 -Duration 5
Average time of 7 runs of 5 seconds is 5005.37266 milliseconds

Progress bar will display progress.
.EXAMPLE
PS>.\Beginner10_2011.ps1
Average time of 5 runs of 5 seconds is 5005.37266 milliseconds

Default values of 5 will be used for Runs and Duration 
Progress bar will display progress.

.LINK
The 2011 Scripting Games Beginner Event 10:
http://blogs.technet.com/b/heyscriptingguy/archive/2011/04/15/the-2011-scripting-games-beginner-event-10-use-powershell-to-measure-time-to-complete-a-command.aspx

.INPUTS
Runs - An Integer value representing the duration in seconds of each messurement Defaults to 5 seconds if not specified
Duration - An Integer value representing the duration in seconds of each messurement Defaults to 5 seconds if not specified
#>
param (
  [Parameter(Mandatory = $false)]
  [int]$Runs = 5, # number of measurement runs
  
  [Parameter(Mandatory = $false)]
  [int]$Duration = 5 # measurement duration
)
# set the progress bar's Activity message
$Activity = "Measuring Accuracy of Start-Sleep cmdlet for " + $runs + " runs of " + $duration + " Seconds"
# Initialize the TotalTime to 0 This is used to sum up the runs Total Time used
$TotalTime = 0

#Perform measurement $Runs number of times for $Duration seconds
#Display a Progress bar to let the user know the command is running
for ($Run = 1; $Run -le $Runs; $Run++) {
  Write-Progress -Activity $Activity -Status "Run #$Run" -percentcomplete (100*$Run/$Runs)
  $TotalTime += (Measure-Command {Start-Sleep -Seconds $Duration}).TotalMilliseconds
} 
#Write out results
Write-Host "Average time of" $runs "runs of" $Duration "seconds is" ($TotalTime/$Runs) "milliseconds"
Top

Comments:

4/23/2011 11:10 PM
Pretty adaptable. Try the INVOKE-EXPRESSION to pass Cmdlets directly via a Variable :)