我正在编写一个脚本,该脚本进行 API 调用并返回 JSON 结果,并在 Powershell 中作为自定义对象数组进行处理。自定义对象的属性之一是状态,因此对于每个唯一状态,我想获取具有该状态的对象的计数。当我的某些状态的代码返回计数为 0 时,我感到很惊讶,因为如果它一开始就不存在,我的脚本就不会搜索该状态。
然后我注意到当有多个具有该状态的对象时代码有效,但是当只有一个具有该状态的对象时,我的代码无法使用 Where-Object 找到它。如果我遍历自定义对象数组并执行 if() 语句,它会找到所有状态,包括只有一个对象的状态。
我在这里遗漏了什么还是这是一个错误?我的笔记本电脑运行的是 PS 5.1,我还在运行 PS 4.0 的 W2K12 服务器上进行了尝试,我得到了同样的结果。
我还能够在下面的代码中模拟这一点(删除 API 和 JSON 部分):
$testArray = @()
$testObject = New-Object -TypeName PSObject
$testObject | Add-Member -type NoteProperty -name Name -value 'Name1' -Force
$testObject | Add-Member -type NoteProperty -name Status -value 'Status1' -Force
$testArray += $testObject
$testObject = New-Object -TypeName PSObject
$testObject | Add-Member -type NoteProperty -name Name -value 'Name2' -Force
$testObject | Add-Member -type NoteProperty -name Status -value 'Status2' -Force
$testArray += $testObject
$testObject = New-Object -TypeName PSObject
$testObject | Add-Member -type NoteProperty -name Name -value 'Name3' -Force
$testObject | Add-Member -type NoteProperty -name Status -value 'Status3' -Force
$testArray += $testObject
$testObject = New-Object -TypeName PSObject
$testObject | Add-Member -type NoteProperty -name Name -value 'Name4' -Force
$testObject | Add-Member -type NoteProperty -name Status -value 'Status1' -Force
$testArray += $testObject
$testObject = New-Object -TypeName PSObject
$testObject | Add-Member -type NoteProperty -name Name -value 'Name5' -Force
$testObject | Add-Member -type NoteProperty -name Status -value 'Status1' -Force
$testArray += $testObject
$testObject = New-Object -TypeName PSObject
$testObject | Add-Member -type NoteProperty -name Name -value 'Name6' -Force
$testObject | Add-Member -type NoteProperty -name Status -value 'Status1' -Force
$testArray += $testObject
$testObject = New-Object -TypeName PSObject
$testObject | Add-Member -type NoteProperty -name Name -value 'Name7' -Force
$testObject | Add-Member -type NoteProperty -name Status -value 'Status3' -Force
$testArray += $testObject
$testObject = New-Object -TypeName PSObject
$testObject | Add-Member -type NoteProperty -name Name -value 'Name8' -Force
$testObject | Add-Member -type NoteProperty -name Status -value 'Status3' -Force
$testArray += $testObject
$testObject = New-Object -TypeName PSObject
$testObject | Add-Member -type NoteProperty -name Name -value 'Name9' -Force
$testObject | Add-Member -type NoteProperty -name Status -value 'Status3' -Force
$testArray += $testObject
$testObject = New-Object -TypeName PSObject
$testObject | Add-Member -type NoteProperty -name Name -value 'Name10' -Force
$testObject | Add-Member -type NoteProperty -name Status -value 'Status1' -Force
$testArray += $testObject
$testObject = New-Object -TypeName PSObject
$testObject | Add-Member -type NoteProperty -name Name -value 'Name11' -Force
$testObject | Add-Member -type NoteProperty -name Status -value 'Status1' -Force
$testArray += $testObject
$testObject = New-Object -TypeName PSObject
$testObject | Add-Member -type NoteProperty -name Name -value 'Name12' -Force
$testObject | Add-Member -type NoteProperty -name Status -value 'Status1' -Force
$testArray += $testObject
$testObject = New-Object -TypeName PSObject
$testObject | Add-Member -type NoteProperty -name Name -value 'Name13' -Force
$testObject | Add-Member -type NoteProperty -name Status -value 'Status1' -Force
$testArray += $testObject
$testObject = New-Object -TypeName PSObject
$testObject | Add-Member -type NoteProperty -name Name -value 'Name14' -Force
$testObject | Add-Member -type NoteProperty -name Status -value 'Status3' -Force
$testArray += $testObject
$testObject = New-Object -TypeName PSObject
$testObject | Add-Member -type NoteProperty -name Name -value 'Name15' -Force
$testObject | Add-Member -type NoteProperty -name Status -value 'Status3' -Force
$testArray += $testObject
$testObject = New-Object -TypeName PSObject
$testObject | Add-Member -type NoteProperty -name Name -value 'Name16' -Force
$testObject | Add-Member -type NoteProperty -name Status -value 'Status3' -Force
$testArray += $testObject
Write-Host "`n`nPowerShell version:"
$PSVersionTable.PSVersion
Write-Host "`n`nUsing Where-Object on the array"
$uniqueStatuses = ( $testArray.Status | sort | Get-Unique )
foreach ($status in $uniqueStatuses)
{
$status
($testArray | Select-Object | Where-Object { $_.Status -eq $status }).Count
}
Write-Host "`n`nLooping through the array"
foreach ($status in $uniqueStatuses)
{
$status
[int]$count = 0
foreach ($object in $testArray)
{
if ($object.Status -eq $status)
{
$count++
}
}
$count
}
在上面的示例代码中,状态 Status2 仅出现在自定义对象之一中,并且 Where-Object 由于某种原因找不到它,但使用 foreach 可以。
当我运行上面的代码时,我得到:
PowerShell 版本:
主要次要版本修订
5 1 14393 0
在数组上使用 Where-Object
状态1
8
状态2
状态3
7
遍历数组
状态1
8
状态2
1
状态3
7