我想检查 .csv 数据库的“代码”属性的值。如果“代码”字段的前三个字符等于 (-eq) 到“产品”的前三个字符,则显示结果数(计数)并将输入添加到 .csv 数据库。
但是,计数并没有显示,就好像结果为空一样。
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 文件中只有一行时,Import-Csv 返回一个 PSCustomObject。当多于一行时,返回的对象是一个Object[]。