在 Powershell 中解析数组时遇到问题

发布于 2025-01-17 20:17:07 字数 1463 浏览 0 评论 0原文

我试图弄清楚如何通过PowerShell来解析数据。我们使用OKTA作为IDP,我想获取用户经理的姓名。

我确实弄清楚了如何获得该值,但是它带有我不需要的其他许多值。我试图仅缩放一个属性,但无法弄清楚如何用查询。

PS V:> Get-Oktauser Johndoe

返回了很多信息,包括经理的姓名:

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

我能够用“ select-object”进一步缩小它。我不知道输出格式的正确术语(hashtable?array?),但是看起来像这样:

PS V:\> Get-OktaUser JohnDoe | Select-Object profile

profile
-------
@{lastName=Doe; zipCode=10006; ChildDepartment=Technology; manager=Mary Jane; city=New York; displayName... 

试图以一种我只能获得“经理”的价值来解析,我尝试了一些东西来查看如何操纵它,但运气不佳。

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

相同的信息,整形格式。还尝试了$ test1 [0],$ test1.profile [0],for $ test $ test in $ test1写作 - 主持人$ _。管理器等。

因此,基本上,我看到了我想要的值。我可以操纵查询,但是如何逗弄一个属性和价值?答案当然很棒,但是如果您能帮助我理解原因,那将是各种各样的帮助(我认为在学习基本的Powershell时)。

无论如何,感谢您的建议!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朮生 2025-01-24 20:17:07

正如您所猜测的,就像 Get-OktaUser 返回的对象具有属性(idstatuscreated 等),每个属性的本身可以是具有 1 个或多个属性的复杂对象 - 例如 typeprofile< /code> 示例中的属性。

因此,当您在格式化输出中看到 @{propertyName=value;...} 语法时,PowerShell 可能会向您显示一个嵌套对象。另一方面,如果属性的值是一个数组,则@将不存在,即。 配置文件:{value value2 value3 ...}

要访问嵌套对象的属性,只需继续使用成员访问运算符 .

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文