Thursday, March 15, 2012

Arrays and Ranges

Not an overly insightful item, but I thought it worth sharing.

One of the guys here was working on managing Virtual Machine lifespans for Engineering and QA.

One of the concerns was that people not keep a single VM around for any real length of time.  So he was calculating the age of the VM in days.  As each VM hit a specific age an email will be generated to remind them of the short life policy.

He was  calculating an [integer] age from a time stamp then using
[integer]Get-Age($VM) -Match "90|120|180" 
to ID for an email.  I pointed out that casting to a string for the Regex was inefficient and that after 180 days people would never be reminded again.  Now the batch will run daily, so we don't need to worry about weekend gaps.  I realize the inefficiency of casting from integer to string is not really measurable for a small number of objects, but.....

Off my head I suggested using a range for the dates
(90,120,180) -contains [integer]Get-Age($VM) 

Then a thought and one or two "Not the right way to do it" later, I had:
((90,120) + (180..365)) -Contains [integer]Get-Age($VM) 

That way they get a daily email for a year, once they breach 6 months.