Page 1 of 1

Incorrect loop execution on Windows 10

Posted: Thu Jan 09, 2020 1:25 pm
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

Re: Incorrect loop execution on Windows 10

Posted: Thu Jan 09, 2020 2:13 pm
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?

Re: Incorrect loop execution on Windows 10

Posted: Thu Jan 09, 2020 11:34 pm
by Alexander Riedel
[Topic moved by moderator]

Re: Incorrect loop execution on Windows 10

Posted: Fri Jan 10, 2020 2:40 am
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.