user2248618
user2248618

Reputation:

How to bind an array to an HTML tag in powershell?

Below is a snip of my powershell code where my response or my variable($witrevisions) is of type array. I am looking to bind this in a html tag which i have defined in the power shell. As I am very new to coding stuff , I am looking the ways how can I bind array to html tag in best possible way

...continuing my line of code

$response4s = (Invoke-RestMethod -Uri $uriAccount -Method get -Headers $AzureDevOpsAuthenicationHeader).values
$wits = $response4s | where({$_.fields.'System.WorkItemType' -eq 'Task'}) # Only retrieve Tasks
$witrevisions = @()
foreach($wit in $response4s){
    $customObject = new-object PSObject -property @{
      "Title" = $wit.fields.'System.Title'
      "AssignedTo" = $wit.fields.'System.AssignedTo'
 } 

    $witrevisions += $customObject      
             }
    $witrevisions | Select-Object `
              Title,                                         
              AssignedTo
}

and this the sample response i am getting in $witrevisions which i have exported in text file. its a table with two column one having emails and other having a title name.i have tried to show by giving it a table view for better understanding

 Assigned To                   Title
[email protected]                testingpro
[email protected]               resttesting

and here is the html tag where I trying to bind the $witrevisions.

$DOWNLOAD_PAGE_BODY_CONTENT = "<!DOCTYPE html>
    `n<html>
    `n<head>
    `n  <title>Validation</title>    
    `n</head>
    `n<body>
    `n
    `n<p>Please click the <a href=" + $($uploadResponse.webUrl) + ">link</a> to download the release.</p>
    `n<p></p>
    `n<p></p>
    `n<p>$witrevisions</p>
    `n</body>
    `n</html>
    `n" 

Can someone please tell me how should I do this??

Upvotes: 0

Views: 392

Answers (1)

Graham
Graham

Reputation: 640

Here is an example of some code that would take your array and emit a table, with an explanation to help you tweak to your specific needs:

"<table><body>$($witrevisions|% {"<tr><td>$($_.Title)</td><td>$($_.AssignedTo)</td></tr>"} )</body></table>"

The double quotes are important because they allow string interpolation (it will replace variables with this value, versus being read a plain text. E.g. '[' + $test + ']' => "[$test]"

If you need to do more complex logic in string interpolation, you can use $(...), the ellipses being regular code.

You can iterate through an array by piping to the ForEach-Object, or it's alias %. All the code in the braces will be executed for each item in the array. The current items is represented by $_.

We're then back to string interpolation and using $(...), which is needed to access the members of the current item.


Note: There are several other ways to accomplish (functionally) the same thing. E.g. foreach(...){} vs |%{...}, so feel free to use a different technique if you are more comfortable with doing something a different way.

Upvotes: 1

Related Questions