【问题标题】:If time between 8:00 AM to 5:30 PM如果时间在上午 8:00 到下午 5:30 之间
【发布时间】:2014-12-16 15:56:44
【问题描述】:

有人可以提一下我如何执行 sendmail 功能吗 如果时间在上午 8:00 到下午 5:30 之间,并且对于 其余时间不发邮件?

基本上类似这样的伪作品。

if(time between 8:00 AM and 5:30 PM)
{
    send mail
{
else
{
    do nothing
}

更多类似的

[int]$hour = get-date -format HH
If($hour -lt 8 -or $hour -gt 17)
{

【问题讨论】:

  • 我没有看到这个问题。你有你想要完成的基本想法。有没有看不懂的部分?你知道如何得到你的当前时间吗?是你不懂的邮件发送吗?请附上您的尝试以及您不了解的具体部分。
  • 我有代码,但它不起作用。
  • @abmv 你能在你的帖子中包含代码吗?
  • $hour -ge 8 -and $hour -le 17 应该在 8 到 5 之间工作

标签: powershell


【解决方案1】:

创建 2 个参考时间戳

$min = Get-Date '08:00'
$max = Get-Date '17:30'

并将他们的TimeOfDay 属性与您的日期的相应属性进行比较:

$now = Get-Date

if ($min.TimeOfDay -le $now.TimeOfDay -and $max.TimeOfDay -ge $now.TimeOfDay) {
  # send mail
}

【讨论】:

    【解决方案2】:

    我在谷歌上搜索,看看是否有人想出了比我更好的解决方案并来到这里。以下是我的解决方法:

    if (((get-date).TimeOfDay.TotalMinutes) -in 480..1050) {
        # some code
    }
    

    我希望它可以帮助别人,干杯! :)

    【讨论】:

    • 对代码的一些描述会很好。也使用变量来启动和停止
    【解决方案3】:

    我喜欢这个主意,但我发现您必须将 TotalMinutes 转换为整数才能在范围内找到它:

    if (([int](get-date).TimeOfDay.TotalMinutes) -in 480..1050) {
        # some code
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-23
      • 2022-01-20
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 2021-04-02
      相关资源
      最近更新 更多