简体   繁体   English

在 PowerShell 中修改日期变量

[英]Modifying a Date variable in PowerShell

I'm looking to create a schedule type check.我正在寻找创建时间表类型检查。

I want to check today's date and if it's not the correct Day of the week for a task to run it continues.我想检查今天的日期,如果它不是运行任务的正确星期几,它会继续。

Once the day is correct, I will then create a timer based on the difference between the expected execution time (let's say 01:00) and now.一旦日期正确,我将根据预期执行时间(假设是 01:00)和现在之间的差异创建一个计时器。 I have had a few ideas on how to achieve this, one of them being below.我有一些关于如何实现这一目标的想法,其中之一如下。 The problem I am facing is I am unsure of how to create a CountDown timer from the NEW-TIMESPAN output and how to dynamically change the $EndDate time value to meet the relevant schedule value.我面临的问题是我不确定如何从NEW-TIMESPAN输出创建倒计时计时器,以及如何动态更改$EndDate时间值以满足相关计划值。 Any pointers would be appreciated.任何指针将不胜感激。

$date = Get-Date -DisplayHint Date
if ($date.DayOfWeek -eq 'Friday')
{
    $StartDate = (GET-DATE)
     # I am unsure of how to dynamically change the $EndDate variable time value and create the 'CountDown variable' from the New-TimSpan output.
    $EndDate = [datetime]"11/13/2020 01:00" 
    $countdown = NEW-TIMESPAN -Start $StartDate -End $EndDate
}
Write-Host $date

EDIT编辑

With some help from @AdminOfTHings I have come up with the following solution.在@AdminOfTHings 的帮助下,我提出了以下解决方案。

if ($date.DayOfWeek -eq $schedule.Day)
{
    $StartDate = (GET-DATE)
    $tempDate = (GET-DATE).ToString()
    $tempDate, $tempTime = $tempDate.Split(' ')
    [datetime]$DateTime = $tempDate + " $($schedule.Time)"
    $Timer = NEW-TIMESPAN -End $DateTime
    $CountDown = $Timer.TotalSeconds
    while ($CountDown -gt 0)
    {
        sleep 1
        $CountDown--
    }
    else
    {
        #START SERVICE
    }
}

New-Timespan creates a TimeSpan object regardless of the parameter set being used.无论使用何种参数集, New-Timespan都会创建一个TimeSpan对象。 If you know how long a timespan should be, then you can statically create that combination of days, hours, minutes, and seconds, making end time irrelevant.如果您知道时间跨度应该是多长,那么您可以静态地创建天、小时、分钟和秒的组合,使结束时间变得无关紧要。

$date = Get-Date -DisplayHint Date
if ($date.DayOfWeek -eq 'Friday') {
    $countdown = New-Timespan -Hours 8
}

You could have a situation where you know you want the task to span 2 days but want it to end at 01:00 .您可能会遇到这样一种情况:您知道希望任务持续 2 天,但希望它在01:00结束。 This would make the total time variable.这将使总时间可变。 You can make adjustments based on start time:您可以根据开始时间进行调整:

$date = Get-Date -DisplayHint Date
if ($date.DayOfWeek -eq 'Friday') {
    $end = Get-Date -Day $date.AddDays(2).Day -Month $date.AddDays(2).Month -Hour 1 -Minute 0 -Second 0
    $countdown = New-TimeSpan -End $end
}

Edit:编辑:

If you reach a target day of week and want to create a timer that lasts until the next 01:00 hour from that point in time, you can do the following:如果您到达一周中的目标日期并希望创建一个持续到该时间点后的下一个01:00的计时器,您可以执行以下操作:

$date = Get-Date
if ($date.DayOfWeek -eq 'Friday') {
    if ($date.Hour -lt 1) {
        # dynamic end date - start date
        $countdown = (Get-Date $date -Hour 1 -Minute 0 -Second 0) - $date
    } else {
        # dynamic end date - start date
        $countdown = (Get-Date $date.AddDays(1) -Hour 1 -Minute 0 -Second 0) - $date
    }
}
$countdown.TotalSeconds # total seconds of the time span

As an aside, if you expect the starting time to be now, you can skip the -Start parameter. -Start ,如果您希望现在开始时间,则可以跳过-Start参数。 Leaving off -Start assumes the starting time is now.离开-Start假设开始时间是现在。

Thanks for the help, I have no idea why I didn't think of it before but I have come up with感谢您的帮助,我不知道为什么我之前没有想到,但我想出了

if ($date.DayOfWeek -eq 'Friday') 

{   
     $StartDate = (GET-DATE)
     $tempDate = (GET-DATE).ToString() 
     $tempDate, $tmpTime = $tempDate.Split(' ')
     datetime]$DateTime = $tempDate + " $($schedule.Time)"
     $Timer = NEW-TIMESPAN -End $DateTime 
}

Just need to understand how to use the timer now (all in seconds)现在只需要了解如何使用计时器(全部以秒为单位)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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