Tuesday, August 10, 2010

Do a task in your sleep. :-P

Have you had to start a logtime running script at inhuman hours, and never really found a way to do this except through the task manager. I created this small script that will accept any input and pass it along the pipe. Just prep the command and start it and it will look like starting at the specified time.

Param (
    [Parameter(Position=0, ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]$itemlist,
    $ContinueAt="")

# ContinueAt format "YYYYMMDDHHmm"

BEgin {
    $now = get-date

    If ($ContinueAt -ne "") {
        $When = Get-date -year $ContinueAt.Substring(0,4) -Month $ContinueAt.SubString(4,2) -day $ContinueAt.Substring(6,2) -Hour $ContinueAt.Substring(8,2) -Minute $ContinueAt.Substring(10,2)

        while ($now -lt $when) {
            Write-Verbose "Sleeping until: $ContinueAt"
            Sleep 10
            $now = get-date
        }
    }
    Write-verbose "Done Sleeping"
}
PROCESS{
    Foreach ($item in $itemlist) {
        $item
    }
}
END {
}  

Hope this helps someone out there.