获取powershell中哈希表的长度
我是powershell新手,尝试获取哈希表的长度(用于for循环),但似乎无法获取哈希表的长度以输出任何内容获取powershell中哈希表的长度,powershell,Powershell,我是powershell新手,尝试获取哈希表的长度(用于for循环),但似乎无法获取哈希表的长度以输出任何内容 $user = @{} $user[0] = @{} $user[0]["name"] = "bswinnerton" $user[0]["car"] = "honda" $user[1] = @{} $user[1]["name"] = "jschmoe" $user[1]["car"] = "mazda" write-output $user.length #nothing
$user = @{}
$user[0] = @{}
$user[0]["name"] = "bswinnerton"
$user[0]["car"] = "honda"
$user[1] = @{}
$user[1]["name"] = "jschmoe"
$user[1]["car"] = "mazda"
write-output $user.length #nothing outputs here
for ($i = 0; $i -lt $user.length; $i++)
{
#write-output $user[0]["name"]
}
$user
是一个哈希表,因此您应该使用user$user.count
。{}
声明一个哈希表,而@()
声明一个数组
你可以用
$user.count
要查找您的哈希表的长度
如果您这样做:
$user | get-member
您可以查看对象的所有方法和属性
$user.gettype()
返回您拥有的对象的类型。这不是数组,而是哈希表。改为使用.count
:
write-output $user.count
+1用于指出get成员。不知怎的,我从来没有学过这一点,这将使事情在未来变得更容易。