powershell working with time objects

I have a perpetually running powershell script which is always looking at a text file to see if an install is scheduled to run within the next 2 minutes. This text file just contains a list of times when installs should run (or nothing). I want this install to run every night at 12:10 am. To do this, I need to make a list of the next 100 days’ worth of 12:10am entries.

$basetime = get-date “11/15/2007 12:10 AM”
[array]$times = @()
for($a=0;$a -le 100;$a++){ $times += “$($basetime.AddDays($a).ToString()) both” }
$times
11/15/2007 12:10:00 AM both
11/16/2007 12:10:00 AM both
11/17/2007 12:10:00 AM both…

This gives me a list of 100 strings that can be read into get-date as a time/date object!

$blah = get-date $times[3].replace(” both”,””)

Why the hell is that “both” part in there? Well, that’s something just for me, which describes the install that is occurring. When evaluating schedule entries, I replace those off and trim the string down. Why do I want to read this into get-date again? So I can do better compares!

$objScheduleTime = Get-Date $blah
if ($objScheduleTime.GetTypeCode() -ne “DateTime”)
   { “timedate is invalid” }
else
   {
      $TimeDifference = $objScheduleTime – (Get-Date)
      if ($TimeDifference -lt 0)
         { “time is in the past” }
      else
         { “time is in the future” }
   }

First, convert $blah into a date-time object, then check the type code to make sure it converted correctly. Incorrect conversions need to be handled and not continue as a null object, or the rest of the script will complain. As usual, there are plenty of ways to do this, but this makes sense to me.