简体   繁体   English

PowerShell-XML阵列问题

[英]PowerShell - XML Array Issue

So I wrote a powershell script to read in an xml file, parse through it, and output to a file after its been cleaned up. 因此,我编写了一个powershell脚本来读取xml文件,进行解析,并在清理后输出到文件。 My issue is that as it loops through the foreach loop it is storing it all to the same variables. 我的问题是,当它遍历foreach循环时,会将其全部存储到相同的变量中。

#Access the XML file
[xml]$XML= [xml](Get-Content $XmlPath)

foreach ($node in $XML.catalog.entry)
{
    #XML Variables
    $Username = $XML.catalog.entry.username 
    $Description = $XML.catalog.entry.description
    $Service = $XML.catalog.entry.service

    #Creating the syslog event that will be sent to SecureVue
    $writeOutput= "Username : [$Username] Description: [$Description] Service: [$Service]"

    #output
    $writeOutput
}

Unfortunately, the output I keep getting it: 不幸的是,我一直得到的输出:

Username : [User1 User2 User3 User4] Description: [Description1 Description2 Description3 Description4] Service: [Service1 Service2 Service3 Service4]
Username : [User1 User2 User3 User4] Description: [Description1 Description2 Description3 Description4] Service: [Service1 Service2 Service3 Service4]
Username : [User1 User2 User3 User4] Description: [Description1 Description2 Description3 Description4] Service: [Service1 Service2 Service3 Service4]
Username : [User1 User2 User3 User4] Description: [Description1 Description2 Description3 Description4] Service: [Service1 Service2 Service3 Service4]

Instead of: 代替:

Username : [User1] Description: [Description1] Service: [Service1]
Username : [User2] Description: [Description2] Service: [Service2]
Username : [User3] Description: [Description3] Service: [Service3]
Username : [User4] Description: [Description4] Service: [Service4]

I know i need some variation of: 我知道我需要一些变化:

foreach($node in $XML.ChildNodes)

But I can't seem to get it to work. 但是我似乎无法使其正常工作。 Any ideas on how to write out each line with the one array position for each node in the array? 关于如何为数组中的每个节点写出具有一个数组位置的每一行的任何想法?

Thank you! 谢谢!

As @Eris said, a sample of your XML file would make this easier to test, but at a glance, this part might be a good place to start: 就像@Eris所说的那样,您的XML文件样本将使其更易于测试,但是乍一看,这部分可能是一个不错的起点:

foreach ($node in $XML.catalog.entry)
{
    #XML Variables
    $Username = $XML.catalog.entry.username 
    $Description = $XML.catalog.entry.description
    $Service = $XML.catalog.entry.service

    ...
}

It looks like you're assigning these variables properties of the collection of nodes, when your intent is likely to assign properties of the individual nodes. 当您的意图可能是分配单个节点的属性时,您似乎正在分配节点集合的这些变量属性。 I'd try something like this, instead: 我会尝试这样的事情,而不是:

foreach ($node in $XML.catalog.entry)
{
    #XML Variables
    $Username = $node.username 
    $Description = $node.description
    $Service = $node.service

    ...
}

ie you want the properties of $node , not $XML.catalog.entry for the variables inside the loop. 即,您需要$node的属性,而不是循环内变量的$XML.catalog.entry属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM