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 =  "https://api.akeyless.io/auth"
    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 =  "https://api.akeyless.io/get-secret-value"
    Body = ($SecretBody | ConvertTo-Json) 
    ContentType = "application/json"
}
$s = Invoke-RestMethod @SecretParameters

write-host "s"

The code returns an object like this:

Test

x

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?

Thanks

Hi, welcome to the forum :wave:

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

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

or

$secret = $s.PSObject.Properties.Value

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