If I use .Count to check the number of items in an empty array, like this:
$theNames = @()
$theTotalNames = $theNames.Count
it finds nothing, and the numeric variable $theTotalNames is 0, as expected
But I have a situation where if I use .Count to check the contents of seemingly empty array, it is returning 1.
I'm populating the array with the results returned from the Invoke-RestMethod query like this:
$responseData = Invoke-RestMethod -Uri $url -Method Get -Headers $headers
$theNames = @($responseData.PsObject.Properties["the_field"].value.field_name)
$theTotalNames = $theNames.Count
If the query returns nothing because there were no fields found, $theTotalNames somehow equals 1. If the query returns one or more items, $theTotalItems will correctly equal 1.. or higher
When I display the contents of $theNames array after the query that returned nothing, the array seems empty.
If I check what's in the array, like this:
if ($theNames) {
"The array contains something"
}
else {
"The array contains nothing"
}
the console always says the array contains nothing.
So, why does .Count think there's at lease one item in the array?
$null -eq $theNames[0]