none
Time of day script section question? RRS feed

  • Question

  • Hello folks,

    A powershell beginner here, editing a script and puzzled by this section where my Timeofday calculation resets at 60, instead of 0 and then stopping.

    $waittime=120#seconds

    $Time=(get-date).TimeofDay

    do{

    sleep1

    $timeleft=$waittime-($Newtime.seconds)

    Write-host"Waiting until $timeleftseconds"

    $Newtime=(Get-Date).TimeofDay -$Time

    } until(($Newtime).Seconds -ge$waittime)

    The output displays starting with 120 correctly, but resets at 60 for some reason instead of stopping at 0.

    Anyone have a suggestion on how to correct or a hint?  :)


    • Edited by romatlo32 Tuesday, January 27, 2015 11:00 PM
    Tuesday, January 27, 2015 10:57 PM

Answers

  • I understand that you want to wait 120 seconds to do something else.

    I'll code it a way more simpler, just like this:

    $waittime=120 #seconds
    
    for($i = $waittime; $i -ge 0; $i--){
        Start-Sleep -s 1
        Write-host "Waiting until $i seconds"
    }

    This will wait 121 seconds (120 to 0), if you want to wait just 120 seconds change the "ge" for "gt"


    • Edited by j0rt3g4 Wednesday, January 28, 2015 12:12 AM
    • Marked as answer by romatlo32 Thursday, January 29, 2015 9:16 PM
    Wednesday, January 28, 2015 12:08 AM

All replies

  • What is the purpose?

    If you need to sleep for 60 seconds, just use start-sleep -seconds 60.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, January 27, 2015 11:00 PM
    Moderator
  • Countdown timer.

    $endtime=[DateTime]::Now.AddSeconds(120)
    do{  
        $left=[math]::Truncate(($endtime - [datetime]::Now).TotalSeconds)
        Write-host "Waiting for $left"  -ForegroundColor green
        sleep 1 
    }until([DateTime]::Now -ge $endtime)


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, January 27, 2015 11:42 PM
    Tuesday, January 27, 2015 11:42 PM
  • Andnow for a better mousse trap:

    $endtime=[DateTime]::Now.AddSeconds(120)
    Write-Progress -SecondsRemaining 120 -Activity Countdown
    do{  
        $left=[math]::Truncate(($endtime - [datetime]::Now).TotalSeconds)
        Write-Progress -Activity Countdown -CurrentOperation 'Tick' -SecondsRemaining $left
        sleep 1 
    }until([DateTime]::Now -ge $endtime) 
    


    ¯\_(ツ)_/¯

    Tuesday, January 27, 2015 11:47 PM
  • I understand that you want to wait 120 seconds to do something else.

    I'll code it a way more simpler, just like this:

    $waittime=120 #seconds
    
    for($i = $waittime; $i -ge 0; $i--){
        Start-Sleep -s 1
        Write-host "Waiting until $i seconds"
    }

    This will wait 121 seconds (120 to 0), if you want to wait just 120 seconds change the "ge" for "gt"


    • Edited by j0rt3g4 Wednesday, January 28, 2015 12:12 AM
    • Marked as answer by romatlo32 Thursday, January 29, 2015 9:16 PM
    Wednesday, January 28, 2015 12:08 AM
  • Thank you folks for all the replies and examples.  Good stuff for me.

    This code is part of a greater script for VMware PowerCLI.  I was trying to break out this code and see how this particular section of the script worked.

    http://www.vhersey.com/2011/12/powercli-script-to-shutdown-my-home-lab/

    My purpose was to specifically understand why this code was not doing what I thought it should (after I tweaked it).  Just for my learning of Powershell. 

    Thanks again, I will review and test these suggestions.

    If no more comments, I will close this thread tomorrow.

    Wednesday, January 28, 2015 11:23 AM
  • Romatlo just try to keep it simple, if you want to troubleshoot your starter script you should open it in the "Windows powershell ISE".

    You can install it by running this:

    Import-Module ServerManager
    Add-WindowsFeature powershell-ISE


    After you have this, save the text of your script with extention ".ps1". and then just right click and select the "Edit" option.

    To use the powershell ISE.

    It's quite simple, to make a break and to remove them  use F9

    If you wanna do a litle step foward use F11

    If you wanna do a complete step foward use F10.

    To break script when you find a mistake use SHIFT+F5

    and to run it again, use F5


    • Edited by j0rt3g4 Friday, January 30, 2015 2:01 AM
    Friday, January 30, 2015 1:59 AM