I'm trying to figure out how to parse data queried via PowerShell. We use Okta as our IdP and I'd like to get a user's manager's name.
I did figure out how to get the value, but it comes with a bunch of other values I don't need. I'm trying to zoom in on just the one attribute but can't figure out how to phrase the query.
PS V:> Get-OktaUser JohnDoe
Returns a lot of information, including manager's name:
id : 00u8u5st55ZLO81uX2p7
status : ACTIVE
created : 2020-10-11T22:25:01.000Z
lastLogin : 2022-05-30T13:29:57.000Z
type : @{id=xxxxxxxxxxxxxxxxxx}
profile : @{lastName=Doe; zipCode=10006; ChildDepartment=Technology; manager=Marey Jane; city=New York; displayName=John Doe; title=Test Account
I was able to narrow it down further with "Select-Object". I don't know the correct term for the output format (hashtable? array?), but it looks like this:
PS V:\> Get-OktaUser JohnDoe | Select-Object profile
profile
-------
@{lastName=Doe; zipCode=10006; ChildDepartment=Technology; manager=Mary Jane; city=New York; displayName...
In trying to parse that in a way that I just get the value for "manager", I tried some things to see how it can be manipulated, but with little luck.
PS V:\\\> $test1 = Get-OktaUser johndoe | Select-Object profile
PS V:\\\> $test1.profile
lastName : Doe
zipCode : 10006
ChildDepartment : Corporate Technology
manager : Mary Jane
city : New York
displayName : John Doe
Same info, neater format. Also tried $test1[0], $test1.profile[0], foreach $test in $test1 write-host $_.manager
, etc.
So basically, I see the value I want. I can sort of manipulate the query, but how do I tease out the one attribute and value? The answer would be great of course, but if you can help me understand why, that would be all sorts of helpful (in learning basic PowerShell I think).
Anyway, thanks for any advice!
CodePudding user response:
As you've surmised, just like the object(s) returned by Get-OktaUser
has properties (id
, status
, created
, etc.), the value of each of those properties can themselves be complex objects with 1 or more properties - like the type
and profile
properties in your example.
So when you see the @{propertyName=value;...}
syntax in formatted output, PowerShell is likely showing you a nested object. If the value of the property had been an array on the other hand, the @
would not have been present, ie. profile : {value value2 value3 ...}
.
To access the properties of the nested object, simply keep using the member access operator .
:
PS ~> $user = Get-OktaUser JohnDoe
PS ~> $user
id : 00u8u5st55ZLO81uX2p7
status : ACTIVE
created : 2020-10-11T22:25:01.000Z
lastLogin : 2022-05-30T13:29:57.000Z
type : @{id=xxxxxxxxxxxxxxxxxx}
profile : @{lastName=Doe; zipCode=10006; ChildDepartment=Technology; manager=Marey Jane; city=New York; displayName=John Doe; title=Test Account
PS ~> $user.profile
lastName : Doe
zipCode : 10006
ChildDepartment : Corporate Technology
manager : Mary Jane
city : New York
displayName : John Doe
PS ~> $user.profile.manager
Mary Jane