Or statements

Ask your PowerShell-related questions, including questions on cmdlet development!
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 6 years and 8 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
A. Hohl
Posts: 18
Last visit: Thu Aug 01, 2019 3:13 am

Or statements

Post by A. Hohl »

Hello,

I am trying to create a script that will validate AD credentials and only allow certain users to access the form that this script will open. So I have it prompting to enter login information and that is working great. The problem is where the ** are when I do the or statement it then allows everyone to run the script instead of going to the else command.

so if user4 tries to sign in they should get the DO ERRO ACTION event. however, they are not, they are getting the DO ACTION command. Any help? or need more details?

$cred = Get-Credential
$username = $cred.username
$password = $cred.GetNetworkCredential().password

$CurrentDomain = "LDAP://" + ([ADSI]"").distinguishedName
$domain = New-Object System.DirectoryServices.DirectoryEntry($CurrentDomain,$UserName,$Password)

**if($cred.UserName -eq "USER1" -or "USER2" -or "USER3"){DO ACTION}
Else{DO ERROR ACTION}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Or statements

Post by jvierra »

You need to fix the "if" syntax.

help about_if
User avatar
A. Hohl
Posts: 18
Last visit: Thu Aug 01, 2019 3:13 am

Re: Or statements

Post by A. Hohl »

Can you give any details? If I say USER1 or USER2 it works fine it’s when I add the third user and so it it does not work. The ** are showing where I am having the problem. Not actually in the script.

Thanks.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Or statements

Post by jvierra »

You must use the match for each instancw and OR the matches.

if( $a -eq 1 -or $a -eq 2){ ...

If you OR them it will always give you true.

1 -or 2
1 -or 3
1 -or 4

All test will always be true.

It is basic logic and it is the part of logic that most fail on because it is not how we take. It is a test. You must test each possibility.
User avatar
A. Hohl
Posts: 18
Last visit: Thu Aug 01, 2019 3:13 am

Re: Or statements

Post by A. Hohl »

Worked perfectly thank you for your help!
This topic is 6 years and 8 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