是否有强制powershell -Export-CSV cmdlet维护特定列顺序的方法? [英] Is there a way to force powershell -Export-CSV cmdlet to maintain a specific column order?
问题描述
我的问题是我如何确保一个csv文件我创建一个DataTable保持列顺序我指定?
我的脚本这样填充csv头和值如下:
... snip ...
pre>
$ dataTable | ForEach-Object {
$ csv + = New-Object PSObject -Property @ {
program_code = $ _。ProgramCode;
first_name = $ _。FirstName;
last_name = $ _。LastName;
email = $ _。email;
phone = $ _。phone;
phone2 = $ _。otherphone;
address = $ _。addr1;
address2 = $ _。addr2;
city = $ _。city;
state = $ _。state;
postal_code = $ _。Zip;
country = $ _。国家;
grad_year = $ _。HsGradDate;
lead_date = $ _。LeadDate;
lead_source = $ _。LeadSource;
school_status = $ _。SchoolStatus;
}
}
$ csv |导出CSV C:\scripts\NewLeads $(Get-Date -Format yyyyMMdd).csv -notype -Append
... snip ...
我希望文件具有在脚本中指定的顺序的列,但是当我在记事本或excel中打开它时,列出现在一个看似随机的订购。
解决方案在PowerShell V3中,而不是:
$ csv + = New-Object PSObject -Property @ {
我会使用:
$ csv + = [pscustomobject] @ {
PowerShell V3解析器会将散列文字 [ordered]或[pscustomobject]。
如果您使用V2,则需要将-Property参数跳过到New-Object和而是使用多个调用Add-Member。它将看起来像:
$ csv + = New-Object PSObject |
Add-Member -Name program_code -Value $ _。ProgramCode -MemberType NoteProperty -PassThru |
Add-Member -Name first_name -Value $ _。FirstName -MemberType NoteProperty -PassThru |
...
I'm new to powershell, so the script is a Frankenstein of various examples of from sites.
My question is how can I make sure that a csv file I am creating for a DataTable keeps the column order I specify?
My script does this to populate the csv headers and values like so:
...snip... $dataTable | ForEach-Object { $csv+=New-Object PSObject -Property @{ program_code=$_.ProgramCode; first_name=$_.FirstName; last_name=$_.LastName; email=$_.email; phone=$_.phone; phone2=$_.otherphone; address=$_.addr1; address2=$_.addr2; city=$_.city; state=$_.state; postal_code=$_.Zip; country=$_.Country; grad_year=$_.HsGradDate; lead_date=$_.LeadDate; lead_source=$_.LeadSource; school_status=$_.SchoolStatus; } } $csv | Export-CSV C:\scripts\NewLeads$(Get-Date -Format yyyyMMdd).csv -notype -Append ...snip...
I want the file to have to columns in the order that I specify in the script, but when I open it in notepad or excel the columns appear in a seemingly random order. Keyword being seemingly since they may have some method of ordering themselves.
解决方案In PowerShell V3, instead of:
$csv+=New-Object PSObject -Property @{
I would use:
$csv+=[pscustomobject]@{
The PowerShell V3 parser will preserve the order of the keys when you cast a hash literal to either [ordered] or [pscustomobject]. A small side benefit to this approach - it will also be faster.
If you are using V2, you'll need to skip the -Property parameter to New-Object and instead use multiple calls to Add-Member. It will look something like:
$csv+=New-Object PSObject | Add-Member -Name program_code -Value $_.ProgramCode -MemberType NoteProperty -PassThru | Add-Member -Name first_name -Value $_.FirstName -MemberType NoteProperty -PassThru | ...
这篇关于是否有强制powershell -Export-CSV cmdlet维护特定列顺序的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!