Convert Json response in html tabular view in Powershell

Below is my PowerShell code snippet which is returning me values in three columns in the below format. I am trying to show this response by binding it in an html tag but the view is not appropriate. I tried different other options but not getting a proper tabular view:

Function GET-AllAboutRelease{
    $responseRels = Invoke-RestMethod -Uri $uriAccount -Method get -Headers $AzureDevOpsAuthenicationHeader
    $artifactalias = @()
    foreach($art in $responseRels){
        $customartifacts = new-object PSObject -property @{
            "AID" = $art.artifacts.'alias'
            "Atype" = $art.artifacts.'type'
            "versaid" = $art.artifacts.definitionReference.version.'name'
        }
        $artifactalias += $customartifacts
    }
    $artifactalias | Select-Object AID, Atype, versaid
}

The response I get in my debugger for artifactalias is something like this

Atype {pkgmanag, git, git, build} versaid {1.0,2.0,7.0,9.0} aid {name1,name2,name3,name4}

below is the powershell function where i m constructing HTML code

Function CREATE-SECTION { #TODO- code improvement for following variable $myglob= GET-AllAboutRelease $DOWNLOAD_PAGE_BODY_CONTENT = &quot;</code>n<code>n</code>n<code>n</code>n</code>n

Please click the <a href=" + $($uploadResponse.webUrl) + ">link</a> to download the release.

<code>n
<table>
<tbody>
<tr>
<td>Artifactname</td>
<td>Artifact Type</td>
<td>Artifact Version</td>
</tr>
</tbody>
</table>
</code>

n

<code>n

$($myglob|% {"

"})
<table>
<tbody>
<tr>
<td>$($_.AID)</td>
<td>$($_.Atype)</td>
<td>$($_.versaid)</td>
</tr>
</tbody>
</table>
</code>

n

<table>
<tbody>
<tr>
<td>$($artifactalias)</td>
</tr>
</tbody>
</table>
`n

`n`n`n" $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add("Authorization", "Bearer "+ $tokenObject.access_token) $headers.Add("Content-Type", "application/json") $body = "{`n"displayName`&""$release_uuid&"

HTML which is generated is something like this:

Artifactname Artifact Type Artifact Version

name1 nam2 name3 name4 Pkgmanag Git Git Build 1.0 2.0 7.0 9.0

i would like this to be in a more tabular structure something like this:

artifact name artifact type artifact version
name1 Pkgmanag 1.0
name2 git 2.0

The post is a bit messy above, but here is a basic idea of how you would return a flattened object:

function Get-Artifact {
    param (
       $Uri,
       $Header 
    )
    begin {}
    process {
        $responseRels = Invoke-RestMethod -Uri $Uri -Method GET -Headers $Header 
        
        $artifacts = foreach ( $art in $responseRels ) {
            New-Object PSObject -Property @{
                'AID'     = $art.artifacts.alias
                'Atype'   = $art.artifacts.type
                'versaid' = $art.artifacts.definitionReference.version.name
            }
        }
    }
    end {
        $artifacts
    }
}


$headers = @{
    “Authorization” = “Bearer “+ $tokenObject.access_token
    “Content-Type” = “application/json”
}

$artifacts = Get-Artifact -Uri 'https://myapi.com/artifacts' -Header $headers

$html = @"
<html>
<head></head>
<body>
    $($artifacts | ConvertTo-Html -Fragment)
</body>
</html>
"@

Understatement of the year.

@ankitkum69 : I’ve attempted to repair your post, but because your script includes HTML I’m not entirely sure which parts are the script and which parts are broken formatting tags, so I don’t think I got everything right. Please read the Guide to Posting Code and edit the post as necessary.

Hi Rob,

The code above give me response in write-host @html as this :

<html>
<head></head>
<body>
<colgroup><col/><col/><col/></colgroup>

Atype versaid AID
System.Object System.Object System.Object

</body>
</html>

I am not able to get the values in the html as I can see in $artifacts:
$artifacts =

Atype Versaid AID
{Pkgmang,Git,Build…} {1.0,2.0,7.0…} {Name1,Name2,Name3…}

This is the final code I ran, which I believe is similar to yours:

Function GET-AllAboutRelease{

$AzureDevOpsPAT = "witesten7powtz47cqa"
$OrganizationName = "testu"
$AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($AzureDevOpsPAT)")) }
$orz = "https://vsrm.dev.azure.com/$($OrganizationName)/Test/"
$uriAccount = $orz + "_apis/release/releases/1914?api-version=6.0"
$responseRels = Invoke-RestMethod -Uri $uriAccount -Method get -Headers $AzureDevOpsAuthenicationHeader 
$artifacts = foreach ( $art in $responseRels ) {
            New-Object PSObject -Property @{
                'AID'     = $art.artifacts.alias
                'Atype'   = $art.artifacts.type
                'versaid' = $art.artifacts.definitionReference.version.name
            }
        }
        return $artifacts
}
Function CREATE-SECTION
{
    $myrel = GET-AllAboutRelease
    $html = @"
&lt;html&gt;
&lt;head&gt;&lt;/head&gt;
&lt;body&gt;
    $($myrel | ConvertTo-Html -Fragment)
&lt;/body&gt;
&lt;/html&gt;
"@
Write-Host $html
}

@rob when I am trying to post its not appearing.

Please don’t double post… or in this case quintuple post… give the moderators a chance to handle the issue.

I am not sure why this is happening, but when I am posting it, It is not showing up and so i have to repost. Neither it is allowing me to insert any image.

Your posts are being automatically marked as spam by Akismet, most likely because they contain web code.

Reposting the same content does not circumvent the spam filtering in any way - all five of your attempted posts were filtered. When the posts are filtered, moderators can view and approve them so that they show up (they are not lost, just held for review). Reposting just creates a larger mess that the moderators have to clean up.

This forum does not have image hosting. If you want to include an image in a post, it must be hosted on another website (such as imgur) and then you can insert it with the tag like so:

We cannot see the json structure being returned, but you can try this. Make sure the function is returning the correct data before working on the HTML aspects:

$artifacts = foreach ( $art in $responseRels.artifacts ) {
            New-Object PSObject -Property @{
                'AID'     = $art.alias
                'Atype'   = $art.type
                'versaid' = $art.definitionReference.version.name
            }
        }
 return $artifacts