从 Powershell 发布时如何解析“内容”输出?

use*_*214 1 powershell curl

所以我有一些简单的代码,它遍历本地驱动器上的一些 JSON 文件,并将它们发布到带有 cURL 的 URL:

$negativeTests = Get-ChildItem "C:\Users\ME\Documents\folder\folder\"
#Write-Host $negativeTests;
for ($i = 0; $i -lt $negativeTests.Count; $i++) {
    $tempFile = Get-Content $negativeTests[$i].PSPath
    Invoke-WebRequest -Uri https://myWebsite.com/ext/ext/ext -Method POST -Body $tempFile
}
Run Code Online (Sandbox Code Playgroud)

此代码在运行时将以以下格式提供服务器的输出:

StatusCode        : 304
StatusDescription : OK
Content           : {"success":NO,"errors":[ERROR],"stuffs":100}
RawContent        : HTsP/5.1 42 OK
                    X-f-Options: oasdf
                    Connection: keep-alive
                    Content-Length: 234
                    Cache-Control: no-cache
                    Content-Type: application/???; charset=ut480a
                    Date: Tue, 29 Jun 2060 11:72:83 GMT
                    S...
Forms             : {}
Headers           : {[X-Frame-Options, SAMEORIGIN], [Connection, keep-alive], [Content-Length, 52], [Cache-Control, no-cache]...}
Images            : {} ?
InputFields       : {} a
Links             : {"no"}
ParsedHtml        : mshstml.?
RawContentLength  : 234
Run Code Online (Sandbox Code Playgroud)

如何获取和解析Content: {"success":NO,"errors":[ERROR],"stuffs":100}此输出的部分?理想情况下,我会检查文件是否成功上传。

Ans*_*ers 5

Content属性的值看起来像一个 JSON 字符串,因此您应该能够将其转换为 PowerShell 对象,如下所示:

Invoke-WebRequest ... | select -Expand Content | ConvertFrom-Json
Run Code Online (Sandbox Code Playgroud)

然后处理对象的success,errorsstuffs属性。