Summary: Introducing Event 1 in the Windows PowerShell Mini-Scripting Games 2014.
Hey, Scripting Guy! You know, the Scripting Games are tons of fun, and I always learn a lot. I wish you would post problems from time-to-time, instead of me having to wait for the official Scripting Games. You don’t have to have prizes, judging, or even guest commentators. I would just like the practice. Is it even possible to do something like that?
—EB
Hello EB,
Microsoft Scripting Guy, Ed Wilson, is here. Sure it is possible. In fact, I like that idea. So here goes…a Mini-Scripting Games Week! I will post two problems a day. For the PowerTip, I will post a short-answer question. It will be the type of thing that people run into all the time when working with Windows PowerShell. Instead of an actual Hey, Scripting Guy! Blog post, I will present a short problem for you to solve.
Next week, I will post the answers. You are on your own, but there is always Facebook, the Scripting Guys Forum, and even Twitter, so you can ask questions, brag about answers or whatever. You can even blog about your solutions.
I ask only one thing:
Please do not post any answers to these problems until next week. Please do not spoil the fun for your fellow scripters!
Now for the kick off...
Problem one description
You are reading a CSV file that was created from another source. The CSV file has four fields that are to be read and then used to update four attributes in Active Directory. The problem is that the CSV file does not have all four fields filled out. In some cases, there is a space in the field, in others a tab, others are null, and others are empty.
When a field does not have a proper value, the attempt to update the attribute in Active Directory results in an error message. Your boss has decided to make the script your problem.
Task
You need to create logic that will exclude all sorts of white space, null fields, uninitialized fields, or whatever. You should also create a CSV file and a simple harness script that encapsulates your logic so you can prove to your boss that the logic works. You do not need to write code that updates attributes in Active Directory—you only need to be able to run a sample to prove that your logic works.
You do not even need to read a CSV file if you do not want to do so. Your test harness can be as simple as this:
PS C:\> $a = $null
PS C:\> if (!$a) {'$a is null'}
$a is null
Hint: Remember the problem. There are all kinds of things that end up being null, empty, or uninitialized. Your logic must test for all of them. You may want to spend some time looking at some of my earlier Hey, Scripting Guy! Blog posts where I talked about strings.
Mini-Scripting Games Week will continue tomorrow when I will have another two problems.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy