3

我正在尝试导入 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
}
4

2 回答 2

5

每个$row都是PSCustomObject

PS > $row.GetType().Name
PSCustomObject

访问基础属性值:

foreach ($column in $row.psobject.Properties.Value){
...
}

检查是否$column$null、空白或空白:

if ([System.String]::IsNullOrWhiteSpace($column)) { ... }
于 2018-09-05T21:14:31.140 回答
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")" 
  }
}
于 2018-09-05T21:17:27.297 回答