本文介绍了获取只有一个键/值的哈希表的计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 foreach 循环,目前在我的哈希表中放置了三个条目:

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]}}}

当我使用

$result.count

我按预期得到 3.我更改了 foreach 循环以使用

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 应该有 1 作为其输出,但它不会将 .count 识别为建议的命令,并且 $result.count 不再输出任何内容.我假设当哈希表中只有一个条目时,它不允许计数?不确定发生了什么,但我的脚本条件取决于 $result 的计数.任何帮助将不胜感激.

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

推荐答案

$result 不是哈希表,所以我在它前面加上了 @($result).count.感谢@Theo 和@Lee_Dailey

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

这篇关于获取只有一个键/值的哈希表的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:12