The Surly Admin

Father, husband, IT Pro, cancer survivor

SQL Queries in PowerShell

There are a whole bunch of blog posts and scripts out there to do SQL queries in PowerShell, so I’ve really hesitated about posting my own version of it.  But hey, it’s my blog and I can do what I want to.  Do what I want to.

Continue reading

December 1, 2014 Posted by | PowerShell | , , | 4 Comments

Getting Directory Information Fast

By now, you may have noticed I’m always on the lookout for better performing code.  This has turned out to be a good habit now that I’m working at athena health, as the pure scale of things is so much larger than places I’ve been at in the past.  One piece I’ve never been able to speed up, though, is iterating through folders and files.   Nicolas1847, a PowerShell scripter on Spiceworks, has come up with an ingenious method to get simple directory information using Robocopy (of all things), and a colleague at athena health likes to shell out to CMD.exe and use the old DIR command.  But are they faster?  And if so, which one?

Continue reading

August 4, 2014 Posted by | Powershell - Performance | , , , | 2 Comments

Quick Script: Date Ranges

Ever need to get an array of all the days between two dates?  This script will calculate the date range and load up an array with date/time objects for every date in between the entered times:

To use it, simply call the function

  • Get-DateRange
  • Get-DateRange -Start 7/24/14 -End 8/1/14
  • Get-DateRange -End 7/20/14

And just to pretty it up as a Function and with comment-based help:

July 25, 2014 Posted by | PowerShell | | Leave a comment

Exporting User Information

Need to export user information to a CSV?  Perhaps as part of your termination user process?  Incredibly easy with PowerShell, let’s see how to do it.

Continue reading

July 14, 2014 Posted by | Powershell - Getting Started | , , , , | Leave a 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 | , , | 34 Comments

Monitoring the Progress of a PowerShell Job

Working in a big Enterprise is a whole different animal than small business (which has been my space for a long time).  I’m finding myself doing a lot more multi-threading because of the pure scale of things that need to be done and running a single threaded sequence just takes too long.  But that doesn’t mean I don’t want to give my users feedback about the progress of my scripts, in fact, the larger the work load the more important feedback becomes.  I recently discovered a technique that allows me to do just that–but not before putting my own spin on it.

Continue reading

June 23, 2014 Posted by | PowerShell | , , , | 5 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

Simple Multiplication Table

My daughter has really been working on her multiplication tables this year and normally she comes home with a study sheet for each number, all the way up to 12.  This week we’re working on 8’s, so 8×0, 8×1, all the way up to 8×12.  Unfortunately she didn’t get a worksheet so she decided to make her own.  If I can take a moment of fatherly pride, she does this kind of thing all the time without us making her.  What a gal!  Well, one of the ways I motivate her is to race her.  So it was on.  Can I write a PowerShell script and get the multiplication table out before she could write it out?!

Continue reading

April 4, 2014 Posted by | Powershell - Getting Started | , , , , | 1 Comment

New Scripts – Bandwidth Report and Google Charts

Got a couple of new scripts I recently wrote, here’s some quick thoughts on them.

Continue reading

April 2, 2014 Posted by | PowerShell - HTML Reporting | , , , , , | 4 Comments

WhoIS Report

Really fascinating project came through Spiceworks the other day and I wanted to talk about it.   In the DNS forum someone posted that they needed a tool to lookup certain WhoIs information.  The interesting part was they had over 1000 domains so they needed something that could create a report on that kind of scale, so a simple WhoIs website or application wasn’t going to cut it.  But as I looked at it, I began to wonder if this was something I could pull off with PowerShell?  I knew there would be no problem once I had the data, but the trick would be getting the data.  But surely there was a RESTful API out there for WhoIs data, right?  If I could find that then a simple Invoke-RestMethod should retrieve the data necessary and we’re good to go.  Right?

Continue reading

March 7, 2014 Posted by | PowerShell | , , , | 1 Comment