Simple Reminder Script doesn't work

时间:2016-04-15 11:14:08

标签: powershell scripting

Since, I am little forgetful, I am making a simple Powershell script, which will remind me of certain specified things at given time. Below is my code, which goes into infinite loops.

$whsehll = New-Object -ComObject Wscript.shell
$Reminder = Read-Host "What time do you want a reminder of?"
$Subject = Read-Host "What do you want me to remind you?"
$Time = Get-Date -Format hh:mm
do {
   Start-Sleep -Seconds 1
   }while (($Reminder -ge $Time)) 
   $whsehll.Popup("Hello, I am here to remind you of $Subject")

I have done all wayouts, but I am unable to work it out.

P.S. I ran a Get-date command first on powershell, and Got the time pattern as 03:00 instead 15:00hrs, so I assume that userinput should also be in 12 hrs clock pattern?

P.P.S. I did try other work-arounds like toggling with comparing options -gt -ge -lt -le but none of it works. Please help.

2 个答案:

答案 0 :(得分:0)

更改以下内容

$Time = Get-Date -Format hh:mm
do {
   Start-Sleep -Seconds 1

do {
   Start-Sleep -Seconds 1
   $Time = Get-Date -Format hh:mm

它会起作用。

您应该在每个循环中取当前时间,而不是在进入循环时与初始时间进行比较。 或者使用HH:mm作为24小时格式,如AgentK所述。

注意,如果$ reminder ist 02:34,它会在02:35通知你,因为-ge代替 - gt(或使用时为14:34/35 HH:mm格式)。

对我而言,使用while循环更容易阅读/理解:

$wshshell = New-Object -ComObject Wscript.shell
$Reminder = Read-Host "What time do you want a reminder of?"
$Subject = Read-Host "What do you want me to remind you?"
while ($Reminder -gt (Get-Date -Format HH:mm)) 
{
   Start-Sleep -Seconds 1
}
$wshshell.Popup("Hello, I am here to remind you of $Subject")

答案 1 :(得分:-2)

稍微修正了你的脚本,现在它似乎正在运行:

$whsehll = New-Object -ComObject Wscript.shell
[datetime]$Reminder = Read-Host "What time do you want a reminder of?"
$Subject = Read-Host "What do you want me to remind you?"
$Time = Get-Date
do {
   Start-Sleep -Seconds 1
   }
while ($Reminder -ge $Time)
$whsehll.Popup("Hello, I am here to remind you of $Subject")

PS。如果您使用24H格式时间,则需要使用大写字母H,例如 HH:mm