Multiple If/Then Statements

by jf781 at 2012-10-23 14:30:16

I am working on creating function that accepts input from a CSV file and then creates an AD user based on the information in the CSV. I am trying to keep the fields in the CSV file to a minimum and have PS do as much work as possible. So far I have it updating all of the fields I’d like to correctly. It does 5 If ElseIf statements to determine a some fields based upon the user’s location.

I would like it to get the title from the CSV and then have PS use that information to put the user in the appropriate AD groups. I’m not sure how to do this without creating a large number of If ElseIf statements.

I’ve read some about creating another function for each Title but wanted to get some input before I went any further. Any help would be greatly appreciated.

by DonJ at 2012-10-23 14:38:12
Well, there are two ways.

One, if the CSV file contains the actual group name, then PowerShell can just specify that as the group to add to. So you wouldn’t need any logic.

But that doesn’t sound like the case for you. So look into the Switch construct instead. It’s like a bunch of ElseIf statements, but a bit tighter. help about_switch{/b] should get you there.
by jf781 at 2012-10-23 16:06:58
That looks like just what i needed. Thanks Don - And thanks again for the class last week, I’ve been amazed how much everything is clicking now.
by DonJ at 2012-10-23 16:09:39
You’re welcome! Yeah, the Switch construct is one of many we don’t cover much in class; read about_scripting for a longer list. Good luck!