1

I have a script snippet that basically gets some unformated xml type output from a command.

Then in a defined filter section I'm transforming that into xml and run a search loop on each node, as from the part below.

What I'm trying to figure out is how I can make a multiple if -and loop, like if (($CimProperty.VALUE -eq $somevariable) -and ($CimProperty.VALUE -eq $something-else))

The only problem is that since it's a foreach loop it won't take it, as it takes each property at a time and then the 'if statement -and portion' for it, which doesn't work since it's the same xml type property section. In other words the loop doesn't go through the entire array to identify both conditions from the if statement.

PS code snippet:

filter Import-CimXml
{
    $CimXml = [Xml]$_
    $CimObj = New-Object -TypeName System.Object
    foreach ($CimProperty in $CimXml.SelectNodes(“/INSTANCE/PROPERTY”))
    {
        if ($CimProperty.VALUE -eq $somevariable)
          {
             write-host "found it"
          }
    }
 }

I hope the scenario is clear, thanks everyone in advance!

2 Answers 2

1

For just two conditions, you can make it fairly straight forward like (the untested);

filter Import-CimXml
{
    $foundfirst  = $false
    $foundsecond = $false
    $CimXml = [Xml]$_
    $CimObj = New-Object -TypeName System.Object
    foreach ($CimProperty in $CimXml.SelectNodes(“/INSTANCE/PROPERTY”))
    {
        if ($CimProperty.VALUE -eq $somevariable)
        {
            $foundfirst = $true
        }
        if ($CimProperty.VALUE -eq $someothervariable)
        {
            $foundsecond = $true
        }
    }
    if ($foundfirst -and $foundsecond)
    {
        write-host "found it"
    }
}

For more conditions, you may want to use arrays of corresponding matchwords/booleans instead.

Sign up to request clarification or add additional context in comments.

1 Comment

That worked perfectly Joachim, I was fixed on figuring a way without the use of other variables and ways, but I've just followed your solution and it works. Thanks a lot!
1

Just extend your xpath query to do it all. It's less code and should be more efficient.

filter Import-CimXml
{
    $CimXml = [Xml]$_
    $CimObj = New-Object -TypeName System.Object
    if($CimXml.SelectNodes(“/INSTANCE[PROPERTY='$somevariable' and PROPERTY='$someothervariable']”).Count -gt 0) { 
        write-host "found it" 
    }
 }

Comments

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.