我正在尝试导入一个csv,搜索每行中的单元格值(列),然后查找并计算任何空值或空白值。然后,如果计数达到13,则执行X。但是,当我运行此代码时,它看起来像元/列值是单个对象,而不是每列的单个值?如何搜索行中的单个单元格值?
示例代码
$DataFileLocation = "\\Server\Output.csv"
$sheet = import-csv $DataFileLocation
$count = 0
foreach ($row in $sheet) {
foreach ($column in $row) {
Write-Host "Searching value: $column"
if ($column -eq $null -or " ") {
Write-Host "Found a blank!"
$count++
}
}
$count
if ($count -eq 13) {
Write-Host "Found match!" -ForegroundColor Red
}
$count = 0
}
答案 0 :(得分:5)
每个$row
是一个PSCustomObject
:
PS > $row.GetType().Name
PSCustomObject
访问基础属性值:
foreach ($column in $row.psobject.Properties.Value){
...
}
检查$column
是$null
,空白还是空白:
if ([System.String]::IsNullOrWhiteSpace($column)) { ... }
答案 1 :(得分:1)
或类似的东西
$DataFileLocation = "\\Server\Output.csv"
$sheet = import-csv $DataFileLocation
$count = 0
foreach ($row in $sheet){
($row | Get-Member) | ? { $_.MemberType -eq "NoteProperty" } | % {
$n = $_.Name; "$n = $($row."$n")"
}
}