0

Got a .ps where im getting alarmgroups from several files. Im trying to add them to an Object but the Problem is every new file hes adding another header into the Object. Is there a possibility, adding the header only 1 time. Append my data to the Object and when hes finished sorting the hole Object?

My Code.

$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

Output:

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            

So i have several headers in my outputfile. Is it possible to erase them or add elements to the Object without the header. Tried several solution nothing worked. If i understand it correctly im generating an Object and add it to an object with all values incl. header.

[pscustomobject]@{
                    Language = $LanguageIDString
                    GroupID = $AlarmgroupIndexString
                    TextID = $ID_string #-as [int] 
                    Text = $_.Value
1
  • What is the purpose of outputting these objects to disk? I'd strongly suggest using a machine-readable format (ie. CSV) for storing the data, unless the sole purpose of the output files is for actual humans to read them Commented Aug 26, 2020 at 13:35

1 Answer 1

1

You can assign all the output from the foreach loop to a single variable and then move the file-write logic to the end of the script where you can output it all at once:

# 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
Sign up to request clarification or add additional context in comments.

1 Comment

Works! Thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.