Average Rating:

2.50 by 2 users.

45 Downloads, 183 Views

1 Comment(s)

BE_10.PS1

By: Ken 21 Apr 2011 08:13 PM UTC in the category: Beginner Event 10

Description:

Use PowerShell to Collect Basic Computer Information
#Date: 4/10/2011
#2011 Scripting Games, Beginner Event 5
#Use PowerShell to Collect Basic Computer Information
#Author: KMcFerron

#Your boss is working on a script that needs to halt execution for five seconds and 
#then check a specific value. In the course of writing the script, your boss became 
#concerned about the accuracy of using the Start-Sleep Windows PowerShell cmdlet to 
#halt execution for five seconds. The script is not going to be used to control a 
#space probe to mars, but it should be more accurate than a traditional egg timer. 
#To be assured of the accuracy of the Start-Sleep command, your boss asks that you 
#measure the time the Start-Sleep command takes to pause for five seconds. 
#To check for variation, your boss wants you to take five measurements and provide 
#the average time in milliseconds. A satisfactory output could be as simple as seen 
#here.

#Average time of 5 runs of 5 seconds is 5007.84288 milliseconds

#    * Extra points for the use of variables to allow for other scenarios than the one in this event
#    * Extra points for clean code
#    * Extra points for formatting output

Function Get-AVERAGESLEEPTIME {
<#
.SYNOPSIS
GETS AVERAGE OF THE START-SLEEP COMMAND
.DESCRIPTION
GETS AVERAGE OF THE START-SLEEP COMMAND
.NOTES
.PARAMETER COUNT
NUMBER OF TIMES TO RUN THE START-SLEEP COMMAND
.PARAMETER SECONDS
TIME IN SECONDS TO RUN THE START-SLEEP COMMAND
.PARAMETER ONLYAVERAGE
ONLY RETURNS THE AVERAGE TIME
.EXAMPLE
GET-AVERAGESLEEPTIME -COUNT 5 -SECONDS 5
.EXAMPLE
GET-AVERAGESLEEPTIME -COUNT 5 -SECONDS 5 -ONLYAVERAGE $TRUE
#>
PARAM(
[INT]$COUNT,
[INT]$SECONDS,
$ONLYAVERAGE
)
$Results = @()
1..$($COUNT) | 
foreach-Object{
	$Results += (Measure-Command {Start-sleep -s $SECONDS}).TotalSeconds
}
[decimal]$Total = 0
$Results | ForEach-Object{
$Total = $Total + $_
}
$Av = $total / $($results.length)
	IF ($ONLYAVERAGE -eq $TRUE){
		$Av
	}
	ELSE{
		"Average time of $COUNT runs of $SECONDS seconds is  $AV milliseconds"
	}
}

Get-AVERAGESLEEPTIME -COUNT 5 -Seconds 5
Top

Comments:

4/23/2011 11:09 PM
Here's something to play with to tweak it. INVOKE-EXPRESSION :)