Incorrect loop execution on Windows 10

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
chavans
Posts: 17
Joined: Fri Jun 14, 2013 1:25 pm

Incorrect loop execution on Windows 10

Post by chavans »

To help you better we need some information from you.

*** Please fill in the fields below if you are currently using a Trial Version of the Product. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***

Product, version and build: Sapien powershell studio 2012, 3.1.19
32 or 64 bit version of product: 64 bit
Operating system: Windows 10
32 or 64 bit OS: 64 bit

*** Please add details and screenshots as needed below. ***
My program runs perfectly on a Windows 7 workstation, but does not function properly on Windows 10 workstation.

Below is a snippet of the script

if(($textbox2.Text -eq "") -or ($textbox2.Text -eq $null))
{
[void][System.Windows.Forms.MessageBox]::Show("Enter email address to send the info to !","No receiver specified")
$buttonGO.Enabled = $true
}
else
{
$validation = $null
$validation = Get-ADUser -filter * | where {$_.UserPrincipalName -eq $textbox2.Text}
if($validation -eq $null)
{
[void][System.Windows.Forms.MessageBox]::Show("Incorrect recipient email address","Check email address")
$errorFlag = 1
}
}


For some reason it keeps going in the else loop even when i am entering the correct email address in textbox2.

DO NOT POST SUBSCRIPTIONS, KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM

User avatar
brittneyr
Site Admin
Posts: 545
Joined: Thu Jun 01, 2017 7:20 am

Re: Incorrect loop execution on Windows 10

Post by brittneyr »

I need more information in order to help you.
-Are there are any error messages?
-What version of PowerShell are you running?
-Does your script run in the ISE?
-Are you able to post more of your script?
Brittney Ryn
SAPIEN Technologies, Inc.

User avatar
Alexander Riedel
Posts: 7458
Joined: Tue May 29, 2007 4:43 pm

Re: Incorrect loop execution on Windows 10

Post by Alexander Riedel »

[Topic moved by moderator]
Alexander Riedel
SAPIEN Technologies, Inc.

jvierra
Posts: 14545
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Incorrect loop execution on Windows 10

Post by jvierra »

The correct way to test a controls contents is like this:

This:
if(($textbox2.Text -eq "") -or ($textbox2.Text -eq $null))
Should be this:
if($textbox2.Text){

When designing logic we should always try to use positive testing and not negative as it is easier to understand and lees prone to errors.

Here is how to structure this test, Notice it uses only positive logic which also simplifies the code.

Code: Select all

if($textbox2.Text){
    if(Get-ADUser -filter "UserPrincipalName -eq '$($textbox2.Text)'"){
        $buttonGO.Enabled = $true
    }else{
        [void][System.Windows.Forms.MessageBox]::Show('Incorrect recipient email address','Check email address')
    }
}else{
    [void][System.Windows.Forms.MessageBox]::Show("Enter email address to send the info to !","No receiver specified")
}
Also you are returning ALL users then testing which would be very inefficient on a large network. The filter will return a user if found or $null if not found.
The positive logic approach is easier to understand and read. It also makes the code much simpler.

I would remove the test of the textbox in favor of validation on the textbox if this is not in the "validated" event.

Locked