Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 使用Get-Date cmdlet_Powershell - Fatal编程技术网

Powershell 使用Get-Date cmdlet

Powershell 使用Get-Date cmdlet,powershell,Powershell,作为我的IT学位的一部分,我正在学习一门脚本课程,我的powershell脚本让我难堪。我必须使用Get Date来确定我的游戏玩了多长时间,并创建一个日志文件来存储玩过的游戏数量,以及玩过的短游戏和最长游戏。日志文件必须在脚本外部创建,但必须在脚本内部更新。我把我目前掌握的代码放在下面 $rand = new-object system.random $number= $rand.next(0,11) clear-host do{ $a = read-host -prompt "enter

作为我的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

要计算执行时间,请获取完成工作之前和之后的日期,然后减去这两个日期,得到表示所用时间的
TimeSpan

$StartTime = Get-Date
# script goes here
$EndTime   = Get-Date
$TimeTaken = $EndTime - $StartTime
Write-Host "A total of $($TimeTaken.TotalSeconds) has passed"
您还可以使用
秒表
类来完成以下操作:

$Watch = [System.Diagnostics.Stopwatch]::StartNew()
# script goes here
$Watch.Stop()
$TimeTaken = $Watch.Elapsed
Write-Host "A total of $($TimeTaken.TotalSeconds) has passed"

你有问题吗?你的问题是什么?所以这不是一个家庭作业帮助网站。我知道这不是为了获得家庭作业帮助。我的问题是:如何使用GetDate作为计时器来计算游戏的长度。是否只是get-date.timeofday-get-date.timeofday?有关一些想法,请参见此问题: