while循环不断循环比较时间

时间:2016-08-22 00:18:15

标签: powershell

$time = Get-Date -DisplayHint Time
$addTime = Read-Host -Prompt "How many minutes until computer turns off?"

$addTimeConvert = [timespan]::FromMinutes($addTime)
$addTimeConvertOut = $addTimeConvert.ToString("hh\:mm\:ss")

$newTime = $time + $addTimeConvert


Write-Host $newTime
Write-Host $time

while ($newTime -ne $time){

   $time = Get-Date -DisplayHint Time



   }
Stop-Computer 

我想编写一个脚本来在一段时间后关闭我的电脑,但由于某种原因,即使在同一时间之后,while循环也会继续。

1 个答案:

答案 0 :(得分:2)

我可以为(可能的)原始问题提供替代解决方案,而不是回答您提出的问题吗?

$minutes = Read-Host -Prompt "How many minutes until computer turns off?"
Start-Sleep -Seconds (60 * $minutes)
Stop-Computer