使用Get-Date cmdlet

时间:2015-10-22 14:10:15

标签: powershell

我正在将脚本课作为我的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

1 个答案:

答案 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"