Powershell - ignore blank combo box

Gareth Davies 276 Reputation points
2021-09-21T19:25:27.697+00:00

I have created a Powershell "app" to make new user creation follow a standard, as in ensure all required attributes have data entered.

As part of this I have added some combo boxes to remind the person creating the account to add new users to security and distribution groups.
Some accounts do not require more than a single security and distribution group, so when the account is created via the "app" (it's a GUI with text entry boxes, combo boxes represented by drop down lists and buttons to execute the account creation process) it spits out a series of errors in the Powershell window because it is trying to add the new account to groups, but the combo box is empty so it can't do what it is being told.

So, my question is, how do I tell it to ignore the instruction to add the new account to a group if the combo box is empty?

The code I'm using, same code for each group apart from the -Identity value.

Add-ADGroupMember -Identity $Securitygroup4dropdown.Text -Members $saMAccountNameentry.Text -confirm:$false

The whole thing is over 2000 lines long, most of it being the GUI, so I am not going to post the whole script here, but to give a little more info
$saMAccountNameentry.Text is a free text entry box used to create the saMAccountName, it is also used to construct the UPN and email address
$Securitygroup4dropdown.Text is one of a series of combo boxes that shows a list of all security groups, the top line of this is blank so unless an entry is selected it has a null value, it's this null value the script doesn't like and causes it to throw the errors.

The errors do not stop the account being created but I don't like seeing the errors. I know I can tell it to hide the Powershell window but that's covering up the errors rather than removing them...

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,572 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,306 Reputation points
    2021-09-21T19:51:21.097+00:00

    Why not check the $Securitygroup4dropdown.Text to see if it's an empty string? If it is, don't try the add the user to any group.

    if ($Securitygroup4dropdown.Text.Trim().Length -gt 0){
        AddAdGroupMember . . .
    }
    

    I'd actually use a more realistic check on the length so it's at least as long as your shortest group name.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Gareth Davies 276 Reputation points
    2021-09-21T20:08:52.943+00:00

    RichMatheisen-8856 That did exactly what I needed, the 0 characters actually works perfectly, as I said the top line of each is blank so if the user doesn't select something from the list the value is null.

    Thank you

    0 comments No comments

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.