Evaluating Parenthesized Boolean Expressions

rated by 0 users
This post has 2 Replies | 2 Followers

Not Ranked
Posts 2
dynek Posted: 04-27-2012 3:47 AM

Hey all,

I've been playing with Powershell for a few months now (if not years) and I came across, what I think is, an interesting case.

I have to evaluate a string as a boolean. For instance I am given the string "($true -and $false) -or $true" and I need to find a way so it returns $true.
Also the string "($true -and $false) -or $false" would return $false.

Apparently there's a way to do it in JAVA: http://snipplr.com/view/42595/evaluating-parenthesized-boolean-expressions-using-stacks/
But I'm not able to find any in Powershell.

Reason behind this is that I have to evaluate if a user is member of groups such as "user is member of (group 1 and group 2) or group 3".

I hope I'm clear enough. Thanks for your time and your help!

Not Ranked
Posts 2

It's like explaining the issue on the forum makes one find the answer by himself.

I will still appreciate any input on this. It seems that the following would do the trick:

$test = '($true -and $false) -or $true'
Invoke-Expression  $test

Top 10 Contributor
Posts 326
Microsoft MVP
Top Contributor

well - when I try it, I get:

PSH [C:\foo]: ($true -and $false) -or $true
True

 

So it works here...

Page 1 of 1 (3 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.