Home > database >  Add multiple values to headers in Powershell
Add multiple values to headers in Powershell

Time:04-22

I have a bit of code, that runs through a few foreach loops.

foreach ($Result in $ResultsCollection)
{

      $Result.Get()        

      [xml]$sdmPackageXml  = New-Object system.Xml.XmlDocument
           $sdmPackageXml.LoadXml($Result.Properties["SDMPackageXML"].Value)

          Write-host "########"
          write-host "LocalizedDisplayName       = " $Result.LocalizedDisplayName
          write-host "PackageID                  = " $Result.PackageID
          write-host "CI_ID                      = " $Result.CI_ID
          write-host "SoftwareVersion            = " $Result.SoftwareVersion
          write-host "Number of Deployment Types = " $Result.NumberOfDeploymentTypes


          foreach ($node in $sdmPackageXml.AppMgmtDigest.DeploymentType)
          {
             write-host "   DT Name                 = " $node.LogicalName

             foreach ($requirement in $node.Requirements)
             {
               foreach ($rule in $requirement.Rule)
               {
          

                 write-host "   Rule                    = " $requirement.Rule.Annotation.DisplayName.Text                  
               }

         }
      }

          write-host ""
}

I would like to add a few headers, so that it easily can be exported to a .csv file, like so:

LocalizedDisplayName   PackagedID    CI_ID   SoftwareVersion

How would it be easiest to achieve that?

I tried using an array and hashtable, but I couldn't really make that work.

Any feedback/help is much appreciated!

CodePudding user response:

You can create a custom object which you add to an array on every foreach loop. Do this to the last foreach loop e.g.

                   $objarray = @() #clear objarray on every run

                   foreach ($rule in $requirement.Rule)
                     $objArray  = [PSCustomObject]@{
                       'LocalizedDisplayName' = $Somevalue
                        'PackageId' = $someothervalue
                     }
                   }

                   $objarray #Display table
  • Related