Powershell 通过 xml 循环创建一个锯齿状数组 [英] Powershell loop through xml to create a jagged array

查看:28
本文介绍了Powershell 通过 xml 循环创建一个锯齿状数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我猜这很简单,我刚开始使用 powershell,在网上搜索了几个小时后,我似乎找不到简单的解决方案.

I'm guessing this is pretty simple, I'm just starting with powershell and I can't seem to find a simple solution after a few hours searching the net.

基本上我有以下xml文件

Basically I have the following xml file

<Config>
<System>
<Server>
    <Name>host1</Name> 
    <Service>service1</Service>
    <Service>service2</Service>
</Server>
<Server>
    <Name>host2</Name> 
    <Service>service1</Service>
</Server>
</System>

<System2>
<Server>
    <Name>host1</Name> 
    <Service>service1</Service>
    <Service>service2</Service>
</Server>
<Server>
    <Name>host2</Name> 
    <Service>service1</Service>
     <Service>service2</Service>
     <Service>service3</Service>

</Server>
</System2>
</Config>

我想将内容放入一个锯齿状的二维数组中,因此系统将如下所示:

And I would like to get the contents into a jagged 2d array, so system would wind up as follows:

  • (host1),(service1,service2)
  • (host2),(service1)

是否可以遍历每个元素并创建这样的数组?

Is it possible to iterate through each element and create such an array?

我希望像上面一样存储数据,以便我可以使用 [0][0] 来引用主机名,然后使用 [1][1..x] 来引用该主机的所有服务.

I'm hoping to store the data as above so that I can use [0][0] to reference the hostname and then [1][1..x] to reference all services for that host.

这样有意义吗?

非常感谢任何帮助或更好的方法.

Any help or better appraoch is greatly appreciated.

推荐答案

您可能会考虑为此使用哈希表.您仍然可以迭代结果,并且可以通过主机名查找结果,例如:

You might think about using a hashtable for this instead. You can still iterate over the results and you can look up results by hostname e.g.:

$xml = [xml]@'
    <Config>
    <System>
    <Server>
        <Name>host1</Name> 
        <Service>service1</Service>
        <Service>service2</Service>
    </Server>
    <Server>
        <Name>host2</Name> 
        <Service>service1</Service>
    </Server>
    </System>

    <System2>
    <Server>
        <Name>host1</Name> 
        <Service>service1</Service>
        <Service>service2</Service>
    </Server>
    <Server>
        <Name>host2</Name> 
        <Service>service1</Service>
        <Service>service2</Service>
        <Service>service3</Service>
    </Server>
    </System2>
    </Config>
'@

$ht = @{}
$hostnameNodes = $xml | Select-Xml -XPath '//Name'
foreach ($node in @($xml | Select-Xml -XPath '//Name'))
{
    $hostname = $node.Node.InnerText.Trim()
    $services = @($node.Node.ParentNode.Service)
    foreach ($service in $services)
    {
        $ht["$hostname"] += @($service.Trim())
    }
}

$ht

输出:

Name                           Value                                    
----                           -----                                    
host1                          {service1, service2, service1, service2} 
host2                          {service1, service1, service2, service3} 

这篇关于Powershell 通过 xml 循环创建一个锯齿状数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆