0

I am new to Powershell and am trying to get repeating output compatible to be able to use ConvertTo-Html. I have a string in a PSCustomObject as well as some arrays. I am trying to denomalize using the following however the title property is not repeating as I would expect like this

Edit - output below

Title        Comment
Hello World  hello
Hello World  bye

Edited as I missed the last select line (here I am expecting title to repeat on each line as the array expands)

$report =  @()
$col1 = @()
$col1 += "hello"
$col1 += "bye"

$col2 = @()
$col2 += "blue"
$col2 += "green"
$col2 += "red"

$reportinfo = New-Object PSCustomObject
$reportinfo | Add-Member NoteProperty -name Title -value [String]"Hello World"
$reportinfo | Add-Member NoteProperty -name Comment -value $col1
$reportinfo | Add-Member NoteProperty -name Colour -value $col2
$report += $reportinfo

$report | select Title -ExpandProperty Comment

This returns the following output

hello

bye

If I use

Write-Output $report

I get the following

Title Comment Colour
----- ------- ------
[String]Hello World {hello, bye} {blue, green, red}

I have tried both with a string cast and without. Any ideas would be greatly appreciated.

Mark Ruse
  • 387
  • 1
  • 4
  • 12

1 Answers1

0

Ok so it looks like the basic goal is to have the first column be all the same string, and then each additional column be your unique data correct? there are a few ways to do this but the best one is probably going to be just creating your objects that way rather than trying to move everything around. Right now when you create your object you are basically telling powershell "I want the title column to say 'Hello World' I want the comment column to contain this array @('hello','bye') and I want the color column to contain the array @('blue','green','red')" which is then what its doing. there are a few ways to get the objects created how you want but I've attached one of the simpler(if slower) ones, the only issue with it is that you wont be able to use heterogeneous arrays (so you'll need to either add a new comment or remove a color)

$report = @()
$col1 = @("hello","bye")
$col2 = @("red","green","blue")

$i = 0
foreach($entry in $col1) {
  $report += [PsCustomObject]@{
    Title = "Hello World"
    Comment = $entry
    Color = $col2[$i]
  }
  $i++
}

So basically what you are doing is looping through every entry in the $col1 array and creating a new object for each one with a Title property of "Hello World", a Comment property representing whatever that array entry is, and a Color property using whatever value is in the same position in the $col2 array, which you are keeping track of by incrementing $i Doing it with a regular FOR loop rather than a ForEach is probably more correct but the syntax is a bit more complex.(but it would be easier to modify this to handle unequal length arrays)

for($i = 0; $i -le $col1.count;$i++) {
  $report += [PsCustomObject]@{
    Title = "Hello World"
    Comment = $col1[$i]
    Color = $col2[$i]
  }
}

Hope that helps and please leave a comment if any part of it doesn't make sense or won't work for your situation.

Mike Garuccio
  • 2,588
  • 1
  • 11
  • 20