Issue Exporting array to CSV in powershell -
i have following code queries mssql server database, , compares result csv, rows in csv not match counterpart row in database added new csv, create csv contains changed lines , discards ones havent changed.
$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 changed rows 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 ($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
the database query , checks , else work fine, when try export results csv come against few different issues depending on try.
if try export-csv $newpricecsv bunch of nonsense on first , second line, sequence of commas every row after, similar shown here: http://sharepoint-community.net/profiles/blogs/powershell-from-an-array-to-comma-separated-file-csv-via-the#
after trying method found on same link shown in code, following error:
unable index object of type system.management.automation.psobject
for row:
$obj | add-member -membertype noteproperty -name $names[$i] -value $row[$i]#
really stuck on go here, appreciated
it looks you're having trouble concepts, rather techniques. ps tutorials treat concepts though self evident, , are. if want understand what's going on, in simpler case yours, try replicating suggested answer provided in following q&a , see if same results.
Comments
Post a Comment