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)” }
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.
Assumed you meant to run Get-DNSServerZone and not Get-SServerZone your code should look something like this:
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.
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.
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.