# Powershell API script print object data

**URL:** <https://developer.sailpoint.com/discuss/t/powershell-api-script-print-object-data/57896>\
**Category:** SHF Discussion and Questions\
**Tags:** powershell-sdk, apis, identity-security-cloud\
**Created:** [May 24, 2024, 9:15am UTC](https://developer.sailpoint.com/discuss/t/powershell-api-script-print-object-data/57896 "2024-05-24T09:15:51Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![hranjan3](https://avatars.discourse-cdn.com/v4/letter/h/ecb155/32.png) [@hranjan3](https://developer.sailpoint.com/discuss/u/hranjan3)\
**Post date:** [May 24, 2024, 9:15am UTC](https://developer.sailpoint.com/discuss/t/powershell-api-script-print-object-data/57896/1 "2024-05-24T09:15:51Z")

</div>

Hi, I am looking one help on powershell script where I am calling one search query which is giving me output in string and object format. I am able to print some values but for some its not showing correct data. Could you please review this and let me know what I need to correct.

```auto
#requester Value : {[name, Test], [id, e2045hj7565ce345b63a3050c], [type, Identity]}
#sources Value : Active Directory, IdentityNow
#accountRequests Value : {012345, CN=012345,OU=Users,OU=US,OU=Accounts,OU=NA,OU=Regions,DC=AD,DC=ORG,DC=Net}
#originalRequests Value : {012345}
#expansionItems Value : {CompanyName}
#recipient Value : {[name, Robert J], [id, 89rer545fghfg2a9bec4cdf00cceb5], [type, Identity]}

```

 ![image](https://global.discourse-cdn.com/sailpoint/original/2X/2/2ef6c614f22fc1a297a60c597a2b4eb3486d7cf8.png)

```auto
$data2 = Search-Post -Search $Search | ForEach-Object {
    New-Object PSCustomObject -Property ([ORDERED] @{
        "Action" = $_.action;
		"Requester" = "$($_.requester.name)";
		"Recipient" = "$($_.recipient.name)";
		"Account ID" = $_.originalRequests;
		"expansionItems" = $_.expansionItems;
		"Sources" = $_.sources;
		"Stage" = $_.stage;
		"Created" = $_.created;
		"Modified" = $_.modified;		

		
    })
}

```

How can I print correct value for Account ID and expansionItems column which is showing value as System.Object

---

<div class="post-metadata">

**Author:** ![Sai\_Sumanth\_Golla](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@Sai\_Sumanth\_Golla](https://developer.sailpoint.com/discuss/u/Sai_Sumanth_Golla)\
**Post date:** [May 24, 2024, 9:40am UTC](https://developer.sailpoint.com/discuss/t/powershell-api-script-print-object-data/57896/2 "2024-05-24T09:40:07Z")

</div>

Here’s a modification in the script that handles the array-like nature of these properties and joins the elements into a comma-separated string, please try this

```auto
$data2 = Search-Post -Search $Search | ForEach-Object {
    New-Object PSCustomObject -Property ([ORDERED] @{
        "Action" = $_.action
        "Requester" = $_.requester.name 
        "Recipient" = $_.recipient.name
        "Account ID" = $_.originalRequests -join ', '  
        "expansionItems" = $_.expansionItems -join ', '  
        "Sources" = $_.sources -join ', '  
        "Stage" = $_.stage
        "Created" = $_.created
        "Modified" = $_.modified
    })
}

```

---

<div class="post-metadata">

**Author:** ![Karthikeyan\_U](https://sea1.discourse-cdn.com/sailpoint/discuss/user_avatar/developer.sailpoint.com/karthikeyan_u/32/23649_2.png) [@Karthikeyan\_U](https://developer.sailpoint.com/discuss/u/Karthikeyan_U)\
**Post date:** [May 24, 2024, 9:40am UTC](https://developer.sailpoint.com/discuss/t/powershell-api-script-print-object-data/57896/3 "2024-05-24T09:40:59Z")

</div>

Hi Himanshu,  
You can use foreach to iterate over the array like $\_.accountRequests[0] by this way you get the required value.  
Thanks!!

---

<div class="post-metadata">

**Author:** ![hranjan3](https://avatars.discourse-cdn.com/v4/letter/h/ecb155/32.png) [@hranjan3](https://developer.sailpoint.com/discuss/u/hranjan3)\
**Post date:** [May 24, 2024, 9:56am UTC](https://developer.sailpoint.com/discuss/t/powershell-api-script-print-object-data/57896/4 "2024-05-24T09:56:38Z")

</div>

Hi Sai and Karthikeyan,

I think I should add one more input then it will be useful to fix the issue. See when I am using this code:

```auto
$data2 = Search-Post -Search $Search | ForEach-Object {
    New-Object PSCustomObject -Property ([ORDERED] @{
        "Action" = $_.action;
		"Requester" = "$($_.requester.name)";
		"Recipient" = "$($_.recipient.name)";
		"Account ID" = $_.originalRequests;
		"expansionItems" = $_.expansionItems;
		"Sources" = $_.sources;
		"Stage" = $_.stage;
		"Created" = $_.created;
		"Modified" = $_.modified;		

		
    })
}

```

in Powershell console I am able to view the output as :

![image](https://global.discourse-cdn.com/sailpoint/original/2X/5/50932f5ffdba1d17d08ac7e679a5ae2df415dea1.png)

and while I am exporting the result data to csv, then in csv two column values are showing as "` System.Object[]`"

 ![image](https://global.discourse-cdn.com/sailpoint/original/2X/6/6123f5e61b857b0398c5a7282fb7d1422a97c503.png)

and this is how I am exporting the csv file:

```auto
$attachFile = $data2 | ConvertTo-Csv -UseCulture -NoTypeInformation | Out-File "C:\Users\TestWF1.csv"

```

now could you please review this case and suggest me fix.

---

<div class="post-metadata">

**Author:** ![hranjan3](https://avatars.discourse-cdn.com/v4/letter/h/ecb155/32.png) [@hranjan3](https://developer.sailpoint.com/discuss/u/hranjan3)\
**Post date:** [May 24, 2024, 9:58am UTC](https://developer.sailpoint.com/discuss/t/powershell-api-script-print-object-data/57896/5 "2024-05-24T09:58:11Z")

</div>

Account ID and expansionItems coulmn values has { } not sure how can I handle this while exporting the csv.

---

<div class="post-metadata">

**Author:** ![hranjan3](https://avatars.discourse-cdn.com/v4/letter/h/ecb155/32.png) [@hranjan3](https://developer.sailpoint.com/discuss/u/hranjan3)\
**Post date:** [May 24, 2024, 10:29am UTC](https://developer.sailpoint.com/discuss/t/powershell-api-script-print-object-data/57896/6 "2024-05-24T10:29:13Z")

</div>

> [@Sai\_Sumanth\_Golla](#):
>
> `-join`

@Sai_Sumanth_Golla I tried to use join for one attribute, but it showed data as

`"Account ID" = $_.originalRequests -join ',';`

`Account ID : System.Management.Automation.OrderedHashtable`

---

<div class="post-metadata">

**Author:** ![Sai\_Sumanth\_Golla](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@Sai\_Sumanth\_Golla](https://developer.sailpoint.com/discuss/u/Sai_Sumanth_Golla)\
**Post date:** [May 24, 2024, 10:49am UTC](https://developer.sailpoint.com/discuss/t/powershell-api-script-print-object-data/57896/7 "2024-05-24T10:49:20Z")

</div>

You can give this a try, change file path and name

```auto
$data2 = Search-Post -Search $Search | ForEach-Object {
    New-Object PSCustomObject -Property ([ORDERED] @{
        "Action" = $_.action
        "Requester" = $_.requester.name 
        "Recipient" = $_.recipient.name
        "Account ID" = ($_.originalRequests -join ', ')
        "expansionItems" = ($_.expansionItems -join ', ')
        "Sources" = ($_.sources -join ', ')
        "Stage" = $_.stage
        "Created" = $_.created
        "Modified" = $_.modified
    })
}

# Export to CSV
$data2 | Export-Csv -Path "C:\Users\Test.csv" -NoTypeInformation

```

---

<div class="post-metadata">

**Author:** ![hranjan3](https://avatars.discourse-cdn.com/v4/letter/h/ecb155/32.png) [@hranjan3](https://developer.sailpoint.com/discuss/u/hranjan3)\
**Post date:** [May 24, 2024, 12:12pm UTC](https://developer.sailpoint.com/discuss/t/powershell-api-script-print-object-data/57896/8 "2024-05-24T12:12:31Z")

</div>

> [@Sai\_Sumanth\_Golla](#):
>
> `$data2 | Export-Csv -Path "C:\Users\Test.csv" -NoTypeInformation`

I tried but its giving same output

Account ID : System.Management.Automation.OrderedHashtable  
expansionItems : System.Management.Automation.OrderedHashtable

---

<div class="post-metadata">

**Author:** ![baoussounda](https://sea1.discourse-cdn.com/sailpoint/discuss/user_avatar/developer.sailpoint.com/baoussounda/32/13095_2.png) [@baoussounda](https://developer.sailpoint.com/discuss/u/baoussounda)\
**Post date:** [May 24, 2024, 12:36pm UTC](https://developer.sailpoint.com/discuss/t/powershell-api-script-print-object-data/57896/9 "2024-05-24T12:36:41Z")

</div>

> [@hranjan3](#):
>
> Account ID : System.Management.Automation.OrderedHashtable  
> expansionItems : System.Management.Automation.OrderedHashtable

Hi @hranjan3,

can you try this ?

```auto
$data2 = Search-Post -Search $Search | ForEach-Object {
    # Handle Account ID as an OrderedHashtable
    if ($_.originalRequests -is [System.Collections.Hashtable]) {
        $originalRequestsString = ($_.originalRequests.GetEnumerator() | ForEach-Object { "$($_.Key): $($_.Value)" }) -join ", "
    } else {
        $originalRequestsString = $_.originalRequests -join ", "
    }

    # Handle expansionItems as an OrderedHashtable
    if ($_.expansionItems -is [System.Collections.Hashtable]) {
        $expansionItemsString = ($_.expansionItems.GetEnumerator() | ForEach-Object { "$($_.Key): $($_.Value)" }) -join ", "
    } else {
        $expansionItemsString = $_.expansionItems -join ", "
    }
    
    New-Object PSCustomObject -Property ([ORDERED] @{
        "Action" = $_.action;
        "Requester" = "$($_.requester.name)";
        "Recipient" = "$($_.recipient.name)";
        "Account ID" = $originalRequestsString;
        "expansionItems" = $expansionItemsString;
        "Sources" = ($_.sources -join ", ");
        "Stage" = $_.stage;
        "Created" = $_.created;
        "Modified" = $_.modified;
    })
}

# Define the output file path
$outputFile = "output.csv"

# Export the results to a CSV file
$data2 | Export-Csv -Path $outputFile -NoTypeInformation -Force

Write-Output "Results have been stored in the file $outputFile"

```

---

<div class="post-metadata">

**Author:** ![hranjan3](https://avatars.discourse-cdn.com/v4/letter/h/ecb155/32.png) [@hranjan3](https://developer.sailpoint.com/discuss/u/hranjan3)\
**Post date:** [May 24, 2024, 12:55pm UTC](https://developer.sailpoint.com/discuss/t/powershell-api-script-print-object-data/57896/10 "2024-05-24T12:55:41Z")

</div>

Hello @baoussounda Thanks for your reponse. I tried the modified code but unfortunately its still showing value as “System.Management.Automation.OrderedHashtable”

 ![image](https://global.discourse-cdn.com/sailpoint/original/2X/d/d6c03f112c0c2e4c7c459e026bedc6e54dadcd7b.png)

in powershell query output I see value for these two attributes as :

Key : originalRequests  
Value : {12345}  
Name : originalRequests

Key : expansionItems  
Value : {Technology}  
Name : expansionItems

---

<div class="post-metadata">

**Author:** ![baoussounda](https://sea1.discourse-cdn.com/sailpoint/discuss/user_avatar/developer.sailpoint.com/baoussounda/32/13095_2.png) [@baoussounda](https://developer.sailpoint.com/discuss/u/baoussounda)\
**Post date:** [May 24, 2024, 12:58pm UTC](https://developer.sailpoint.com/discuss/t/powershell-api-script-print-object-data/57896/11 "2024-05-24T12:58:37Z")

</div>

@hranjan3 it’s seem like this attributes are hashtables, try this :

```auto
$data2 = Search-Post -Search $Search | ForEach-Object {
    if ($_.originalRequests -is [System.Collections.Hashtable]) {
        $originalRequestsString = ($_.originalRequests.Values -join ", ")
    } else {
        $originalRequestsString = $_.originalRequests -join ", "
    }

    if ($_.expansionItems -is [System.Collections.Hashtable]) {
        $expansionItemsString = ($_.expansionItems.Values -join ", ")
    } else {
        $expansionItemsString = $_.expansionItems -join ", "
    }
    
    New-Object PSCustomObject -Property ([ORDERED] @{
        "Action" = $_.action;
        "Requester" = "$($_.requester.name)";
        "Recipient" = "$($_.recipient.name)";
        "Account ID" = $originalRequestsString;
        "expansionItems" = $expansionItemsString;
        "Sources" = ($_.sources -join ", ");
        "Stage" = $_.stage;
        "Created" = $_.created;
        "Modified" = $_.modified;
    })
}

# Define the output file path
$outputFile = "output.csv"

# Export the results to a CSV file
$data2 | Export-Csv -Path $outputFile -NoTypeInformation -Force

Write-Output "Results have been stored in the file $outputFile"

```

---

<div class="post-metadata">

**Author:** ![hranjan3](https://avatars.discourse-cdn.com/v4/letter/h/ecb155/32.png) [@hranjan3](https://developer.sailpoint.com/discuss/u/hranjan3)\
**Post date:** [May 24, 2024, 2:44pm UTC](https://developer.sailpoint.com/discuss/t/powershell-api-script-print-object-data/57896/12 "2024-05-24T14:44:46Z")

</div>

@baoussounda Still its giving same error System.Management.Automation.OrderedHashtable

[JSONOutput.txt](https://developer.sailpoint.com/discuss/uploads/short-url/em85kda5AMlMea2WX3tlfNobpGp.txt) (2.1 KB)

I have attached the json output text, if possible could you please try it from your end.

---

<div class="post-metadata">

**Author:** ![brennenscott](https://sea1.discourse-cdn.com/sailpoint/discuss/user_avatar/developer.sailpoint.com/brennenscott/32/23642_2.png) [@brennenscott](https://developer.sailpoint.com/discuss/u/brennenscott)\
**Post date:** [May 24, 2024, 3:21pm UTC](https://developer.sailpoint.com/discuss/t/powershell-api-script-print-object-data/57896/13 "2024-05-24T15:21:24Z")

</div>

Hi @hranjan3, it looks like some of the suggestions here are on the right track, but aren’t quite correct. The `-Join` command is only for arrays, and will not work for Hashtables (Objects in PowerShell). Additionally, each object must be handled at every level. It looks like you are struggling with the nested objects in your JSON response.

One quick way to get the objects in string form is to convert the format to JSON.

```auto
$data2 | ConvertTo-Json -Depth 100 # You can define how many levels of nesting you want to expand; 100 is the max.

```

This will show the entire object and all nested objects in JSON format, but this may not be “clean” to you. If you need it to be in a clean, organized format, you have to handle each object and all nested objects.

This should get you started in the right direction:

```auto
$data2 = Search-Post -Search $Search | ForEach-Object {
    New-Object PSCustomObject -Property ([ORDERED] @{
        # ACTION - This is a string and can retrieved without any specific formatting or adjustment
        Action = $_.action;
        # REQUESTER - This is an object and requires manual expansion. Here, I will just select properties from that object.
        Requester = "$($_.requester.name) ($($_.requester.id))";
        # RECIPIENT - This is also an object and requires manual expansion.
        Recipient = "$($_.recipient.name) ($($_.recipient.id))";
        # ACCOUNT ID - Also within an object. We have to select this. I don't see an account ID in my originalRequests object, so I will make this so that it will select the account ID if it is present/valid, otherwise it will say N/A
        "Account ID" = $($_.originalRequests.accountId ? $_.originalRequests.accountId : "N/A");
        # EXPANSION ITEMS - Not sure what you need in here. We have to select the data we want. But I'll give an example below of selecting the items requested. This is also an array of objects, so we will have to join with a comma or whatever else we want.
        "Requested Updates" = $(($_.expansionItems | ForEach-Object {"$($_.attributeRequest.op) $($_.attributeRequest.name ? $_.attributeRequest.name : 'N/A'): $($_.attributeRequest.value ? $_.attributeRequest.value : 'N/A')"}) -Join ", ");
        # STAGE - This is a simple string and can be retrieved without any specific formatting or adjustment
        Stage = $_.stage;
        # CREATED - This is a simple string and can be retrieved without any specific formatting or adjustment
        Created = $_.created;
        # MODIFIED - This is a simple string and can be retrieved without any specific formatting or adjustment
        Modified = $_.modified;
    })
}

```

> ⚠ **Important**  
>   
> This is just a sample script and provides a quick explanation of how to expand objects and join values from an array to a string. This may not be exactly what you need and is not intended to be a perfect solution. This is intended to be something to get you started in the right direction. Please perform testing and data validation before using this for any production use.

---

<div class="post-metadata">

**Author:** ![hranjan3](https://avatars.discourse-cdn.com/v4/letter/h/ecb155/32.png) [@hranjan3](https://developer.sailpoint.com/discuss/u/hranjan3)\
**Post date:** [May 24, 2024, 3:40pm UTC](https://developer.sailpoint.com/discuss/t/powershell-api-script-print-object-data/57896/14 "2024-05-24T15:40:55Z")

</div>

Hi @brennenscott Thank you so much for your response! I will try suggested changes and will update here.

---

<div class="post-metadata">

**Author:** ![hranjan3](https://avatars.discourse-cdn.com/v4/letter/h/ecb155/32.png) [@hranjan3](https://developer.sailpoint.com/discuss/u/hranjan3)\
**Post date:** [May 24, 2024, 4:03pm UTC](https://developer.sailpoint.com/discuss/t/powershell-api-script-print-object-data/57896/15 "2024-05-24T16:03:37Z")

</div>

@brennenscott I tried updated script and it worked like charm! Thank you so much for your help.

Thank you all!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/sailpoint/original/2X/f/f2136700ed5e3703e0b85e02f6be799dacca7735.png) [@system](https://developer.sailpoint.com/discuss/u/system)\
**Post date:** [July 23, 2024, 4:04pm UTC](https://developer.sailpoint.com/discuss/t/powershell-api-script-print-object-data/57896/16 "2024-07-23T16:04:34Z")

</div>

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