Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Nov 2013
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Run script if start time in PowerShell

    Hello,

    I very new to powershell and i need help to achive this.

    At one stage of my poweshell script i want to define that if start time is 11:40 P.M. then hold this running script till 12:01 A.M. Suppose if its before 11:40 P.M. continue the script or if its past 12:01 A.M. then also continue, but if time is 11:40 hold on till 12:01A.M and then continue. Time / clock of local machine.

    Desperately needs help.
    Thanking in advance.

  2. #2
    Platinum Lounger
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    3,739
    Thanks
    7
    Thanked 242 Times in 230 Posts

  3. #3
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,407
    Thanks
    208
    Thanked 833 Times in 766 Posts
    Carmine,

    This code works in my tests.
    Code:
    $TrigerHr = 23
    $TriggerMin = 39
    
    $CurDT = Get-Date
    $CurHr = $CurDT.Hour
    $CurMin = $CurDT.Minute
    
    if ( $CurHr -eq $TrigerHr -and $CurMin -gt $TriggerMin ) { 
      Write-Host "Sleeping"
      #Note: Subtracting from 61 so you get 12:01 AM for restart!
      $Delay = (61 - $CurMin) * 60 #Calculates sleep based on current time.
      Start-Sleep -Seconds $Delay
    }
    
    #Continue here
    HTH
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  4. The Following User Says Thank You to RetiredGeek For This Useful Post:

    carmine (2014-03-26)

  5. #4
    New Lounger
    Join Date
    Nov 2013
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi,

    Thanks Paul T.
    Thanks R.G. Thanks a lot.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •