Getting property from object when the name is not known

Hi guys,

New member, some advice needed.

I am using Powershell to construct a rest API call to a third party service. It looks as below:

$AuthBody = @{
	"access-id"   = "xxx"
	"access-key"  = "xxx"
	"access-type" = "access_key"
$AuthParameters = @{
    Method = "POST"
    Uri =  ""
    Body = ($AuthBody | ConvertTo-Json) 
    ContentType = "application/json"
$token = (Invoke-RestMethod @AuthParameters).token
Write-Host "NEW TOKEN: [$token]"
$SecretBody = @{
    "names" = @($secretName)
    "token" = "$token"
$SecretParameters = @{
    Method = "POST"
    Uri =  ""
    Body = ($SecretBody | ConvertTo-Json) 
    ContentType = "application/json"
$s = Invoke-RestMethod @SecretParameters

write-host "s"

The code returns an object like this:



Where test is the name of the secret and x the value. I want to just get x, which I can do by by saying $s.Test (ok bad variable name), but I will not know the secret name in advance (it will be a parameter and passed into the secretBody above. What should my syntax therefore be?


Hi, welcome to the forum :wave:

Is it the only property of the object? You could use:

$secret = $s | Select-Object -ExpandProperty *


$secret = $s.PSObject.Properties.Value

Perfect, that works! And yes, only property. :slight_smile: