简体   繁体   中英

Exchange powershell Export-csv

First off just a little warning that my PowerShell skills are very limited, so I may be re-inventing the wheel and doing it badly.

Anyway to the point I'm trying to create/run a script to pull all records of delivered mail to all accounts using the message tracking log. The issue I'm having is when exporting my results to csv it almost seems as if the results are encrypted. Below is the code I've managed to piece together:

$msg = Get-TransportServer server_to_query | Get-MessageTrackingLog -ResultSize Unlimited -Start "10/01/2014 10:00:00" -End "10/01/2014 11:00:00" -EventID deliver | select-object -ExpandProperty Recipients | Group-Object | sort count -desc | ft -auto  

Now I know it gives me the information I want as when I do the below it displays the first 10 records as well as how many in total.

$msg.count
$msg | select -first 10

However when i do the below its not exporting it to anything at even looks close.

$msg | Export-CSV export.csv 

Its most likely to be really basic what im missing or not understanding however I feel like I'm going round in cycles now. Any pointers in the right direction would be great.

Thanks Mark

Your main issue is that you populate $msg with results for format-table . Almost anyone would say that it is fine for shell ouput only. If you ever intend to do anything else with the data Format-Table is not the way to go. First, remove the ft -auto

$msg = Get-TransportServer server_to_query | Get-MessageTrackingLog -ResultSize Unlimited -Start "10/01/2014 10:00:00" -End "10/01/2014 11:00:00" -EventID deliver | select-object -ExpandProperty Recipients | Group-Object | sort count -desc

After that it will export better but you will see things like this in your csv:

"System.Collections.ArrayList","17","System.Collections.ObjectModel.Collection`1[System.Management.Automation.PSObject]"

Since most of this data is just an email address repeated over and over i would wonder if you export should be something like this.

$msg | select name,count -first 10 | Export-Csv c:\temp\test.csv

Dont know if that is the output you are going for but it looks like it is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM