使用 -OutFile 参数时如何在 Invoke-WebRequest/Invoke-RestMethod 上获取 HTTP 状态?

How to get HTTP status on Invoke-WebRequest/Invoke-RestMethod when using -OutFile parameter?

我有一个 restful API 的请求(请注意,Invoke-WebRequestInvoke-RestMethod 都会发生):

$resp1 = Invoke-WebRequest -OutFile $clusterConfigZipFile -Uri $apiFolder -Body $fileContent -Method 'POST' -Credential $adminCredentials -ContentType "application/x-www-form-urlencoded" -ErrorAction SilentlyContinue -Verbose

我知道它工作正常,因为:

我的问题是 $resp1 中的代码没有返回任何内容。当你使用 -OutFile.

时,我有 read 它是 "feature"

是否有捕获响应或至少响应代码的 PowerShell?

documented 一样,如果除了写入文件之外还希望返回响应,请使用 -Passthru 参数。

-OutFile<String>

Saves the response body in the specified output file. Enter a path and file name. If you omit the path, the default is the current location.

By default, Invoke-WebRequest returns the results to the pipeline. To send the results to a file and to the pipeline, use the Passthru parameter.