-4

I am taking a scripting class as part of my IT degree and I am stumped on my powershell script. I have to use Get-Date to time how long my game is played, and also create a log file that stores number of games played, and the shorted and longest games played. The log file must be created outside the script, but must update within the script. I put the code I have so far below.

$rand = new-object system.random
$number= $rand.next(0,11)
clear-host

do{ $a = read-host -prompt "enter number between 1 and 10"
if ($a -gt $Number) {Write-Host "number is too high"}
elseif ($a -lt $Number) {Write-Host "number is too low"}
elseif ($a -eq $Number) {Write-Host "You did it!! It took you $x tries!"}
else {"You have to guess a number!!!"}
$x = $x + 1
 } while ($a -ne $number)
$path = C:\temp\logfile.log.txt
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206

1 Answers1

0

To time execution, grab the date before and after you've done your work, and then subtract the two to get a TimeSpan representing the time it took

$StartTime = Get-Date
# script goes here
$EndTime   = Get-Date
$TimeTaken = $EndTime - $StartTime
Write-Host "A total of $($TimeTaken.TotalSeconds) has passed"

You could also use the StopWatch class to accomplish this:

$Watch = [System.Diagnostics.Stopwatch]::StartNew()
# script goes here
$Watch.Stop()
$TimeTaken = $Watch.Elapsed
Write-Host "A total of $($TimeTaken.TotalSeconds) has passed"
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206