Author Posts

March 10, 2016 at 6:54 am

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.

March 10, 2016 at 7:04 am

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.

March 10, 2016 at 7:13 am

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

March 10, 2016 at 7:15 am

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!

March 10, 2016 at 7:22 am

	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.

March 10, 2016 at 7:32 am

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.

March 10, 2016 at 7:38 am

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.

March 10, 2016 at 8:14 am

Much easier in 2012 if you have one available.

March 10, 2016 at 8:19 am

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

March 10, 2016 at 8:22 am

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.

March 10, 2016 at 8:27 am

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

March 10, 2016 at 9:21 am

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'

}


}