Powershell:如何仅使用 1 header 将值添加到 Object

Powershell: How to add values to Object with only 1 header

得到了一个 .ps,我从几个文件中得到了 alarmgroups。我试图将它们添加到 Object,但问题是每个新文件都在向 Object 添加另一个 header。有没有可能,只添加 header 1 次。将我的数据附加到 Object,当他完成对洞的排序时 Object?

我的代码。

$rootPath = $PSScriptRoot
if ($rootPath -eq "") {
    $rootPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
}

$alarmPath = "$rootPath\Alarmgroups"
$mdi_alarms_template = "$rootPath\tmpl\mdi-alarms.tmpl.html"
$mdi_alarms = "$rootPath\mdi-alarms.html"

$fileNames = Get-ChildItem -Path $alarmPath -Filter *.algrp

$AlarmgroupIndexString = $LanguageIDString = $tmpString = $ID_string = $html_output= ""
$MachineCode = "Out_2.Alarm.Current"
$BitNo = $Element = $Format2Element =  $Format3BitID  =0
$BitID = $Format3TextID = 1
$list  = New-Object System.Collections.ArrayList

Clear-Content "$rootPath\test.txt"
Clear-Content "$rootPath\list.txt"
Clear-Content "$rootPath\output.txt"

# Parse each alarm group file in the project
foreach ($file in $fileNames) {

    $Content = [xml](Get-Content -Path $file.FullName)

    $ns = New-Object System.Xml.XmlNamespaceManager($Content.NameTable)
    $ns=@{asdf="http://asdf-automation.co.at/AS/VC/Project"}

    $AlarmgroupIndex = Select-Xml -Xml $Content -XPath "//asdf[contains(@Name,'Index')]" -namespace $ns | select -ExpandProperty node
    $AlarmgroupIndexString = $AlarmgroupIndex.Value
    $AlarmgroupLanguageText = Select-Xml -Xml $Content -XPath "//asdf:TextLayer" -namespace $ns | select -ExpandProperty node
    $AlarmgroupIndexMap = Select-Xml -Xml $Content -XPath "//asdf:Index" -namespace $ns | select -ExpandProperty node
    
    $LUT =@{}  
    $AlarmgroupIndexMap | foreach{
        $LUT.($_.ID) = $_.Value
    }

    $tmpArray =@() 
    $list = $AlarmgroupLanguageText | foreach{ 

       $LanguageIDString = $_.LanguageId
        
            $AlarmgroupTextLayer = Select-Xml -Xml $Content -XPath "//asdf:TextLayer[@LanguageId='$LanguageIDString']/asdf:Text" -namespace $ns | select -ExpandProperty node 

            $AlarmgroupTextLayer | foreach{  
                if($LUT.ContainsKey($_.ID))
                {
                    $ID_string = $LUT[$_.ID]
                }
                
                [pscustomobject]@{
                    Language = $LanguageIDString
                    GroupID = $AlarmgroupIndexString
                    TextID = $ID_string #-as [int] 
                    Text = $_.Value
                }
                $ID_string =""
            }
        $LanguageIDString=""  
    }
    
    $list = $list |Sort-Object -Property  Language, GroupID, {$_.TextID -as [int]}
   
   # $list = $list |Sort-Object -Property @{Expression={$_.Language}}, @{Expression={$_.TextId}} , @{Expression={$_.TextID -as [int]}} 

    $list | Out-File "$rootPath\list.txt" -Append -Encoding utf8

输出:

GroupID Language TextID Text                                                                                                                                                         
------- -------- ------ ----                                                                                                                                                         
24      aa       Group                                                                                                                                                               
24      aa       0                                                                                                                                                                   
24      aa       1                                                                                                                                                                   
24      aa       2                                                                                                                                                                   
24      aa       3                                                                                                                                                                   
24      aa       4                                                                                                                                                                   
24      aa       5                                                                                                                                                                   
24      aa       6                                                                                                                                                                   
24      aa       7                                                                                                                                                                   
24      aa       8                                                                                                                                                                   
24      aa       9                                                                                                                                                                   
24      aa       10
GroupID Language TextID Text                                                                                                                                                         
------- -------- ------ ----                                                                                                                                                         
24      ar       Group                                                                                                                                                               
24      ar       0                                                                                                                                                                   
24      ar       1                                                                                                                                                                   
24      ar       2                                                                                                                                                                   
24      ar       3                                                                                                                                                                   
24      ar       4                                                                                                                                                                   
24      ar       5                                                                                                                                                                   
24      ar       6                                                                                                                                                                   
24      ar       7            

所以我的输出文件中有几个 header。是否可以删除它们或在没有 header 的情况下向 Object 添加元素。尝试了几种解决方案都没有用。 如果我理解正确,我会生成一个 Object 并将其添加到一个 object 中,其中包含所有值。 header.

[pscustomobject]@{
                    Language = $LanguageIDString
                    GroupID = $AlarmgroupIndexString
                    TextID = $ID_string #-as [int] 
                    Text = $_.Value

您可以将 foreach 循环的所有输出分配给单个变量,然后将 file-write 逻辑移动到脚本的末尾,您可以在其中一次输出所有内容:

# Assign results of entire `foreach(){}` statement to `$combinedLists`
$combinedLists = foreach ($file in $fileNames) {

    # XML navigation + object creation + assignment to `$list` still goes here

    # We sort and then instead of assigning the output to a variable directly,
    # we just let it "bubble up" from here to the `$combinedList` assignment
    $list |Sort-Object -Property  Language, GroupID, {$_.TextID -as [int]}
}

# Now we can write everything to file at once (overwrites existing contents)
$combinedLists |Out-File "$rootPath\list.txt" -Force -Encoding utf8