ForEach not working with Variable

I’m a PowerShell noobie, so please be patient with me.

I run the following command against my Doamin Controller:

[String]$varZoneList = @(Invoke-Command -ComputerName DC4 -ScriptBlock {
** Get-SServerZone | **
** Where-Object {$_.IsReverseLookupZone -EQ $False} | **
** Select-Object -ExpandProperty ZoneName })

It runs fine, and if I view the contents of “$varZoneList” I see all my DNS Zone names with a space between each.
The issue is when I try to run “ForEach” referencing the of “$varZoneList” variable. I run the command below and get nothing:

ForEach ($Zone IN $varZoneList) { Write-Host “`n$($Zone.ZoneName)” }

I’m hoping/guessing it’s something simple to fix.

Any help would be greatly appreciated.

Thanks in advance.

With casting the result to the type of [String] and with using -ExpandProperty for your Select-Object you turn rich and powerful objects into stupid plain text. In PowerShell we want to work with objects and properties. :wink:

Assumed you meant to run Get-DNSServerZone and not Get-SServerZone your code should look something like this:

$varZoneList = 
Invoke-Command -ComputerName DC4 -ScriptBlock {
    Get-DNSServerZone |
    Where-Object { -not $_.IsReverseLookupZone } 
}

Now if you want to see one of the properties contained in the object saved in the variable $varZoneList you can use the dot notation like this:

$varZoneList.ZoneName

BTW: When you post code, error messages, sample data or console output please format it as code using the preformatted text button ( </> ). Place the cursor on an empty line, click the button and then paste your code.

Thanks in advance.

1 Like

Olaf, thanks for your response. When I run your script the variable ($varZoneList) only contains: “DnsServerPrimaryZone”.

DnsServerPrimaryZone DnsServerPrimaryZone DnsServerConditionalForwarderZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerStubZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerPrimaryZone DnsServerPrimaryZone

When I run my script the variable ($varZoneList) contains the expected DNS zones. (I removed the “[String]” from the variable.)
I apologize. I may have not been clear in my initial post.
I would like to use the ‘ForEach’ statement to read each DNS Zone name from the “$varZoneList” variable and write it to the screen. This is for testing at this point to get my scripting right. Later I will run other cmdlets against each DNS Zone names. Right now I just need to get the ‘ForEach’ statement working.

Thanks again.

The code I posted does exactly this in my environment. What do you see when you run the following snippet in your environment?

Invoke-Command -ComputerName DC4 -ScriptBlock {
    Get-DNSServerZone | Where-Object { -not $_.IsReverseLookupZone }
}

I see somethin like this:

ZoneName           ZoneType IsAutoCreated IsDsIntegrated IsReverseLookupZone IsSigned
--------           -------- ------------- -------------- ------------------- --------
_msdcs.contoso.com Primary  False         True           False               False
contoso.com        Primary  False         True           False               False
TrustAnchors       Primary  False         True           False               False
1 Like

Hi Olaf,

Sorry, I feel like an idiot for not realizing this in the beginning. When I run the variable ($varZoneList) part of the script and the ‘ForEach’ statement on the domain controller (DC4), it works fine when I enter a PSSession.

Here is where I am running into the issue. I open a PowerShell windows on my laptop and ‘run as’ with my AD domain admin account. (I do NOT enter a PSSession to the DC4.) The variable ($varZoneList) part of the script runs and the DNS Zone data is in there. The ‘ForEach’ statement does not appear to like this (not being in a PSSession).

I would like to be able to use the ‘ForEach’ statement against the data in the variable ($varZoneList) without opening a PSSession, if it is possible. However, you have quite a lot more experienced than me and I will follow your advice, if it is that I should enter a PSSession.

Again, sorry for the lack of info in my original post and replies.

It does not matter where you run the code. If you’re allowed to query the DNS server it should work.

??? ??? ???

What is it actually what you’re trying to do?

Just query the server you like and save the result in a variable.

$DNSZoneList = 
    Get-DNSServerZone -ComputerName DC4 | 
        Where-Object { -not $_.IsReverseLookupZone }

Now you can do whatever you need to do with the objects saved in the variable $DNSZoneList.

foreach ($DNSZone in $DNSZoneList) {
    $DNSZone
}
1 Like

Olaf,

Thanks. Looks like I made things too complicated in my script.

I ran your latest code and it works exactly like it is supposed to.

I appreciate your help.