.csv データベースの「コード」プロパティの値を確認したい。「コード」フィールドの最初の 3 文字が「製品」の最初の 3 文字に (-eq) 等しい場合、結果の数 (カウント) を表示し、入力を .csv データベースに追加します。
ただし、結果が null であるかのようにカウントは表示されません。
foreach ($row in $InventoryContents)
{
#Lets build a String first
$exportString = $row.User +','+$row.Product+','+$row.Company+','+$row.Model+',';
#Everything right so far
#first three characters of Product
$firstKey = $row.Product.SubString(0,3).ToUpper()
echo $firstKey;
#problem here
$nonSortedContents = Import-Csv $nonSortedCsv | where-Object { ($_.Code.split('-')[0]) -eq $firstKey }
#but arrapently this works : Import-Csv $nonSortedCsv| where-Object { ($_.Code.split('-')[0]) -eq "MON"}
$lines = $nonSortedContents.count
echo $lines
#null
#if not enetered
if ($lines -eq $null)
{
$wholeKey = $firstKey +'-'+ ("{0:D5}" -f 0).ToString()
$exportString = $exportString + $wholeKey
$exportString -join "," >> $nonSortedCsv
}
else
{
$wholeKey = $firstKey +'-'+ ("{0:D5}" -f $lines).ToString()
$exportString = $exportString + $wholeKey
$exportString -join "," >> $nonSortedCsv
}
}
私はそれで動作するようになりました
$nonSortedContents = @(Import-Csv $nonSortedCsv | where-Object { ($_.Code.split('-')[0]) -eq $firstKey })
どうやら、CSV ファイルに 1 行しかない場合、Import-Csv は PSCustomObject を返します。複数の行がある場合、返されるオブジェクトは Object[] です。