我有以下代码查询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# < / p>
尝试在代码中显示的同一链接上找到的方法后,出现以下错误:
Unable to index into an object of type System.Management.Automation.PSObject
表示行:
$obj | add-member -membertype NoteProperty -name $names[$i] -value $row[$i]#
真的坚持从这里出发去哪里,感谢任何帮助
答案 0 :(得分:0)
看起来你在概念上遇到了麻烦,而不是技术。 PS教程经常将这些概念看作是不言而喻的,有时它们也是如此。如果您想了解发生了什么,在比您更简单的情况下,请尝试复制以下问答中提供的建议答案,看看您是否得到相同的结果。