Powershell - skip line if entry is blank

Gareth Davies 276 Reputation points
2020-12-24T20:01:13.227+00:00

I have a Powershell form used by support staff to enter required data for a new user account, it then goes away and creates the account, populates attributes and assigns an Office 365 license as required.
I included the option to add users to up to 4 security groups and 4 distribution groups. It all works just fine, however if any of the group memberships are left blank, as in a user is only added to 2 distribution groups, the Powershell window gives errors because of the null values, it still creates the user and adds them to the selected groups so it does not stop anything.

I have added the instruction to hide the powershell window and only display the form itself, but I would like it if I could find a way to tell it to skip the add to groups if the value is blank, my neat freak side is not happy seeing these lines of red code show up even though only I see them and they do not represent an actual problem.
The groups are selected via a dropdown list displayed in a combobox, one combobox per possible group addition and the selected group is used as the value for a variable which the code then uses to add the user to the selected group. Each variable is included in its own line of code which adds the group membership.

So my question is this, is there a way to tell it to skip the add to group operation if no value is selected in the combobox?

We have a traditional AD domain, all DCs are Server 2016 but the functional level is still 2012 R2 thanks to a legacy CRM app that will not work if we raise the domain to 2016, this is being replaced but at this moment we are stuck with 2012 R2. We sync with Azure/Office 365 using AADC. The environment is 80% virtual running in Hyper-V

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2020-12-24T20:23:52.08+00:00

    The value of the combobox is stored in variable?

    Than something like this should work:

    if ($value1)
    {# add user to ad-group code}
    

    Maybe it is easier to help if you post the code.


    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  2. Rich Matheisen 47,901 Reputation points
    2020-12-24T20:56:33.1+00:00

    Not doing data validation before you use user-entered data is almost always a bad idea! It's how you invite things like SQL-injection attacks, among other bad things.

    Even something as simple as this works to your advantage:

    $val = "x,"
    if ($val.trim()) { # quick check to verify that data is present and not all spaces
        $ver = $val.trim()   # get rid of leading/trailing spaces (can also trim other characters)
        if ($ver -notmatch "^[A-Za-z0-9_-]+$"){ # typical naming characters
            "Bad name"
        }
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.