0

I am new to Powershell and is currently facing this problem.

I call a REST API in Powershell which stores the API response in $GetResponse variable. Hereby some display statements:- Write-Host $Getresponse.GetType() Output :- System.Management.Automation.PSCustomObject

Write-Host $Getresponse.registered_models.name Output :- test test1

Write-Host $Getresponse | Get-Member -Force Output:- @{registered_models=System.Object[]}

I want to store the output of $Getresponse into an array variable but couldn't figure out a way to do it.

Sjain
  • 69
  • 1
  • 13
  • @mklement0 - Intend to use Write-Host here is to display statements. To give a glimpse of type of output and its values. I did save the output of GetResponse in an array variable and tried using foreach statement to display output of that array but it doesnt help. – Sjain Dec 13 '20 at 20:54
  • @mklement0 - As you suggested , I changed Write-Host to Write-Output and got output as `registered_models : {@{name=test; creation_timestamp=1607438370479; last_updated_timestamp=1607438370479}, 2020-12-13T21:00:12.8712595Z @{name=test1; creation_timestamp=1607438378299; last_updated_timestamp=1607438378299}}` But if I want to retrieve only name part and store that into an array. Could you please suggst how can I do that ? – Sjain Dec 13 '20 at 21:06
  • 1
    Don't put your code in the comment section. Add any updates to your original post, properly formatted, so that it is more easily followed and responded to. Anyway, as for your resultset and handling it. See, [Everything you ever wanted to know about Hash Tables](https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-hashtable?view=powershell-7.1) and [Everything you ever wanted to know about PSCustomObject](https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-pscustomobject) – postanote Dec 14 '20 at 00:47
  • 1
    Also, as for this 'I am new to Powershell ', that's all well and good, but then it would be prudent to get some training to limit/eliminate, confusion, bad code, errors, habits, etc., as well as learning to follow best practices. There are plenty of free resources to learn it. Sites like [this](https://www.tutorialspoint.com/powershell/index.htm), or [Microsoft Learn](https://learn.microsoft.com/en-gb/learn/modules/introduction-to-powershell/?WT.mc_id=modinfra-0000-thmaure) and [Youtube](https://www.youtube.com/results?search_query=beginning+powershell). – postanote Dec 14 '20 at 00:58
  • 1
    Thank you for your time and patience @mklement0. I just marked your answer as accepted. – Sjain Dec 16 '20 at 14:17
  • I appreciate it, @Sjain; my pleasure. – mklement0 Dec 16 '20 at 14:35

1 Answers1

0

Note:

  • Write-Host is typically the wrong tool to use, unless the intent is to write to the display only, bypassing the success output stream and with it the ability to send output to other commands, capture it in a variable, or redirect it to a file. To output a value, use it by itself; e.g., $value instead of Write-Host $value (or use Write-Output $value, though that is rarely needed); see this answer.

  • Also note that Write-Host, because it uses simple .ToString() stringification instead of PowerShell's rich output formatting system, typically produces unhelpful representations of complex objects; to output to the display only while still getting rich output formatting, pipe to the Out-Host cmdlet instead.

  • Even the rich default output formatting can be insufficient for deeply nested, such as object graphs that are constructed (automatically, by Invoke-RestMethod) from JSON responses received from REST services, for instance. To quickly visualize the structure of such object graphs, you can convert them back to JSON, by piping them to ConvertTo-Json - be sure to specify a sufficient -Depth, for the reasons explained in this post.


Based on the sample of the formatted output you posted in a comment, you were looking for the nested .name property values, which you can obtain via member-access enumeration.

[array] $names = $Getresponse.registered_models.name
mklement0
  • 382,024
  • 64
  • 607
  • 775