Generate Random Timeslot for scheduled tasks

I was tasked to create a PS script that created a Windows Server Backup policy with a random start time

I came up with the following script for the random start time bits.

function GetTimeSlot($startTime, $EndTime) { $timeslots = @() for ($i = $startTime; $i -ne $EndTime; $i++) { if ($i -eq 24) { $i = 0 } $timeslots += $i } $timeslot = $(Get-Random $timeslots):00 return $timeslot } GetTimeSlot startTime 20 EndTime 4

There might be a more Powershell’ish way of doing this, but it gets the job done.

By | 2017-08-22T10:37:09+00:00 September 10th, 2014|Powershell|3 Comments

About the Author:

Ronnie Jakobsen

Twitter: @RonnieJakobsen

3 Comments

  1. Diandra July 18, 2016 at 6:06 - Reply

    It’s great to read something that’s both enjoyable and provides prsaagtimdc solutions.

  2. Hi Katie: I’ve emailed my friend Daviea, who I believe mentioned that she’d found a good one.I might also recommend checking out my friend Elisa’s weblog at channelingErik.com. It is an excellent resource and a thriving and gifted community.Peace, Paul

  3. http://www./ November 26, 2016 at 14:23 - Reply

    Jag sitter och gråter ögonen ur mig, mitt på jobbet. Så fin historia, så full av kärlek. Så orättvist. Så ofantligt sorgligt. Johanna, som jag inte kände, men som jag följt genom Ung Cancers arbete. Tack för att du var med och gjorde skillnad. Vila i frid. <3

Leave A Comment