4

I am wondering how or if I can use dynamic conditional parameters when using an IF statement in powershell?

Essentially the list of conditions could be 1 to many and I would have to build out some sort of filter, but not sure how to achieve this in powershell, or if its possible.

I created a small example of what I am trying to do, obviously in a real example the parameters would be truly dynamic.

$include = @("*dynamic*", "*text*")

$testString = "This is a string of text where I want to match dynamic conditional statements"

$filter = ""

foreach ($i in $include) {
$filter = $([string]::Format('{0} -like "{1}"', $filter, $i))
}

write-host $filter

#What I want it to do in pseduo code
#if ($testString matches $filter) { DoSomething }

#What it should literally do based on above example
if ($testString -like "*dynamic*" -and $testString -like "*text*") { write-host $testString }

This is how I got it working for anyone else who runs into problem and needs a clearer example. I basically built out the statement using a foreach loop on the contents of the array containing the parameters. If it was the first parameter it include the initial IF statement, and after loop finishes it tacks on the closing brackets and what code to run if the IF condition is met.

$include = @("*dynamic*", "*text*")

$testString = "This is a string of text where I want to match dynamic conditional statements"

$varnametocompare = '$testString'

$x = 0
$filter = ""

foreach ($i in $include) {
if ($x -eq 0) {
        $filter = $([string]::Format('if({0} -like "{1}"', $varnametocompare, $i))
        $x++
    }
else {
        $filter = $([string]::Format('{0} -or {1} -like "{2}"', $filter, $varnametocompare, $i))
     }
}
$filter = $([string]::Format('{0}) {{ Write-Host {1} }}', $filter, $varnametocompare))

Invoke-Expression $filter
1
  • 1
    if ($(foreach ($i in $include) { if ($testString -notlike $i) {$false; break} } $true)) { DoSomething } Commented Apr 28, 2016 at 4:22

1 Answer 1

1

PowerShell essentially already does this with Where-Object, so you could just use that.

[ScriptBlock]s are basically [string]s already.

if ($testString | Where-Object $filter) { <# ... #> }

Actually I realize now that won't quite work. But you can use Invoke-Expression instead:

if ((Invoke-Expression $filter)) { <# ... #> }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the response Brian, but I am having trouble with the syntax. I've tried a bunch of variations based off the powershell invoke-expression detailed help, and other examples found off google, but nothing seems to work. If you could offer specific example based off my example that would be greatly appreciated!
This lead me to the right answer, but needed some reworking.. I have included crude example of how I got it to work, but basically I needed to encapsulate the entire statement in a string, not just the filtering portion, and then use Invoke-Expression.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.