Powershell API script print object data

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.

#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]}

$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

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

$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
    })
}
1 Like

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

1 Like

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:

$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

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

and this is how I am exporting the csv file:

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

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

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

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

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

Account ID : System.Management.Automation.OrderedHashtable

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

$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

I tried but its giving same output

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

Hi @hranjan3,

can you try this ?

$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"

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

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

Key : originalRequests
Value : {12345}
Name : originalRequests

Key : expansionItems
Value : {Technology}
Name : expansionItems

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

$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"

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

JSONOutput.txt (2.1 KB)

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

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.

$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:

$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;
    })
}

:warning: 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.

4 Likes

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

1 Like

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

Thank you all!

1 Like