繁体   English   中英

在Powershell中将数组导出到CSV问题

[英]Issue Exporting array to CSV in powershell

我有以下代码,查询MSSQL Server数据库,然后将结果与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时,根据我的尝试,遇到了一些不同的问题。

如果我只是简单地尝试导出-Csv $ newpriceCSV,那么我在第一行和第二行上都会出现一堆废话,然后在后面的每一行使用一系列逗号,类似于此处显示的内容: http : //sharepoint-community.net / profiles / blogs / powershell从数组到逗号分隔的文件csv-via#

尝试在与代码所示相同的链接上找到的方法后,出现以下错误:

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

对于该行:

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

真的卡住了从这里去的地方,感谢您的帮助

看起来您遇到的是概念问题,而不是技术问题。 PS教程通常将这些概念视为显而易见的,有时甚至是显而易见的。 如果您想了解发生了什么,在比您更简单的情况下,请尝试复制以下问答中提供的建议答案,看看是否获得相同的结果。

https://social.technet.microsoft.com/Forums/scriptcenter/zh-CN/9d37b573-0f6e-4176-bda5-0e188b41d014/powershell-easy-way-to-output-an-array-to-csv-text-文件?论坛= ITCG

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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