-powershell哈希玛普:嵌套过滤

问题描述 投票:0回答:1

above让我可以轻松地在特定服务器上获得服务器,服务的列表,但更重要的是,它提供了一种简单的方法,可以返回过滤器降低的hashmap来获取所有信息,例如。在特定的服务器等上

write-host "list of servers:"; $test.keys; write-host "list of services on server1:"; $test['server1'].keys; write-host "hashmap of services on server1:"; $test['server1']; list of servers: server2 server1 list of services on server1: service2 service1 hashmap of services on server1: Name Value ---- ----- service2 {serviceName, processName, properties1, group…} service1 {serviceName, processName, properties1, group…} hashmap of SUBSET services: server2 {service3} server1 {service1}
我不确定是否有一种优雅的方法来执行更深入的量身定制的过滤器,例如,将哈希图筛选到仅在组名称中的子集等的服务等。
write-host "hashmap of SUBSET services:"; #expected result @{ "server1"=@{ "service1"=@{ "filePath"="C:\hi"; "serviceName"="servName1"; "processName"="procName1" ; "group"=@("ALL", "SUBSET" ); "properties1"=@{"prop1"="prop2";}; "properties2"=@{"prop1"="prop2";}; }; }; "server2"=@{ "service3"=@{ "filePath"="C:\hi3"; "serviceName"="servName3"; "processName"="procName3" ; "group"=@("ALL", "SUBSET" ) "properties1"=@{"prop1"="prop2";}; "properties2"=@{"prop1"="prop2";}; }; }; };

我只能想到的只是一个庞大的笨拙的嵌套系列,用于循环,如果我想执行其他过滤器,例如fet hashmap of hashmap a hashmap a hashmap,这可能很难重复使用。

有人有任何想法吗?如果我可以轻松地访问$ test ['server1']而没有第一个级别的循环,那么有一种方法可以为嵌套字段做同样的事情,例如
$test(*)(*)[“group”].contains(“SUBSET”)

感谢您的评论中的额外信息。
如果只需列出将关键字“子集”作为其“组”项目数组中的值之一的服务的过滤结果,则可以这样做:

$filtered = @{} # a new Hashtable object to store the results foreach ($server in $test.Keys) { # loop over the items in the original Hash foreach ($service in $test.$server.Keys) { if ($test.$server.$service.group -contains 'SUBSET') { $filtered.Add($server,@{$service = $test.$server.$service}) } } }

现在,

$filtered
仅包含服务器及其服务中包含“子集”的服务:
powershell hashmap hashtable
1个回答
0
投票


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.