DJMcCarthy12
DJMcCarthy12

Reputation: 4119

Export list/array to CSV in Powershell

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!

Upvotes: 1

Views: 6038

Answers (3)

user10779965
user10779965

Reputation: 1

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

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

Upvotes: 0

Adam
Adam

Reputation: 4178

"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.

Upvotes: 2

brendan62269
brendan62269

Reputation: 1106

The most PowerShell way:

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

Upvotes: 2

Related Questions