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 Get-TimeSlot($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 } Get-TimeSlot -startTime 20 -EndTime 4

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

Comments (3):

  1. Diandra says:

    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 It is an excellent resource and a thriving and gifted community.Peace, Paul

  3. http://www./ says:

    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 Reply