Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the count of a hashtable that only has one key/value in it

I have a foreach loop that currently puts three entries in my hashtable:

$result = foreach($key in $serverSpace.Keys){
    if($serverSpace[$key] -lt 80){
        [pscustomobject]@{
            Server = $key
            Space = $serverSpace[$key]}}}

When I use

$result.count

I get 3 as expected. I changed the foreach loop to exlude the entries less than or equal to one using

$result = foreach($key in $serverSpace.Keys){
    if($serverSpace[$key] -lt 80 -and $serverSpace[$key] -gt 1){
        [pscustomobject]@{
            Server = $key
            Space = $serverSpace[$key]}}}

$result.count should have 1 as its output but it doesn't recognize .count as a suggested command and $result.count doesn't output anything anymore. I'm assuming when theres only one entry in the hash table it won't allow a count? Not sure whats going on but my conditions for my script are dependent on the count of $result. Any help would be appreciated.

like image 662
Tabiat1739 Avatar asked Nov 14 '25 10:11

Tabiat1739


1 Answers

$result is not a hashtable so I prefixed it with @($result).count. Thank you to @Theo and @Lee_Dailey

like image 136
Tabiat1739 Avatar answered Nov 17 '25 08:11

Tabiat1739



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!