Kicking off a task without needing to setup a scheduled job

Posted: April 27, 2012 in Scripts

Quite often I want to have a few lines of Powershell at say 6pm. Rather than setting up a scheduled job, either via Windows or the Powershell scheduler, I setup this quick little code block. Just enter in a 24hr time and it will work out how many seconds until that time and put in a sleep. This might not be the neatest way of doing things, but it’s quick an easy!

$StartTime = Read-Host -Prompt “Enter time in 24 hour format, eg. 18:00:00”

$TimeDelay = [DateTime]$StartTime – (Get-Date)
$TimeDelayInt = $TimeDelay.TotalSeconds -as [int]

If ($TimeDelayInt -gt 0){
Start-Sleep -Seconds $TimeDelayInt
Write-Host “Replace this with some sort of code you want to run…”
}
Else{
Write-Host “The time you entered has already passed. Try again.”
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s