【问题标题】:Traverse REST Api URL using Invoke-WebRequest使用 Invoke-WebRequest 遍历 REST Api URL
【发布时间】:2015-12-23 23:50:06
【问题描述】:

使用 PowerShell 我试图调用一个 webrequest,它读取第一个请求的入口点 $defUrl,然后是每个 URL 中的后续端点。

以上返回$UrlParsed后:

然后我将其添加到第一张图像中,以便横穿后续端点。

foreach ($Url in $UrlsParsed) {
  Invoke-WebRequest -uri $UrlsParsed -Credential $cred
}

添加foreach 循环后,出现以下错误。

我想获取的信息位于该 URL 的不同深度。

需要一些关于我可能做错了什么的见解以及如何解决这个问题的建议。

【问题讨论】:

  • 请避免发布代码或错误的图像。尽可能以文本形式包含信息。

标签: powershell httpwebrequest


【解决方案1】:

改变这个:

foreach ($Url in $UrlsParsed) {
  Invoke-WebRequest -uri $UrlsParsed -Credential $cred
}

进入这个:

foreach ($Url in $UrlsParsed) {
  Invoke-WebRequest -uri $Url -Credential $cred
}

【讨论】:

  • 之前尝试过,但没有成功。据我了解,每个单独的请求都应该通过。认为我可能需要创建一个新对象,然后使用它通过循环调用新请求。
  • 是的,我应该更清楚。抛出相同的异常。
  • 请用准确的代码和错误信息更新您的问题。
  • 我们的应用程序通过 json api(网页)公开应用程序状态列表。当我通过豪华的休息请求方法阅读时。它首先显示所有端点的列表,如下所示 [ { "serviceUrl": "demo.galacticempire.co.uk/tie-fighter/api/v2/services/…", "level": "ERROR" }} .....
  • 显示,不告诉。更新您的问题。
【解决方案2】:

经过一些帮助和反复试验,得到了我想要的结果。适当的注释代码。

$user = "darthmaul"
$pwd = "#deathStarOrder66" | ConvertTo-SecureString -asPlainText -Force

#This Url exposes all the service endpoint from a top level.
$defUrl = "https://weaponsTest1.galacticempire.co.uk/star-destroyer/api/v2/service_states?limit=1000"
$cred = New-Object System.Management.Automation.PSCredential($user,$pwd)
#connect to the first page that exposes all the URL
$stackStatus = Invoke-RestMethod -Uri $defUrl -Credential $cred

#appends only the service url for an individual service
$parsedUrls = $stackStatus.serviceUrl
#secure requests required else the call fails
$UrlsParsed = $parsedUrls.Replace("http","https")

#for each service found perform an addition call to determine the status of the service via a foreach loop 
foreach ($Url in $UrlsParsed)
{
    #use the individual url
    $sep = Invoke-WebRequest -uri $url -Credential $cred
    #select specific table you want to display and rename as you see fit.
    $svcStatus = $sep | select @{name="Response";Expression ={$_.statuscode}}, @{name="Status";Expression ={$_.statusdescription}} 
    #convert from json so you can select the field you want more easily.
    $svcCall = $sep | ConvertFrom-Json | select displayname, serviceTypeUrl, port, serviceport

    #in order to join those 2 variables together ($svcStatus & $svcCall)
    # add new members to the x.status object by appending svcCall.X to make it look like they are part of the object svcStatus.
    #
    $svcStatus | Add-Member -MemberType NoteProperty -Name displayname -Value $svcCall.displayname
    $svcStatus | Add-Member -MemberType NoteProperty -Name serviceTypeUrl -Value $svcCall.serviceTypeUrl
    $svcStatus | Add-Member -MemberType NoteProperty -Name port -Value $svcCall.port
    $svcStatus | Add-Member -MemberType NoteProperty -Name servicePort -Value $svcCall.servicePort

    #print out to view.
    $svcStatus | fl
}

【讨论】:

    猜你喜欢
    • 2020-07-11
    • 2019-06-16
    • 2021-12-20
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 2018-07-19
    相关资源
    最近更新 更多