Query a Static array against a dynamic array

This topic contains 11 replies, has 4 voices, and was last updated by Profile photo of Dan Potter Dan Potter 6 months, 2 weeks ago.

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #36363
    Profile photo of Ed O'Connor
    Ed O’Connor
    Participant

    I am having a issue trying to verify that if one or more items in a static array are in the results of a dynamic array that it simply output $true or $false.

    I thought this would be pretty simply but I can't get it to work for the life of me.

    
    $ZonesExist = $null
    
    $VerifyExists = "testVerify.com"
    
    
    $ExistingZones = (gwmi -Namespace root\MicrosoftDNS -Class MicrosoftDNS_Zone -Filter "ZoneType = 4" | select-object Name)
    
    
    ForEach ($Zone in $ExistingZones) {
    	If ($Zone -like "$VerifyExists") {$ZonesExist -eq $True}}
    		Else {$ZonesExist = $False} 
    }
    }
    $ZonesExist
    

    No matter what I put as the comparison operator (-like, -notlike, -eq) it returns True

    Also, if I type $ExistingZones it definitely shows 'testVerify' in the list along with a few others.

    Any help is appreciated.

    #36367
    Profile photo of Don Jones
    Don Jones
    Keymaster

    The contents of $Zone is an object that has a Name property. You can't do a direct -like against the entire object. Try:

    $zone.name -like "*VerifyExists*"

    Instead. Or, in your Select, use Select-Object -Expand Name instead, which will make $ExistingZones a collection (not an array) of simple String objects.

    However, if $VerifyExists is a collection, then -like can't work. You would use -contains or -in instead, although those don't accept wildcards.

    #36372
    Profile photo of random commandline
    random commandline
    Participant

    This will result in True or False.

    $VerifyExists = "testVerify.com"
    $ExistingZones = (gwmi -Namespace root\MicrosoftDNS -Class MicrosoftDNS_Zone -Filter "ZoneType = 4" | select-object -ExpandProperty Name)
    $ExistingZones -contains $VerifyExists
    
    #36374
    Profile photo of Ed O'Connor
    Ed O’Connor
    Participant

    I had tried the -expand and it made no difference.

    I just tried to make the changes you suggested and no matter which variation I try everything still comes out as $True

    I feel like I am missing something very basic!

    #36376
    Profile photo of Don Jones
    Don Jones
    Keymaster
    	If ($Zone -like "$VerifyExists") {$ZonesExist -eq $True}}
    		Else {$ZonesExist = $False} 
    

    Look at what you're doing in the If statement. You're not assigning $True to $ZoneExists, you're comparing $ZoneExists to $True. Remove -eq and change it to =. -Eq is for comparing; = is for assigning.

    #36378
    Profile photo of Ed O'Connor
    Ed O’Connor
    Participant

    Sorry Don, that was my typo/mistake. I copied a scratchpad version of what I was doing which I modified so it didn't give any client info away and it still had the '{ZonesExist -eq $True). My working copy does use the = sign.

    #36379
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Then either $Zone or $VerifyExists may not contain what you think it does. Without running your code in your environment, I can't get more specific – but if it were me, I'd write some "debug code" to output those variables, so that I could see what they actually contain.

    Also, not knowing if this is your scratchpad thing again:

    ForEach ($Zone in $ExistingZones) {
    	If ($Zone -like "$VerifyExists") {$ZonesExist -eq $True}}
    		Else {$ZonesExist = $False} 
    }
    }
    

    There are too many curly braces going on here. If you look, you've got an extra one after the $True, for example, and another extra one at the very end. That would change the behavior.

    Finally, and again not knowing what your actual code looks like, you're outputting $ZonesExist AFTER the END of the loop. That means the variable will only ever reflect the most recent comparison. You could have ten $False in a row and, with this logic, you'd never see them. If the 11th item was $True, that'll you'd ever see.

    #36386
    Profile photo of Dan Potter
    Dan Potter
    Participant

    Much easier in 2012 if you have one available.

    #36387
    Profile photo of Dan Potter
    Dan Potter
    Participant

    'zonename' -in (gwmi -Namespace root\MicrosoftDNS -Class MicrosoftDNS_Zone -Filter "ZoneType = 4").name

    #36389
    Profile photo of Don Jones
    Don Jones
    Keymaster

    And just a modification of that for future references...

    (gwmi -Namespace root\MicrosoftDNS -Class MicrosoftDNS_Zone -Filter "ZoneType = 4" | select -expand name) -contains 'zonename'
    

    Since that's more compatible with older versions of PowerShell that don't have the magic secret property enumeration trick introduced in v3 ;). Also note the reversal of logic to use the older -contains rather than -in – useful trick to know if you need to run on 1.0 or 2.0.

    #36390
    Profile photo of Ed O'Connor
    Ed O’Connor
    Participant

    Random CommandLine,

    Thank you, what you gave me helped a lot. I can now specify multiple zones in my $VerifyExists and search against the $ExistingZones.

    This is what I came up with:

    $VerifyExists = ("zone1.com","zone2.com","zone3.cn")
    $ExistingZones = (gwmi -Namespace root\MicrosoftDNS -Class MicrosoftDNS_Zone -Filter "ZoneType = 4" | select-object -ExpandProperty Name)
    
    
        Foreach ($Verify in $VerifyExists) { 
            Foreach ($Zone in $ExistingZones) {
            write-output "Processing $Verify against $Zone"
            $Zone -contains $Verify}
            }
    

    Now I can move on the the next step. Thanks again

    #36393
    Profile photo of Dan Potter
    Dan Potter
    Participant

    You don't need the double foreach or the parens around the zonelist.

    $V = "zone1.com","zone2.com","zone3.cn"
    
    $zones = (gwmi -Namespace root\MicrosoftDNS -Class MicrosoftDNS_Zone -Filter "ZoneType = 4").name
    
    foreach($i in $v){
    
    if($zones -contains $i){
    
    'dosomething'
    
    }
    
    
    }
Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.