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.
This topic is 4 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
chavans
Posts: 17
Last visit: Thu Jan 09, 2020 1:27 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: 1655
Last visit: Thu Mar 28, 2024 7:24 am
Answers: 39
Been upvoted: 30 times

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
SAPIEN Technologies, Inc.
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Incorrect loop execution on Windows 10

Post by Alexander Riedel »

[Topic moved by moderator]
Alexander Riedel
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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.
This topic is 4 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked