header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Logical Operators in Powershell
Last Post 03 Jan 2008 10:45 AM by tHyEstes. 5 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
tHyEstesUser is Offline
New Member
New Member
Posts:13
Avatar

--
02 Jan 2008 10:03 PM  

Hi All,

 

I think I am overlooking a very simple thing but I cannot figure it out.

I am using a function with a parameter but I want to check if the parameter is valid so there are three valid parameters, test1, test2, and test3 if something else is given as parameter an error is thrown. How can it be that even when I call the function with a valid parameter still the exeption is thrown?

If (($Location -ne "test1";) -or ($location -ne "test2";) -or ($location -ne "test3";)){throw "Location must be test1, test2 or test3)"}

 

Can somebody help?

 

Regards,

Martijn

bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
03 Jan 2008 01:04 AM  
That is because it cant be all three it will fail always (one of the -or conditions will fail.)

A better options would be test for -eq. I don't generally like to have an if statement with only an else, but in this case it fits

if(($location -eq "test1") -or ($location -eq "test2") -or ($location -eq "test3")){continue}else{throw "Location must be test1, test2 or test3)"}

You could also use a switch statement
switch($location)
{
"test1" {continue}
"test2" {continue}
"test3" {continue}
default {throw "Location must be test1, test2 or test3)"}
}
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
SAPIENScripterUser is Offline
New Member
New Member
Posts:45

--
03 Jan 2008 01:22 AM  
I prefer using Switch as well. Otherwise, use -and instead of -or. What is happening to expand on Brandon's point is that if $location equals "test1", then the other two comparisons will false and the -OR says, return TRUE if at least one of the comparisons is TRUE which is happening.
Jeffery Hicks
Microsoft PowerShell MVP
http://blog.sapien.com
http://www.scriptinganswers.com

"Those who forget to script are doomed to repeat their work."
bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
03 Jan 2008 02:38 AM  
I don't believe you can use -and for this. Using -and all conditions must be $true.

-Or will work, but you have to test for a positive response not negative.

The key is understanding the if statement only sees $true or $false. If the result is $true then the if statement completes. If it is $false, the else statement (if exist) completes.

In this case we are checking for -ne. Since $location cannot be all three one of the -ne will return $true.

If we check for -eq instead then it will only return $true if the value matches so we get an expected response.

All that said... switch is clear and the way to go IMO.
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
SAPIENScripterUser is Offline
New Member
New Member
Posts:45

--
03 Jan 2008 03:01 AM  
But of course. That's what I get for trying to post something without thinking clearly after a long day. There's a reason double negatives are a no-no in writing and they should be in scripting as well. :-)

I agree that using Switch is the way to go. That was my initial thought but I figured I'd try to explain why -or wasn't working. Now that my head is back on straight I think I'll be ok.
Jeffery Hicks
Microsoft PowerShell MVP
http://blog.sapien.com
http://www.scriptinganswers.com

"Those who forget to script are doomed to repeat their work."
tHyEstesUser is Offline
New Member
New Member
Posts:13
Avatar

--
03 Jan 2008 10:45 AM  

Oke thanks for the replies it's all clear to me now

You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer