Generate the code to create a HashTable/PSCustomObject from an existing object

126 Views Asked by At

I am looking for a way to generate the code to create a HashTable/PSCustomObject from a variable that already exists.

Function Call

$response = Invoke-RestMethod <Parameters>...

Contents of $response

@{
    user = @{
        id = 123
        name = 'User 1'
        email = '[email protected]'
    }
    data = @{
        field1 = 'Foo'
        field2 = 'Bar'
        field3 = 'Baz'
    }
}

I can access all the individual elements of the HashTable e.g. $response.user.id and I can convert the whole lot to JSON to see it all, but if I want to manually re-create the $response variable (for example to provide different values when mocking), is there a way that I can get PowerShell to give me the code/definition to re-create the same structure?

Thanks

0

There are 0 best solutions below