How to print object values in Powershell

Hi, can someone please help me on one powershell api call. I am using one API Get-BetaPersonalAccessTokens to display existing personal access token created in Sailpoint IDN

Get-BetaPersonalAccessTokens

Name                           Value
----                           -----
id                             xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
name                           PAT
scope                          {sp:scopes:all, sp:scopes:default}
created                        1/20/2024 5:14:35 PM
owner                          {[id, xxxxxxxxxxxxxxxxxxxxxxxxxxx], [type, IDENTITY], [name, Jimmy]}
managed                        False
lastUsed                       3/25/2024 1:27:19 PM

This API I am calling from one Powershell script :

$Result2 = Get-BetaPersonalAccessTokens |ConvertTo-Html -Fragment | out-string

Send-MailMessage -from $EmailFrom -to $EmailTo -subject $EmailSubject -Body $EmailBody -BodyAsHtml -SmtpServer

Can you please suggest how can I get proper value here for Scope and Owner.
example - Scope as sp:scopes:all, sp:scopes:default
Owner as Jimmy

@tyler_mairose @darrenjrobinson @dmbrown1386

When you convert complex or multivalued Objects to a string in PowerShell it will display as the Object’s Type instead of it’s content. I would first try removing the Out-String Pipe and change the Body Parameter from -Body $EmailBody to -Body "$EmailBody" . I’m assuming $EmailBody contains the $Result2 variable?

But even when using the ConvertTo commands you’ll see similar behavior. And Owner would capture all attributes so you may need to create a PSCustomObject to get the output looking the way you want. Just make sure anything you want treated a string that is not a string is encapsulated in quotes. So for Scopres you would write that as "$($Result2.scope)" to force it to a string in your Custom Object.

Let me know if you have any other questions.

1 Like

Hi Derek @dmbrown1386 ,
Thanks for your response. I tried suggestion provided by you but looks like I am doing some mistake. If its possible, could you please provide sample modified code as per my case, may be this will be helpful for many users for reference.

Hey @hranjan3!

The reason this is occurring is because the value of the scope property is an array, which goes unhandled when converting to HTML without instructions to expand that array. Below is a quick script I threw together for you to handle this. Feel free to modify as you see fit. This will generate the HTML data for you. All Scopes will be joined together with a comma.

Script

$data = Get-BetaPersonalAccessTokens | ForEach-Object {
    New-Object PSCustomObject -Property ([ORDERED] @{
        "ID"  = $_.id;
        "Name"  = $_.name;
        "Scopes" = $($_.scope -Join ", ");
        "Created" = $_.created;
        "Owner" = "Type = '$($_.owner.type)', Name = '$($_.owner.name)', ID = '$($_.owner.id)'";
        "Managed" = $_.managed;
        "Last Used" = $_.lastUsed
    })
}

$htmlData = $data | ConvertTo-HTML

What you should see as the HTML Result

Source Code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
<table>
<colgroup><col/><col/><col/><col/><col/><col/><col/></colgroup>
<tr><th>ID</th><th>Name</th><th>Scopes</th><th>Created</th><th>Owner</th><th>Managed</th><th>Last Used</th></tr>
<tr><td>[REDACTED]</td><td>postman</td><td>sp:scopes:all</td><td>2/27/2024 6:02:29 PM</td><td>Type = &#39;IDENTITY&#39;, Name = &#39;brennen.scott&#39;, ID = &#39;[REDACTED]c&#39;</td><td>False</td><td>3/25/2024 3:36:15 PM</td></tr>
<tr><td>[REDACTED]</td><td>Automation Test</td><td>idn:accounts:manage, idn:accounts:read</td><td>3/25/2024 6:32:14 PM</td><td>Type = &#39;IDENTITY&#39;, Name = &#39;brennen.scott&#39;, ID = &#39;[REACTED]&#39;</td><td>False</td><td></td></tr>
</table>
</body></html>

Actual HTML Table

1 Like

Hi Brennen,

Thank you so much for your response! Solution you provided worked like charm!

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.