Getting DNS Records from Subnets Listed in a File RRS feed

  • Question

  • I have a script that I am running that will dump all DNS records into a CSV file. I have figured out a way to get specific subnets. The problem is that I have 10+ subnets to get. I would like to be able to import a list of subnets, and have the data called to get all of the subnets in my file and put it into a final export. 

    here's what i have that is working:

    Get-DnsServerResourceRecord -ZoneName zone.local -RRType A | where-object {$_.Recorddata.ipv4address -like "10.1.*.*" -or $_.Recorddata.ipv4address -like "10.2.*.*"} | Select-Object HostName, RecordType ,@{Name='IPv4Address';Expression={$_.RecordData.IPv4Address}},Timestamp,TimeToLive,DistinguishedName

    Now I could just take out the "where-object" statements and dump all data into a file and manually go in and take out what i dont need, but I was hoping to try to get the data that I DO need into a file because this export file will be used for a second step on another script. I'm trying to automate it as much as possible. 

    here's what i tried to do:

    $subnets = get-content .\zonedata.txt

    get-dnsserverresourcerecord -zonename "" -RRType A | foreach ($subnet in $subnets) {where-object {$_.Recorddata.ipv4address -like $subnet}} |Select-Object HostName, RecordType ,@{Name='IPv4Address';Expression={$_.RecordData.IPv4Address}}

    This just returns an error for me. If i can get this part to work from a file, it would save me a ton of time!

    Thursday, May 9, 2019 10:04 PM

All replies

  • "Foreach()" cannot be used in a pipeline.  You need to use "ForEach-Object"

    help foreach-object -online


    Thursday, May 9, 2019 10:31 PM
  • get-dnsserverresourcerecord -zonename "" -RRType A | foreach { foreach ($subnet in $subnets)

    I think that you need 2 foreach statements. One to process the pipeline ($_) and another to process the named object ($subnet). Note how ISE objects to pipeing to "foreach ($file in $files)". 

    • Edited by MotoX80 Friday, May 10, 2019 2:02 PM
    Friday, May 10, 2019 12:32 AM