Issue with multiple values in Powershell variable

Gabriel Gonzalez 1 Reputation point
2022-10-25T22:59:14.72+00:00

Hello All.

I'm currently facing an issue while running a DNS PS cmdlet trying to extract the current two dns servers set for an specific nic card and comparing them to a variable

Here's the variable:

$Variable1 = "10.99.1.4","10.99.1.3"

And here's the little piece of code:

Variable2 = (Get-DnsClientServerAddress -AddressFamily IPv4 | where "InterfaceAlias" -match "Main")

For variable2, I'm getting the following:

$Variable2.ServerAddresses

10.99.1.4
10.99.1.3

for variable1 within the script, I get the same results:

$Variable1

10.99.1.4
10.99.1.3

When I compare the two variables with the following code, I'm getting that they are different and I cannot find why (I should be getting an "OK" output):

If ($Variable2.ServerAddresses -eq $Variable1) {Write-Output "Main NIC DNS ORDER --> OK!"} else {Write-Output "Main NIC DNS ORDER --> FAILED (x)"}

MAIN NIC DNS ORDER --> FAILED (x)

I've already tried the following:

setting variable1 as a system string as the following:

[string []] $Variable1 = "10.99.1.4","10.99.1.3"

Spliting with commas :

$Variable1'10.99.1.4,10.99.1.3' -split ','

using -match instead of -eq

but no luck so far.

Any ideas??

Thx a lot

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2022-10-26T02:07:56.667+00:00

    Arrays are not strings. :-)

    $Variable1 = "10.99.1.4","10.99.1.3"  
    $Variable2 = [PSCustomObject]@{ServerAddresses = @('10.99.1.4','10.99.1.3')}    # array elements in the same order as $Variable1  
    $Variable3 = [PSCustomObject]@{ServerAddresses = @('10.99.1.3','10.99.1.4')}    # array elements in a dufferent order to $Variable1  
      
    # Compare variables 1 and 2  
    if ($null -eq (Compare-Object -ReferenceObject $Variable1 -DifferenceObject $Variable2.ServerAddresses -IncludeEqual | Where-Object {$_.SideIndicator -ne '=='})){  
        "The two arrays are equal"  
    }  
    else{  
        "The two arrays are not equal"  
    }  
      
    # Compare variables 1 and 3  
    if ($null -eq (Compare-Object -ReferenceObject $Variable1 -DifferenceObject $Variable3.ServerAddresses -IncludeEqual | Where-Object {$_.SideIndicator -ne '=='})){  
        "The two arrays are equal"  
    }  
    else{  
        "The two arrays are not equal"  
    }  
      
    

  2. Rich Matheisen 47,901 Reputation points
    2022-10-26T19:13:29.317+00:00

    Try this:

    $Control = "10.99.1.4","10.99.1.3"  
    $Variables = @( [PSCustomObject]@{ServerAddresses = @('10.99.1.4','10.99.1.3')},                # array elements in the same order as $Control  
                    [PSCustomObject]@{ServerAddresses = @('10.99.1.3','10.99.1.4')},                # array elements in a different order to $Control  
                    [PSCustomObject]@{ServerAddresses = @('10.99.1.3','10.99.1.9')},                # array elements different to $Control  
                    [PSCustomObject]@{ServerAddresses = @('10.99.1.3','10.99.1.4','10.99.1.9')}     # different number of array elements to $Control  
                  )  
      
    $ControlCount = 0  
    if ($Control -is [array]){  
        $ControlCount = $Control.Count  
    }  
    else{  
        Throw "`$Control is not an array"  
    }  
      
    foreach ($v in $Variables){  
        if ($v.ServerAddresses -is [Array]){  
                if ($v.ServerAddresses.Count -ne $ControlCount){  
                    Write-Warning "Different array sizes"  
                    Continue  
                }  
        }  
        for ($i=0; $i -le ($ControlCount - 1); $i++){  
            if ($Control[$i] -ne $v.ServerAddresses[$i]){  
                Write-Warning "Array elements differ, or array element orders are different"  
                Break  
            }  
        }  
    }  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.