- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Invoke-RestMethod -Uri https://...
调用休息服务电话。 .调用结果,位于 JSON
, 可以通过管道将数据传输到 Format-Table -Property ...
并显示数据。
但是当Select-Object -Property ...
在调用后使用相同的参数,PSObject
有列但没有数据。如果我使用不同的网络服务,调用将起作用。
什么可能导致 PSObject
不显示任何值?
公共(public)休息网络服务的工作示例
Invoke-RestMethod -Uri https://jsonplaceholder.typicode.com/todos/1 |
Select-Object -Property title
@{title=delectus aut autem}
Invoke-RestMethod -Uri https://cat-fact.herokuapp.com/facts | Select-Object -Property text
最佳答案
在转换 JSON 数组时,您偶然发现了两个 PowerShell 怪异的邪恶组合:
Invoke-RestMethod
和 ConvertFrom-Json
通过管道发送从 JSON 转换而来的数组作为一个整体,而不是像往常一样逐个元素发送 :ComvertFrom-Json
的行为已更改 与通常的元素枚举行为一致,以及 -NoEnumerate
switch 被添加为对旧行为的选择。有关导致此更改的讨论,请参阅 GitHub issue #3424 .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
)仍然输出具有
.text
的自定义对象属性,而不是
.text
属性值。
# 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 ,因为输出的只是字符串值,而不是自定义对象。
关于json - 从成功的 Invoke-RestMethod 中选择对象数据失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56916962/
我有一个REST API,它将返回行。然而, 为什么Invoke-RestMethod -Uri "..." -Method Get | select XXX,YYY仅返回 header ? XXX
我有这段代码: $url = "https://outlook.office365.com/api/v1.0/me/messages" $date = Get-Date -Format "yyyy-M
问题 我很难将 curl 调用转换为Powershell Invoke-RestMethod 调用,因为Powershell并不会发出最多信息的错误消息(如果有的话)。 通话(Ubuntu) toke
我正在尝试创建一个 powershell 脚本来访问 DYN 的 API 并在我使用/测试的 DNS 区域上执行检查/更新。 我正在关注他们的 API 详细信息,这是第一个链接,https://hel
我正在使用 Invoke-RestMethod 从我正在使用的应用程序中获取页面名称。我注意到当我在页面上执行 GET 时,它会像这样返回页面名称 This page â is working 但是实
我们正在使用 Invoke-RestMethod在 PowerShell 脚本中调用 GET具有可变长度运行时的方法端点。有些电话可能会在几秒钟后返回,有些可能需要长达 20 分钟。我们通过 -Tim
使用PowerShell 4.0和Invoke-RestMethod cmdlet。我在使用-OutFile和-PassThru选项时遇到麻烦。 每当我添加-PassThru选项时,都会创建-OutF
我想调用一个传递包含反斜杠字符的参数的 REST API。 API 需要像 api/test// 这样的路由,例如我想传递 domain\login作为 user_name 的值. 我用 PowerS
我正在尝试将文件上传到URL。我尝试了这两种方法: Invoke-RestMethod -Uri $uploadUrl -Method Put -Headers $uploadHdrs -InFile
似乎这个问题已经被提出并得到解答,但到目前为止我遇到的每个解决方案都没有帮助。我正在编写一个 PowerShell 脚本来运行一些 REST API 来获取使用信息。我的脚本在尝试与服务器通信时立即中
Note: this question is here for historical reasons or for users of Powershell -Method GET -ContentT
我正在使用RESTful API,并且使用Firefox RESTClient插件一切都很好。我可以轻松查询API。 但是,当我尝试将相同的API调用插入powershell时不起作用! 我已经尝试了
由于某种原因,这不起作用 Invoke-RestMethod -Uri "http://localhost" -Headers @{"Host"="web.domain.com"} 我收到错误 The
Invoke-RestMethod 遇到了一个非常奇怪的问题。我使用它发送 GET 并在请求中包含 cookie: $getEndpoint = "http://YYYYYYYYYYYYYY/clie
我可以使用Powershell v5中的JIRA rest API。但是,相同的代码在Powershell v3中引发以下错误。 WARNING: Remote Server Response: Th
我正在使用 Invoke-RestMethod -Uri https://... 调用休息服务电话。 .调用结果,位于 JSON , 可以通过管道将数据传输到 Format-Table -Proper
如何使用 Powershell 调用 Azure 身份验证背后的 API?更具体地说,我确实希望这条线能够工作:Invoke-RestMethod -Method Get -Uri 'https://
我正在通过 GET Invoke-RestMethod 从我的应用程序中提取有关提供程序的一些详细信息。目前它正在返回有关提供者的所有详细信息。我只想返回事件状态设置为 True 的提供商的代码。 $
如何使用 Powershell 调用 Azure 身份验证背后的 API?更具体地说,我确实希望这条线能够工作:Invoke-RestMethod -Method Get -Uri 'https://
我正在尝试使用新的 invoke-restmethod cmdlet 来 POST JSON 文件并已成功完成此操作。但是,我没有像使用 CURL 时那样收到网络服务器的响应。对于我想要完成的任务,我
我是一名优秀的程序员,十分优秀!