【问题标题】:Issue Exporting array to CSV in powershell在 Powershell 中将数组导出为 CSV 的问题
【发布时间】:2016-11-08 19:34:32
【问题描述】:

我有以下代码查询 MSSQL 服务器数据库,然后将结果与 CSV 进行比较,将 CSV 中与数据库中对应行不匹配的任何行添加到新的 CSV,以创建 CSV仅包含更改的行并丢弃未更改的行。

$priceCSV = Import-Csv C:\Users\user\Desktop\pricedelta\Products.csv

$newpriceCSV = @(,@("SKU","BrandName","ID","Name","1stCheapestDistributorName","1stCheapestDistributorPrice","1stCheapestDistributorStock","1stCheapestDistributorProductName","1stCheapestDistributorDeliveryCost","2ndCheapestDistributorName","2ndCheapestDistributorPrice","2ndCheapestDistributorStock","2ndCheapestDistributorProductName","2ndCheapestDistributorDeliveryCost","3rdCheapestDistributorName","3rdCheapestDistributorPrice","3rdCheapestDistributorStock","3rdCheapestDistributorProductName","3rdCheapestDistributorDeliveryCost","4thCheapestDistributorName","4thCheapestDistributorPrice","4thCheapestDistributorStock","4thCheapestDistributorProductName","4thCheapestDistributorDeliveryCost","5thCheapestDistributorName","5thCheapestDistributorPrice","5thCheapestDistributorStock","5thCheapestDistributorProductName","5thCheapestDistributorDeliveryCost","DescriptionType"))

####
#Checks for changed rows are here
####

$NewArray = @()
$names = @("SKU","BrandName","ID","Name","1stCheapestDistributorName","1stCheapestDistributorPrice","1stCheapestDistributorStock","1stCheapestDistributorProductName","1stCheapestDistributorDeliveryCost","2ndCheapestDistributorName","2ndCheapestDistributorPrice","2ndCheapestDistributorStock","2ndCheapestDistributorProductName","2ndCheapestDistributorDeliveryCost","3rdCheapestDistributorName","3rdCheapestDistributorPrice","3rdCheapestDistributorStock","3rdCheapestDistributorProductName","3rdCheapestDistributorDeliveryCost","4thCheapestDistributorName","4thCheapestDistributorPrice","4thCheapestDistributorStock","4thCheapestDistributorProductName","4thCheapestDistributorDeliveryCost","5thCheapestDistributorName","5thCheapestDistributorPrice","5thCheapestDistributorStock","5thCheapestDistributorProductName","5thCheapestDistributorDeliveryCost","DescriptionType")

$skip = 1

foreach($row in $newpriceCSV)
{

    if($skip -eq 1){
        $skip = 0
        continue
    }

    $obj = new-object PSObject
    for ($i=0;$i -lt 30; $i++){
        $obj | add-member -membertype NoteProperty -name $names[$i] -value $row[$i]
    }
    $NewArray+=$obj

    $obj=$null
}

$NewArray

$NewArray | Export-Csv -path "C:\Users\user\Desktop\pricedelta\NewProducts.csv" -NoTypeInformation -Encoding Unicode

数据库查询和检查以及其他一切工作正常,但是当我尝试将结果导出到 CSV 时,我会遇到一些不同的问题,具体取决于我的尝试。

如果我只是尝试 Export-Csv $newpriceCSV,那么我会在第一行和第二行得到一堆废话,然后是每一行后面的逗号序列,类似于此处显示的内容:http://sharepoint-community.net/profiles/blogs/powershell-from-an-array-to-comma-separated-file-csv-via-the#

尝试在代码中显示的同一链接上找到的方法后,我收到以下错误:

Unable to index into an object of type System.Management.Automation.PSObject

对于行:

$obj | add-member -membertype NoteProperty -name $names[$i] -value $row[$i]#

真的不知道从哪里开始,感谢任何帮助

【问题讨论】:

  • 显然真正的$newpriceCSV 不包含数组,这就是错误消息所说的。使用 PowerShell ISE 在循环内设置断点并评估 $row.GetType()$row | gm 此外,您的代码效率极低,切换到使用 foreach 作为表达式以自动生成数组和 [PSCustomObject]$hashtable 方法而不是添加-成员。

标签: powershell csv


【解决方案1】:

您似乎在概念上遇到问题,而不是技术上。 PS 教程通常将这些概念视为不言自明的,有时确实如此。如果您想了解发生了什么,在比您的情况更简单的情况下,请尝试复制以下问答中提供的建议答案,看看您是否得到相同的结果。

https://social.technet.microsoft.com/Forums/scriptcenter/en-US/9d37b573-0f6e-4176-bda5-0e188b41d014/powershell-easy-way-to-output-an-array-to-csv-text-file?forum=ITCG

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-22
    • 2017-07-03
    • 1970-01-01
    • 2018-09-15
    • 2013-10-25
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    相关资源
    最近更新 更多