The Surly Admin

Father, husband, IT Pro, cancer survivor

Calculating the Quarter

Quick post since it’s been awhile since I’ve done anything.  I’ve mentioned before but I do a lot of reports, and I recently got one to do some calculations for 95th percentile–well, kind of.  The funny thing was the person who asked me was supposed to be making a solution themselves, kind of homework from the boss but they didn’t mention it to me and I thought it would be a fun challenge so I went and wrote up a script that could do it, parameters and everything.   Oops!  Anyway, when done they loved the report so I streamlined it from taking 10 minutes to run to about 1 or 2 (all in SQL, no PowerShell) and thought I was done with it.  Then came the ask, not only do they want that data for the month, they want it for the whole quarter!  So now I had to figure out which quarter I was in and what the date range for that would be.  Yeah, I could put it in by hand, but there’s no challenge in that.

First came a Google to see if someone could do it in a one liner, and sure enough Vinoth over at StackOverflow had some great maths for that.  Now calculate the dates and the easiest way was to simply create a little loop 1 through 4 and calculate my dates:


#Define Quarters
$Quarters = @{}
$Start = Get-Date Month 1 Date 1 Year (Get-Date).Year
ForEach ($Q in (1..4))
{
$Quarters.Add($Q,[PSCustomObject]@{
From = $Start
To = $Start.AddMonths(3).AddTicks(-1)
})
$Start = $Start.AddMonths(3)
}
#Define this quarter
$Quarter = [int]([math]::Ceiling((Get-Date Format MM)/3))

view raw

Get-Quarter.ps1

hosted with ❤ by GitHub

First use Get-Date to get the January 1st, then loop 4 times adding 3 months each time to get the date ranges.  Now to get a date range for the current quarter?  $Quarters[$Quarter].

 

Advertisement

November 15, 2016 Posted by | PowerShell | , | 1 Comment

Getting Last Logon Information With PowerShell

Recently I had to write a report that got the last logon date for all of our users and I really ran into the LastLogonDate problem.  What problem is that, you might ask?  Well, it’s been documented a lot but the root of the problem is when a user logs into a domain account, their login time is recorded into the lastLogon field in Active Directory on the domain controller they authenticated against.  This field is stored as FileTime, which is the date and time as a 64-bit value in little-endian order representing the number of 100-nanosecond intervals elapsed since January 1, 1601 (UTC).  And just to make it a little bit more fun Active Directory does not replicate it.  So if someone logs in in India, and you query your Active Directory Domain Controller here in Massachusetts you will NOT get the updated information.  So now what?

Continue reading

June 30, 2014 Posted by | PowerShell | , , | 35 Comments

Additional Thoughts on Hashtables

I recently starting looking at the subreddit for PowerShell, and I’ve seen a lot of people using hashtables for just about everything.  I’ve even seen a few people using them over at PowerShell.com and in both cases the usage was dubious at best.  Here are my thoughts on it, and why hashtables are usually not the right choice.

Continue reading

April 8, 2014 Posted by | Powershell - Getting Started | , | 6 Comments

Objects and Hashtables

I expect, if you got the right group of people together, you could have a good old Mac vs PC style argument over the use of Powershell Objects (PSObjects) and hashtables.  And I’ll be honest, while I’ve used hashtables a lot for splatting I’ve used them very little for anything else.  Time to look at the two and figure out which is better, once and for all!

Continue reading

February 21, 2013 Posted by | Powershell - Best Practices, Powershell - Performance | , , , | 4 Comments