Select-成功调用-RestMethod 导致对象数据失败
Select-Object Data Failure From Succesful Invoke-RestMethod
我正在使用 Invoke-RestMethod -Uri https://...
调用休息服务。 JSON
中的调用结果可以通过管道将数据传输到 Format-Table -Property ...
并显示数据。
但是当在使用相同参数调用后使用 Select-Object -Property ...
时,PSObject
有列但没有数据。如果我使用不同的网络服务,调用将有效。
什么可能导致 PSObject
不显示任何值?
public rest web 服务的工作示例
Invoke-RestMethod -Uri https://jsonplaceholder.typicode.com/todos/1 |
Select-Object -Property title
结果
@{title=delectus aut autem}
新失败不同API
Invoke-RestMethod -Uri https://cat-fact.herokuapp.com/facts | Select-Object -Property text
你在第二个例子中的问题是没有名为 text
的道具。 [咧嘴一笑]
唯一的道具是 all
,它包含一组对象,这些对象确实包含名为 text
的道具。所以你需要一些可以得到更深层次支撑的东西。一种方法是使用两个 Select-Object
调用。像这样的……
$Url = 'https://cat-fact.herokuapp.com/facts'
$RawIRM = Invoke-RestMethod -Uri $Url
$SO_IRM = $RawIRM |
Select-Object -ExpandProperty all |
Select-Object -Property text
$SO_IRM
变量现在有一个包含 178 个关于猫的字符串的数组。 [咧嘴一笑]
您在转换 JSON arrays:
时偶然发现了两个 PowerShell 奇怪的邪恶组合
Invoke-RestMethod
和ConvertFrom-Json
通过管道发送converted-from-JSON数组作为一个整体,而不是像往常一样逐个元素:
注意: 在PowerShell (Core) 7.0, ComvertFrom-Json
' s 的行为已更改 以与通常的 enumeration-of-elements 行为保持一致,并且 -NoEnumerate
开关作为 opt-in 添加到旧行为。有关导致此更改的讨论,请参阅 GitHub issue #3424。
但是,在撰写本文时(PowerShell (Core 7.2) Invoke-RestMethod
仍然表现出这种意外行为,这在 GitHub issue #15272.
Select-Object
不执行member-access enumeration,所以它直接寻找指定的属性(例如text
)在数组 上,它不存在。
用一个简单的例子来演示问题:
# Windows PowerShell only:
# Because ConvertFrom-Json sends an *array* (of 2 custom objects) through
# the pipeline, Select-Object looks for property .text on the *array* -
# and can't find it.
# The same applies to Invoke-RestMethod (also still in
# PowerShell (Core) as of v7.2)
PS> ConvertFrom-Json '[{ "text": "a" }, { "text": "b" }]' | Select-Object text
text
----
# NO VALUES
一个简单的解决方法是在(...)
中包含ConvertFrom-Json
/Invoke-RestMethod
调用,强制枚举 的数组,导致 Select-Object
按预期工作。:
# (...) forces enumeration
PS> (ConvertFrom-Json '[{ "text": "a" }, { "text": "b" }]') | Select-Object text
text
----
a
b
请注意,诸如 Select-Object -Property text
(没有 -ExpandProperty
)之类的命令仍会输出 custom objects 具有 .text
属性 ,而不是 .text
属性 值 。
如果您只对 属性 values 感兴趣,则解决方案更简单,因为您可以使用above-mentioned member-access 直接在数组上枚举:
# Using .<propName> on an array (collection) implicitly returns the
# property values from the *elements* of that collection (member-access enumeration).
PS> (ConvertFrom-Json '[{ "text": "a" }, { "text": "b" }]').text
a
b
请注意输出现在没有 text
header,因为输出的只是 字符串值 ,而不是自定义 objects.
我正在使用 Invoke-RestMethod -Uri https://...
调用休息服务。 JSON
中的调用结果可以通过管道将数据传输到 Format-Table -Property ...
并显示数据。
但是当在使用相同参数调用后使用 Select-Object -Property ...
时,PSObject
有列但没有数据。如果我使用不同的网络服务,调用将有效。
什么可能导致 PSObject
不显示任何值?
public rest web 服务的工作示例
Invoke-RestMethod -Uri https://jsonplaceholder.typicode.com/todos/1 |
Select-Object -Property title
结果
@{title=delectus aut autem}
新失败不同API
Invoke-RestMethod -Uri https://cat-fact.herokuapp.com/facts | Select-Object -Property text
你在第二个例子中的问题是没有名为 text
的道具。 [咧嘴一笑]
唯一的道具是 all
,它包含一组对象,这些对象确实包含名为 text
的道具。所以你需要一些可以得到更深层次支撑的东西。一种方法是使用两个 Select-Object
调用。像这样的……
$Url = 'https://cat-fact.herokuapp.com/facts'
$RawIRM = Invoke-RestMethod -Uri $Url
$SO_IRM = $RawIRM |
Select-Object -ExpandProperty all |
Select-Object -Property text
$SO_IRM
变量现在有一个包含 178 个关于猫的字符串的数组。 [咧嘴一笑]
您在转换 JSON arrays:
时偶然发现了两个 PowerShell 奇怪的邪恶组合Invoke-RestMethod
和ConvertFrom-Json
通过管道发送converted-from-JSON数组作为一个整体,而不是像往常一样逐个元素:注意: 在PowerShell (Core) 7.0,
ComvertFrom-Json
' s 的行为已更改 以与通常的 enumeration-of-elements 行为保持一致,并且-NoEnumerate
开关作为 opt-in 添加到旧行为。有关导致此更改的讨论,请参阅 GitHub issue #3424。但是,在撰写本文时(PowerShell (Core 7.2)
Invoke-RestMethod
仍然表现出这种意外行为,这在 GitHub issue #15272.
Select-Object
不执行member-access enumeration,所以它直接寻找指定的属性(例如text
)在数组 上,它不存在。
用一个简单的例子来演示问题:
# Windows PowerShell only:
# Because ConvertFrom-Json sends an *array* (of 2 custom objects) through
# the pipeline, Select-Object looks for property .text on the *array* -
# and can't find it.
# The same applies to Invoke-RestMethod (also still in
# PowerShell (Core) as of v7.2)
PS> ConvertFrom-Json '[{ "text": "a" }, { "text": "b" }]' | Select-Object text
text
----
# NO VALUES
一个简单的解决方法是在(...)
中包含ConvertFrom-Json
/Invoke-RestMethod
调用,强制枚举 的数组,导致 Select-Object
按预期工作。:
# (...) forces enumeration
PS> (ConvertFrom-Json '[{ "text": "a" }, { "text": "b" }]') | Select-Object text
text
----
a
b
请注意,诸如 Select-Object -Property text
(没有 -ExpandProperty
)之类的命令仍会输出 custom objects 具有 .text
属性 ,而不是 .text
属性 值 。
如果您只对 属性 values 感兴趣,则解决方案更简单,因为您可以使用above-mentioned member-access 直接在数组上枚举:
# Using .<propName> on an array (collection) implicitly returns the
# property values from the *elements* of that collection (member-access enumeration).
PS> (ConvertFrom-Json '[{ "text": "a" }, { "text": "b" }]').text
a
b
请注意输出现在没有 text
header,因为输出的只是 字符串值 ,而不是自定义 objects.