我正在将脚本课作为我的IT学位的一部分而且我对我的PowerShell脚本感到难过。我必须使用Get-Date
来计算我的游戏播放时间,并创建一个日志文件,用于存储播放的游戏数量,以及播放的短片和最长的游戏。必须在脚本外部创建日志文件,但必须在脚本中更新。我把目前为止的代码放在下面。
$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
答案 0 :(得分:0)
要执行时间,请在之前获取之前的日期 之后,然后减去两个以获得表示时间的TimeSpan
它花了
$StartTime = Get-Date
# script goes here
$EndTime = Get-Date
$TimeTaken = $EndTime - $StartTime
Write-Host "A total of $($TimeTaken.TotalSeconds) has passed"
您也可以使用StopWatch
类来完成此任务:
$Watch = [System.Diagnostics.Stopwatch]::StartNew()
# script goes here
$Watch.Stop()
$TimeTaken = $Watch.Elapsed
Write-Host "A total of $($TimeTaken.TotalSeconds) has passed"