Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

powershell - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...