0

I am trying to append the $date variable which should contain the ticket creation date to an existing array that contains open tickets:

$teamConfig = @(
    [pscustomobject]@{
        TeamName  = 'Team1'
        TeamEmail = 'team1@domain.tld'
    }
    [pscustomobject]@{
        TeamName  = 'Team2'
        TeamEmail = 'team2@domain.tld'
    }
)

$query = "select * from INCAutomation"

$results = Invoke-Sqlcmd -query $query -ServerInstance 'localhost' -Database 'AyushTest'

$teamTickets = foreach($team in $teamConfig) {
  # Filter tickets based on team name
  $ticketIds = $results |Where-Object TeamName -eq $team.TeamName |Select -ExpandProperty TicketID
  $date = $results |Where-Object TeamName -eq $team.TeamName |Select -ExpandProperty createdDate

    # Create an empty array
    $ticketIdArray = @()

    foreach ($id in $ticketIds) {
        $thisId = "
            <tr>
            <td>$($id)</td>
            <td>$($date)</td>
            </tr>
        "
        $ticketIdArray += $thisId
    }


  $team |Select TeamName,TeamEmail,@{Name='HTML';Expression={$ticketIdArray}}
}

$teamTickets.HTML

However this is the output I get (example):

<tr>
<td>INC0001</td>
<td>10/12/20 10/12/20 10/12/20 10/12/20</td>
</tr>
        

<tr>
<td>INC0002</td>
<td>10/12/20 10/12/20 10/12/20 10/12/20</td>
</tr>
        
<tr>
<td>INC0003</td>
<td>10/12/20 10/12/20 10/12/20 10/12/20</td>
</tr>

This is my desired output:

<tr>
<td>INC0001</td>
<td>10/12/20</td>
</tr>
        

<tr>
<td>INC0002</td>
<td>10/12/20</td>
</tr>
        
<tr>
<td>INC0003</td>
<td>10/12/20</td>
</tr>

It seems like for whatever reason the date is being appended based on the total number of teams.

Any ideas? Thanks in advance

yush
  • 365
  • 9
  • 26
  • 4
    Word of advice: separate _data modeling_ from _presentation_ - right now you're making choices about the final ouput format (HTML) in a context where you're really more interested in being able to meaningfully interrogate the data programatically – Mathias R. Jessen Dec 30 '20 at 13:35
  • hey @KemalK.no luck either, I got the following error: Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ExpandProperty'. Specified method | is not supported. – yush Dec 30 '20 at 13:36

3 Answers3

1

i can not see the result can you try this

$ticketIds = $results |Where-Object TeamName -eq $team.TeamName |Select TicketID,createdDate


# Create an empty array
$ticketIdArray = @()

foreach ($id in $ticketIds) {
    $thisId = "
        <tr>
        <td>$($id.TicketID)</td>
        <td>$($id.createdDate)</td>
        </tr>
    "
    $ticketIdArray += $thisId
}
Kemal K.
  • 301
  • 2
  • 7
1

Kemal's solution may work, but I suggest that you use .NET arraylists instead of PS arrays.

PS arrays are of fixed size, which prevents their ironically native add/remove and related methods from working.

Meanwhile, .NET arraylists are not of fixed size, so those methods work. There is a performance benefit for arraylist manipulation if the arrays are big.

Instead of $myArray = @(), use [collections.arraylist]$myArray = @().

Thereafter, you can simply execute $myArray.Add($thisId), $myArray.Remove($thatId), etc.

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
Dr. C.
  • 76
  • 6
1

As suggested by @ScripterMike and explained here, using the increase assignment operator (+=) to create a collection might get pretty expensive.
To avoid this, the correct PowerShell syntax is using the pipeline:

$ticketIdArray = foreach ($id in $ticketIds) {
     "
         <tr>
         <td>$($id.TicketID)</td>
         <td>$($id.createdDate)</td>
         </tr>
     "
 }
iRon
  • 20,463
  • 10
  • 53
  • 79