1

done some googling but answers I have found seem to be more complex than what I need. I have a simple script to fetch DNS cache entries, and I'd like to export the results to a CSV in the most "powershell" manner. Code looks like this:

function Get-Dns
{
    $domains = @()
    $cmdOutput = Invoke-Command -ScriptBlock {ipconfig /displaydns}
    ForEach ($line in $($cmdOutput -split "`r`n"))
    {
        if ($line -like '*Record Name*'){
            $domain = $line -split ":"
            $domains += $domain[1]
    }
}

so I have an array, $domains, which I would like to use Export-CSV to essentially output a one column CSV with one domain per line. Using Export-CSV seems to just output the length of each element rather than the contents itself. Any help is appreciated!

3 Answers 3

2

"ipconfig /displaydns" is going to give you back a big'ol string array, which is going to be harder to work with. Try the native commandlets for DNS manipulation:

Get-DnsClientCache | Export-Csv -Path .\stuff.csv

If you're using Windows 7 or earlier, try this...

$dns_client_cache = @()
$raw_dns_data = ipconfig /displaydns
for ($element = 3; $element -le $raw_dns_data.length - 3; $element++) {
    if ( $raw_dns_data[$element].IndexOf('Record Name') -gt 0 ) {
        if ( $dns_entry ) { $dns_client_cache += $dns_entry }
        $dns_entry = New-Object -TypeName PSObject
        Add-Member -InputObject $dns_entry -MemberType NoteProperty -Name 'RecordName' -Value $raw_dns_data[$element].Split(':')[1].Trim()
    } elseif ( $raw_dns_data[$element].IndexOf('Record Type') -gt 0 ) {
        Add-Member -InputObject $dns_entry -MemberType NoteProperty -Name 'RecordType' -Value $raw_dns_data[$element].Split(':')[1].Trim()
    } elseif ( $raw_dns_data[$element].IndexOf('Time To Live') -gt 0 ) {
        Add-Member -InputObject $dns_entry -MemberType NoteProperty -Name 'TimeToLive' -Value $raw_dns_data[$element].Split(':')[1].Trim()
    } elseif ( $raw_dns_data[$element].IndexOf('Data Length') -gt 0 ) {
        Add-Member -InputObject $dns_entry -MemberType NoteProperty -Name 'DataLength' -Value $raw_dns_data[$element].Split(':')[1].Trim()
    } elseif ( $raw_dns_data[$element].IndexOf('Section') -gt 0 ) {
        Add-Member -InputObject $dns_entry -MemberType NoteProperty -Name 'Section' -Value $raw_dns_data[$element].Split(':')[1].Trim()
    } elseif ( $raw_dns_data[$element].IndexOf('CNAME Record') -gt 0 ) {
        Add-Member -InputObject $dns_entry -MemberType NoteProperty -Name 'CNAMERecord' -Value $raw_dns_data[$element].Split(':')[1].Trim()
    }
}

$dns_client_cache | Export-Csv -Path .\dns_stuff.csv -Force -NoTypeInformation

Sorry! I know it's messy.

Sign up to request clarification or add additional context in comments.

6 Comments

Wow. Very useful. Thank you!
Quick question - this appears to be supported on some hosts I am testing on, but not on others? Is there a way to ensure it will run on any system with powershell installed? thanks!
Add the -CimSession parameter: -CimSession <remotemachine>
Ah, looks like Get-DnsClientCache is not supported on Win7. Lame! I think i will have to go down the cmd displaydns route then.
Yikes. Yea, Windows 7 doesn't have the needed OS bits for that commandlet. I believe you are correct.
|
2

The most PowerShell way:

(ipconfig /displaydns|where{$_-match'Record Name'})|%{$_.split(":")[1].Trim()}>dnscache.txt

4 Comments

Thanks for the input!
Is there any way to achieve the same result but with using the Export-Csv function? I am trying to stay consistent with other aspects of the script that already exist.
Without seeing the other parts of your script I couldn't tell you how to do that. For data to properly output into a csv you need to create an object, I'll work out an example and post another answer.
actually, looks like @LordAdam has a good solution that will do what you are probably looking for, that's pretty much what I had in mind.
0

I ended up going with to export multiple value array to csv

$Data | %{$_} | export-csv -nti -Path C:\

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.