Switch and IF, can it be done smarter?

Hi.

I am working on a AD script to create a user, i am at a point where i am addding exchange groups. Below is just an example of the challenge. I got multiple countries, and based on different factors etc, department then they need to be added to certerin exchange groups. I am currently working with Switch and then IF, but i end up doing a lot of code copy-pasting.

Is there a smarter way to write such logic?


switch ($Country) {
    US {
            add user to "A" exchange groups
            If (user department = sales then add user to "B" exchange groups
            if (user department = Accounting then add user to "C" exchange groups 
    }

    DK { 
            add user to "D" exchange groups
            If (user department = sales then add user to "E" exchange groups
            if (user department = Accounting then add user to "F" exchange groups 
    }
    
    DE { 
         and so on ...
    }

    GB { 
          and on...   
    }

    NL { 
        .....
    }

    IT { 
        
    }

    FR { 
        
    }

    AT { 
       
    }

    CH { 
        
    }

    PH { 

    }

Are the "Z exchange groups" (or β€œX” or β€œY”) the same for the "US" and "DK" and so on? If Yes why make a difference then? If no - what do you meant with …

Hi Olaf.

I can see that is unclear.
The Z, Y, X groups are different for each country.

I meant to write: With my current code logic, then i end up doing a lot of copy-pasting.

OK, so you have to have a kind of decision tree. And if the groups in the different countries are different I don’t see another way than to have them all individually represented in this decision tree. :man_shrugging:t4:

It sounds like an onboarding process. Usually you put new employees of certain departments in dedicated groups and the rest should come from this group memberships. But the initial treatment may be - depending on the structure of your company - cumbersome. :man_shrugging:t4:

1 Like

Thanks for your feedback Olaf, that is much appriciated. :slight_smile: