Hi guys, I know that I am missing something very simple, I just can’t figure out what it is. I have a list of servers that I need to get a list of the DNS servers that they are pointing to.

This is what I have

Get-Content dnsservers.txt | Foreach-Object {Get-DnsClientServerAddress –ExpandProperty ServerAddresses}| Export-csv -path dnsforwarderlist-output.csv

when i run it, i get “Get-DnsClientServerAddress : A parameter cannot be found that matches parameter name ‘ExpandProperty’.” yet when i run it against the computer that I am on, i get the correct info.

How can I run this against a list of remote servers

please let me know

thanks

app

7 Spice ups

This?

Get-Content dnsservers.txt | 
	Foreach-Object {
	Get-DnsClientServerAddress |Select-Object –ExpandProperty ServerAddresses
} | Export-csv -path dnsforwarderlist-output.csv -notypeinformation

2 Spice ups

-notypeinformation on the export-csv cmdlet drops the object type line that gets inserted as the fist line of the csv by default.

The -expandproperty of the select-object cmdlet can only expand 1 property. Can be a pain if you want 2 or more.

Alternatively, with PS v3 and above, this should work too:

Get-Content dnsservers.txt | Foreach-Object {
	(Get-DnsClientServerAddress).ServerAddresses
} | Export-csv -path dnsforwarderlist-output.csv -notypeinformation

edit: typos.

2 Spice ups

i am getting close. for some reason when it runs, I get the following

Length
13
13
16
16
16
13
13
16

What command did you run?

Food first, then I’ll fire up my windows VM.

edit: don’t pipe the output to export-csv for now …

1 Spice up

There’s about 3 things you’re missing…M Boyle pointed out the missing Select. On top of that, you need to use each item in your ForEach (right now, you’re basically just running the same command for every item in the list, but you’re not actually using them).

For example:

Get-Content c:\temp\somefile.txt | foreach { Do-Something $_ | select -ExpandProperty PropertyIWant}

Note the “$_”. That refers to the current item in the pipeline.

The third thing is that Get-DnsClientServerAddress is not going to give you the forwarders for your server. You want either Get-DnsServer or Get-DnsServerForwarder (both can get it for you).

I have a script that should do what you’re looking for; you can find it here: http://community.spiceworks.com/scripts/show/3169-list-forwarders-for-all-domain-dns-servers

thanks Raven Hunter. for this script, I am not looking for the domain DNS forwarders, I am looking for the DNS servers that all of these servers are using.

app

DOH! I even thought that to myself earlier “I wonder where the $_ is?”.

Nice little script, I shall borrow it :slight_smile:
Minor thing, you don’t need to use a backtick after the pipe as a line continuations char. The pipe will suffice.

Oh, got it.

Then Get-DnsClientServerAddress is what you want. That being said, it doesn’t have a -ComputerName parameter, so you’ll have to run it on each server. Fortunately, Invoke-Command will take care of that for you.

Also, you’re going to run in to a problem in that you need to “put your output together” to get what you’re looking for, which means setting up an output object and its properties.

I’ve gone ahead and added what I think you’ll need to your script; this worked in my testing, but you may have different restrictions in your environment that might prevent this from working. It won’t work with IP addresses.

$output = Get-Content dnsservers.txt | Foreach-Object {
    $serverName = $_
    $serverOutput = Invoke-Command -ComputerName $serverName -ScriptBlock {
        Get-DnsClientServerAddress | Where-Object -Filter {$_.AddressFamily -eq 2 -and $_.ServerAddresses.count -ge 1}
    }
    
    foreach ($interface in $serverOutput) {
        $props = @{'ServerName' = $serverName;
                   'InterfaceAlias' = $serverOutput.InterfaceAlias;
                   'InterfaceIndex' = $serverOutput.InterfaceIndex;
                   'ServerAddresses' = $($serverOutput.ServerAddresses -join '; ')}

        $obj = New-Object -TypeName PSObject -Property $props
        Write-Output $obj
    }
}

$output | Export-Csv -Path dnsforwarderlist-output.csv -NoTypeInformation

Let me know if that does the trick.

EDIT: I realized I didn’t test putting that in a CSV file, and when I did, it had a problem I suspected it might. I’ve modified the script to correct that problem. Basically, the ServerAddresses field would just have the text “System.String” in it, which is why I added the “-join '; '”. Basically, the problem is that that field is populated by a string array instead of a string, which doesn’t translate properly when being output as a string; it’s seen by the system as an object instead of text, so it just shows what type of object is in that field. By using -join, it takes all parts of the array and puts them together with the delimiter you specify in between, in this case, a semicolon (I prefer a semicolon for this since it’s going in to a comma delimited file; it won’t hurt it to use commas, since it wraps everything in double quotes, but that’s just the way I am.

2 Spice ups

I know; I frequently do it anyway, as that way I don’t have to try to remember all of the rules for when you can and can’t leave the backtick out. Thanks for the feedback!

thanks to the both of you for the help. Raven, you totally knocked it out of the park with that help.

thanks again

No problem; but you’ll want to use the updated version I just put up, not the first version. I found a bug already :wink:

1 Spice up

I find the rule quite simple, if it doesn’t work then I may need a backtick :slight_smile:

2 Spice ups

Very good point. That being said, I have a tendency to script in Notepad++, then copy and paste it in to a PowerShell window, so depending on the size of the script, tracking those issues down can be a bit of an extra burden. The backtick doesn’t even take an extra second to hit, and the byte that it adds to the script won’t make my script too big :wink:

Actually, these days I use a nice wide screen monitor which is shorter but wider (disturbingly like me these days too, but no more about that), so I’ll happily use longer lines.

1 Spice up

That’s what I’ve got, too; sadly, half the time, I have it zoomed in so everything’s a bit bigger. My eyes aren’t what they used to be.