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]}
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
Hello @baoussounda Thanks for your reponse. I tried the modified code but unfortunately its still showing value as “System.Management.Automation.OrderedHashtable”
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;
})
}
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.