Output to CSV, by row
i have the script below , output on console of information want add csv file. i have information written in 1 row per each dns record. right now, of information in 1 column, list. if take quotes off, information per row, domain controller name , names of variables. i have done before using quotes , worked. not sure going on now.
import-module activedirectory import-module dnsserver #create log file $date = (get-date).tostring('mmddyyy') $outfile = new-item -type file c:\temp\"$date"testdns.csv -force $newline = @() #get domain controllers $domaincontrollers = (get-addomain).replicadirectoryservers #get domain dns root $dnsrootdomain = (get-addomain).dnsroot foreach ($domaincontroller in $domaincontrollers) { write-host $domaincontroller -foregroundcolor green $serverdnsnames = get-content "c:\temp\testservers.txt" foreach ($serverdnsname in $serverdnsnames) { $revhostname = $serverdnsname + '*' # write-host $hostname ##getting record data $arecorddata = (get-dnsserverresourcerecord -computername $domaincontroller -zonename $dnsrootdomain -rrtype -name $serverdnsname) write-host "a record host name "$arecorddata.hostname write-host "a record type "$arecorddata.recordtype write-host "a record timestamp "$arecorddata.timestamp write-host "a record ip address "$arecorddata.recorddata.ipv4address.ipaddresstostring #$newline += $domaincontroller,$arecorddata.hostname,$arecorddata.recordtype, $arecorddata.timestamp, $arecorddata.recorddata.ipv4address.ipaddresstostring add-content $outfile -value $domaincontroller,$arecorddata.hostname,$arecorddata.recordtype, $arecorddata.timestamp, $arecorddata.recorddata.ipv4address.ipaddresstostring -encoding ascii # $newline | export-csv $outfile -notypeinformation ##getting ptr record data $revrecorddata = (get-dnsserverzone -computername $domaincontroller | {$_.zonename -like "*in-addr.arpa"} | get-dnsserverresourcerecord -computername $domaincontroller -rrtype ptr | {$_.recorddata.ptrdomainname -like $revhostname}) write-host "ptr record host name "$revrecorddata.hostname -foregroundcolor yellow write-host "ptr record type "$revrecorddata.recordtype -foregroundcolor yellow write-host "ptr record timestamp "$revrecorddata.timestamp -foregroundcolor yellow write-host "ptr record data "$revrecorddata.recorddata -foregroundcolor yellow } }
you need initialize $newline array once each dc, not once in script.
richard mueller - mvp enterprise mobility (identity , access)
Windows Server > Windows PowerShell
Comments
Post a Comment