If Statement Help

I created a script to copy groups from templates user names we have in a flat file on a network share. The script works good, however I’m having an issue with the If statements that give a status. I can’t get an if statement to work if no user was keyed into the textbox. Any ideas? I’ve removed the If statement that I was attempting to use.

Add the Quest Addon

Add-PSSnapin Quest.ActiveRoles.ADManagement

Load the Winforms assembly

[reflection.assembly]::LoadWithPartialName( “System.Windows.Forms”)

Create the form

$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(600,220)

#Change the background color. Remove the “#” and key in a color

$Form.BackColor = “gray”

#Set the dialog title
$form.text = “AD Group Copy Tool”
#####################################################################################

$list = Get-content “\server\Applications\ADTemplates\ADTemplates.txt”
#Removed the real server name for the post

write-host $list.count #total lines read from file
foreach ($line in $list)
{
write-host $line
}

Create label to select template

$label = New-Object Windows.Forms.Label
$label.Location = New-Object Drawing.Point 50,25
$label.Size = New-Object Drawing.Point 200,15
$label.text = “Select the Appropriate Template User”
#####################################################################################

Drop Down Box Size and Location

$DropDownBox = New-Object System.Windows.Forms.ComboBox
$DropDownBox.Location = New-Object Drawing.Point (20,50)
$DropDownBox.Size = New-Object Drawing.Point (250,20)
$DropDownBox.DropDownHeight = 200
#####################################################################################

foreach ($line in $list) {
$DropDownBox.Items.Add($line)
}

Create label to key in the new user ID

$label2 = New-Object Windows.Forms.Label
$label2.Location = New-Object Drawing.Point 350,25
$label2.Size = New-Object Drawing.Point 200,15
$label2.text = “Key in the new user ID”
#####################################################################################

Create TextBox for the new user ID

$DName = New-Object System.Windows.Forms.TextBox
$DName.Location = New-Object Drawing.Point 300,50
$DName.Size = New-Object Drawing.Point 250,20
#####################################################################################

Create Button and set text and location

$button = New-Object Windows.Forms.Button
$button.text = “Execute”
$button.Location = New-Object Drawing.Point 500,132
$button.BackColor = “red”
#####################################################################################

Button to execute the command

$button.add_click({
$x=$DropDownBox.SelectedItem
$DName2=$DName.Text
$K = Get-QADUser ($x) |select memberof
foreach($user in $K.memberof)
{
Add-QADGroupMember -Identity $user -Member $Dname.Text
#$GroupResult = Add-QADGroupMember -Identity $user -Member $Dname.Text
}
$Name = $Dname.Text
$User2 = Get-QADUser -LDAPFilter “(sAMAccountName=$Name)”
If ($User2 -eq $Null)
{ $GroupResult = ECHO $DName.Text “does not exist in Active Directory”
$outputBox.text=($GroupResult)
}
Else {$GroupResult = ECHO Completed copying groups from $x to $DName.Text
$outputBox.text=($GroupResult)
}
If ($x -eq $Null)
{ $GroupResult = ECHO “Please select a template user from the dropdown box”
$outputBox.text=($GroupResult)
}
})

#####################################################################################

#Create Output Box, location and size
$outputBox = New-Object System.Windows.Forms.TextBox
$outputBox.Location = New-Object System.Drawing.Size(10,100)
$outputBox.Size = New-Object System.Drawing.Size(565,25)
$outputBox.MultiLine = $True

$outputBox.ScrollBars = “Vertical”

$Form.Controls.Add($outputBox)
#####################################################################################

Create label to for the output status box

$Status = New-Object Windows.Forms.Label
$Status.Location = New-Object Drawing.Point 10,80
$Status.Size = New-Object Drawing.Point 565,15
$Status.text = “Status:”
#####################################################################################

Create label to key in the new user ID

$Footer = New-Object Windows.Forms.Label
$Footer.Location = New-Object Drawing.Point 10,155
$Footer.Size = New-Object Drawing.Point 500,15
$Footer.text = “This Tool Requires Quest.ActiveRoles.ADManagement Add-on for PowerShell 2.0”
#####################################################################################

Add the controls to the Form

$form.controls.add($button)
$form.controls.add($label)
$form.controls.add($label2)
$form.controls.add($DName)
$form.controls.add($DName2)
$form.controls.add($User)
$form.controls.add($DropDownBox)
$form.controls.add($Footer)
$form.controls.add($Status)
#####################################################################################

Display the dialog

$form.ShowDialog()

I’m not sure what you’re asking, since you’ve removed the If statement that was giving you problems. Are you just trying to detect whether the user has entered any text? If so, you have a couple of options (with many variations of ways to check for an empty string):

if ($DName.TextLength -eq 0) { }
if ($DName.Text -eq [String]::Empty) { }

Good call! The if ($DName.TextLength -eq 0) { } worked perfectly. I was initially trying if ($DName.Text -eq $null) which wasn’t working.

In this case, it’s never null. If the box is empty, the Text property will return a non-null (but empty) string.

In general, you can avoid having problems with this distinction by using the String.IsNullOrEmpty method:

if ([String]::IsNullOrEmpty($someVariable)) { }

Thanks for the feedback. I appreciate the help.