0

I have a problem with my code. I need to grab user details using PowerShell and then export them to JSON so I can use them in Python. Everything works great, except for exporting one variable.

That's the code I use:

$manager | Add-Member -NotePropertyName "CreateOnIDs" -NotePropertyValue $managerSecondaryID
 
$newAccountData += New-Object PSObject -Property @{employee=$employee; secondaryID=$testedSecondaryID[1]; employeeType=$secondaryID_EmployeeType; path=$path; manager=$manager}

Output:

{
    "path":  "OU=OU,DC=domain,DC=domain,DC=net",
    "secondaryID":  "XXXXXXY1",
    "manager":  [
                    {
                        "ID":  "xxxxxxx",
                        "FirstName":  "FakeName",
                        "LastName":  "FakeLastName",
                        "DisplayName":  "FakeDisplayName",
                        "Domain":  "FakeDomain",
                        "CreateOnIDs":  "xxxxxxz xxxxxxy xxxxxxq"
                    }
                ],
    "employee":  [
                     {
                         "ID":  "xxxxxx0",
                         "FirstName":  "FakeName",
                         "LastName":  "FakeLastName",
                         "EmailAddress":  "Fake.Email@fake.com",
                         "Title":  "FakeTitle",
                         "Department":  "FakeDepartment",
                         "Company":  "FakeCompany",
                         "Country":  "Country",
                         "EmployeeType":  "EMP",
                         "Manager":  "CN=xxxxxxx,OU=Users,OU=IT,DC=domain,DC=domain,DC=net",
                         "Domain":  "domain.domain.net",
                         "CreateOn":  "domain.domain.net",
                         "AccountType":  "SelectedAccountType"
                     }
                 ],
    "employeeType":  "EmployeeType"
}

So I'm getting the output I'd like to have but the problem is with manager['CreateOnID']. I need to have it in following format:

"CreateOnIDs":  [
                "xxxxxxz",
                "xxxxxxy",
                "xxxxxxq"
                ]

What's weird is that when I use ConverTo-Json on the $manager variable only then I'm getting the correct result. My guess would be that something is wrong while I create $newAccountData, but I'm unable to find a solution.

$manager.CreateOnIDs is

IsPublic    isSerial    Name      BaseType
--------    --------    ----      --------
True        True        Object[]  System.Array
  • 1
    `$newAccountData |ConvertTo-Json -Depth 9`? – iRon Aug 28 '21 at 12:25
  • 1
    The short of it: Unfortunately, `ConvertTo-Json` has default recursion depth of `2`, causing more deeply nested objects to be _truncated_ (cut off and represented by the result of calling `.ToString()` on them; in v7.1+ you'll at least get a _warning_ when that happens). Use the `-Depth` parameter with a sufficiently high number, as shown in the [linked post](https://stackoverflow.com/q/53583677/45375). – mklement0 Aug 28 '21 at 12:29

0 Answers0