简体   繁体   中英

While loop continuously looping comparing times

$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 

I want to write a script to turn off my computer after a set amount of time but for some reason the while loop continues even after they are the same time.

Rather than answer your asked question, can I offer an alternative solution to the (probable) original problem?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM