Is there a possibility for me to retrieve a IF condition from a variable. I want to store conditions in a database outside of the code and then retrieve the condition and put it in the IF Condition. If the condition is successful then I will perform an action as mentioned in the database. it will be something like below.
Not in that way, no. $condition would need to be True or False. You could potentially store the entire code block and use Invoke-Expression to evaluate it. There might be a way to cast a string as a script block, but I’m still not sure PowerShell would evaluate it as Code the way you’re asking.
Chris, that’s not quite the question. In your case, $a is storing True or False, not the expression itself. PowerShell evaluates it and stores the result. He’s asking if there’s a way to pull a string from a database and evaluate it.
You could, but using Invoke-Expression, which does open you up to a lot of potential abuse or attacks. Assuming the condition is in $c…
if ( (Invoke-Expression $c) ) {
# this runs if $c evaluates to True
}