【发布时间】: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