Invoke-RestMethod调用仅返回下面非常无用的异常,并且(据我所知)不允许您收集正文内容(fiddler跟踪结果中显示的JSON对象).这似乎是一个非常糟糕的实现,如果是这样,因为http 500定义非常具体,客户端应该返回响应的主体以帮助排除故障...我错过了什么?
invoke-restmethod -method Post -uri "https://api-stage.enviance.com/ver2/EqlService.svc/eql" -Body (ConvertTo-Json $eqlhash) -Headers @{"Authorization"="Enviance $session"}
Run Code Online (Sandbox Code Playgroud)
invoke-restmethod:远程服务器返回错误:(500)内部服务器错误.在行:1字符:9 ...
Fiddler跟踪如下
HTTP/1.1 500内部服务器错误连接:关闭日期:星期四,2013年9月12日17:35:00 GMT服务器:Microsoft-IIS/6.0 X-Powered-By:ASP.NET X-AspNet-版本:2.0.50727 EnvApi-版本:2.0,2.0 EnvApi-Remaining-Calls:994,994 EnvApi-Remaining-Interval:2684,2684 Cache-Control:no-cache Pragma:no-cache Expires:-1 Content-Type:text/csv; 字符集= utf-8的
{"errorNumber":0,"message":"当前用户无权从表'CustomFieldTemplate'"中检索数据
我有一个使用Skytap API(REST)的powershell脚本.我想抓住错误,如果有错误,并尝试显示它.
例如,我们正在改变IP:
Invoke-RestMethod -Uri https://cloud.skytap.com/configurations/XXXXXX/vms/YYYYYY/interfaces/ZZZZZZ?ip=10.0.0.1 -Method PUT -Headers $headers
Run Code Online (Sandbox Code Playgroud)
如果在其他地方使用IP,我将得到409冲突错误(请求格式正确,但与其他资源或权限冲突).
我想检查错误是否为409,然后告诉它做一些其他事情.
我需要使用 Power Shell 从请求中获取成功和/或错误状态代码。我总是得到一个空白状态。
我尝试过 Invoke-WebRequest 和 Invoke-RestMethod。我已成功通话,但找不到获取状态代码的方法。
这里是如何写的:
$resource = "some url"
$Logfile = "C:/path/log.log"
function LogWrite
{
Param([string]$logstring)
Add-content $logfile -value $logstring
}
Try
{
$Response = Invoke-WebRequest -Method Post -Uri $resource
Write-Output("Success.")
LogWrite $Date
LogWrite SuccessOnCall
LogWrite $Response.StatusCode
}
Catch
{
$ErrorMessage = $_.Exception.Message
Write-Output($ErrorMessage)
$FailedItem = $_.Exception
Write-Output($FailedItem)
LogWrite $Date
LogWrite ErrorOnCall
LogWrite $ErrorMessage
Break
}
Run Code Online (Sandbox Code Playgroud)
我也试过:
LogWrite "StatusCode:" $Response.Exception.Response.StatusCode.value__
Run Code Online (Sandbox Code Playgroud)
我使用了这个问题(和其他链接):Invoke-Restmethod: how to get the return code?
试图解决这个问题,我的日志确实写了“SuccessOnCall”,但 StatusCode 为空。
谢谢你。