每次调用休息方法

时间:2018-08-20 16:29:47

标签: powershell cisco

我正在尝试向Cisco Prime API的 AccessPointsDetails 端点发出请求,然后遍历该请求以获取AccessPoints的所有URL对象,这样我就可以在每个URL对象上获取数据。

我得到以下输出:

VERBOSE: Making request to: @{@type=AccessPointDetails; @url=https://cpist/webacs/api/v3/data/AccessPointDetails/3373245; $=3373245}
VERBOSE: Making request to: @{@type=AccessPointDetails; @url=https://cpist/webacs/api/v3/data/AccessPointDetails/3413720; $=3413720}
VERBOSE: Making request to: @{@type=AccessPointDetails; @url=https://cpist/webacs/api/v3/data/AccessPointDetails/3432295; $=3432295}
VERBOSE: Making request to: @{@type=AccessPointDetails; @url=https://cpist/webacs/api/v3/data/AccessPointDetails/3432310; $=3432310}
VERBOSE: Making request to: @{@type=AccessPointDetails; @url=https://cpist/webacs/api/v3/data/AccessPointDetails/3462672; $=3462672}
VERBOSE: Making request to: @{@type=AccessPointDetails; @url=https://cpist/webacs/api/v3/data/AccessPointDetails/3497980; $=3497980}
VERBOSE: Making request to: @{@type=AccessPointDetails; @url=https://cpist/webacs/api/v3/data/AccessPointDetails/3497993; $=3497993}
VERBOSE: Making request to: @{@type=AccessPointDetails; @url=https://cpist/webacs/api/v3/data/AccessPointDetails/3512621; $=3512621}
VERBOSE: Making request to: @{@type=AccessPointDetails; @url=https://cpist/webacs/api/v3/data/AccessPointDetails/3526872; $=3526872}
VERBOSE: Making request to: @{@type=AccessPointDetails; @url=https://cpist/webacs/api/v3/data/AccessPointDetails/16162527426; $=16162527426}
@type              @url                                                            $          
-----              ----                                                            -          
AccessPointDetails https://cpist/webacs/api/v3/data/AccessPointDetails/3373245     3373245    
AccessPointDetails https://cpist/webacs/api/v3/data/AccessPointDetails/3413720     3413720    
AccessPointDetails https://cpist/webacs/api/v3/data/AccessPointDetails/3432295     3432295    
AccessPointDetails https://cpist/webacs/api/v3/data/AccessPointDetails/3432310     3432310    
AccessPointDetails https://cpist/webacs/api/v3/data/AccessPointDetails/3462672     3462672    
AccessPointDetails https://cpist/webacs/api/v3/data/AccessPointDetails/3497980     3497980    
AccessPointDetails https://cpist/webacs/api/v3/data/AccessPointDetails/3497993     3497993    
AccessPointDetails https://cpist/webacs/api/v3/data/AccessPointDetails/3512621     3512621    
AccessPointDetails https://cpist/webacs/api/v3/data/AccessPointDetails/3526872     3526872    
AccessPointDetails https://cpist/webacs/api/v3/data/AccessPointDetails/16162527426 16162527426

这也是一个商店的响应JSON:

{
  "queryResponse": {
    "@last": 9,
    "@first": 0,
    "@count": 10,
    "@type": "AccessPointDetails",
    "@requestUrl": "https://cpist/webacs/api/v3/data/AccessPointDetails?.group=0026",
    "@responseType": "listEntityIds",
    "@rootUrl": "https://cpist/webacs/api/v3/data",
    "entityId": [
      {
        "@type": "AccessPointDetails",
        "@url": "https://cpist/webacs/api/v3/data/AccessPointDetails/3373245",
        "$": "3373245"
      },
      {
        "@type": "AccessPointDetails",
        "@url": "https://cpist/webacs/api/v3/data/AccessPointDetails/3413720",
        "$": "3413720"
      },
      {
        "@type": "AccessPointDetails",
        "@url": "https://cpist/webacs/api/v3/data/AccessPointDetails/3432295",
        "$": "3432295"
      },
      {
        "@type": "AccessPointDetails",
        "@url": "https://cpist/webacs/api/v3/data/AccessPointDetails/3432310",
        "$": "3432310"
      },
      {
        "@type": "AccessPointDetails",
        "@url": "https://cpist/webacs/api/v3/data/AccessPointDetails/3462672",
        "$": "3462672"
      },
      {
        "@type": "AccessPointDetails",
        "@url": "https://cpist/webacs/api/v3/data/AccessPointDetails/3497980",
        "$": "3497980"
      },
      {
        "@type": "AccessPointDetails",
        "@url": "https://cpist/webacs/api/v3/data/AccessPointDetails/3497993",
        "$": "3497993"
      },
      {
        "@type": "AccessPointDetails",
        "@url": "https://cpist/webacs/api/v3/data/AccessPointDetails/3512621",
        "$": "3512621"
      },
      {
        "@type": "AccessPointDetails",
        "@url": "https://cpist/webacs/api/v3/data/AccessPointDetails/3526872",
        "$": "3526872"
      },
      {
        "@type": "AccessPointDetails",
        "@url": "https://cpist/webacs/api/v3/data/AccessPointDetails/16162527426",
        "$": "16162527426"
      }
    ]
  }
}

我觉得自己可能会有点疲倦,并且做一个愚蠢的疏忽?

解决方案代码(请参见接受的答案):

Function allAP {
Write-Verbose "Getting all APs for Store $Store"
$storeApReq = "https://cpist/webacs/api/v3/data/AccessPointDetails.json?.group=$Store"
Write-Verbose "Making request to $storeApReq"
$Global:apIdListReq = Invoke-RestMethod -uri $storeApReq -method Get -ContentType 'application/json' -headers @{ Authorization = $auth }
$Global:apIdList =  $apIdListReq.queryResponse.entityId
$Global:apIdCount =  $apIdListReq.queryResponse."@count"

Write-Verbose "Found $siteAPCount APs in Sites Database. $apIdCount out of $siteAPCount APs found."
Write-Verbose "Response Received: $apIdList"
$Global:apIdURL =  $apIdListReq.queryResponse.entityId
$Global:apURL = $apIdUrl.'@url'

Write-Verbose "Starting a loop."
ForEach($apIdURL in $apIdList) {
Invoke-RestMethod -uri $apURL -method Get -ContentType 'application/json' -headers @{ Authorization = $auth }
Write-Verbose "Making request to: $apURL"
$apURL

}
}

1 个答案:

答案 0 :(得分:1)

您遇到的最大问题是错误地访问了返回对象的成员。 Invoke-RestMethod返回应用程序返回给您的JSON的pscustomobject表示形式。您的示例JSON如下所示:

{
  "queryResponse": {
    "@last": 9,
    "@first": 0,
    "@count": 10,
    "@type": "AccessPointDetails",
    "@requestUrl": "https://cpist/webacs/api/v3/data/AccessPointDetails?.group=0026",
    "@responseType": "listEntityIds",
    "@rootUrl": "https://cpist/webacs/api/v3/data",
    "entityId": [
      {
        "@type": "AccessPointDetails",
        "@url": "https://cpist/webacs/api/v3/data/AccessPointDetails/3373245",
        "$": "3373245"
      },

      ...
    ]
  }
}

这被转换为pscustomobject

[pscustomobject]@{
    'queryResponse' = [pscustomobject]@{
        '@last'         = 9
        '@first'        = 0
        '@count'        = 10
        '@type'         = 'AccessPointDetails'
        '@requestUrl'   = 'https://cpist/webacs/api/v3/data/AccessPointDetails?.group=0026'
        '@responseType' = 'listEntityIds'
        '@rootUrl'      = 'https://cpist/webacs/api/v3/data'
        'entityId'      = @(
            [pscustomobject]@{
                '@type' = 'AccessPointDetails'
                '@url'  = 'https://cpist/webacs/api/v3/data/AccessPointDetails/3373245'
                '$'     = '3373245'
            }

            ...
        )
    }
}

我不确定为什么访问.'@url'对您不起作用。以下是您要执行的操作的可行实现。


function Get-AccessPoint
{
    param
    (
        [Parameter(Position = 0, Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]
        $Store
    )

    # function is accessing above-scope variables $auth and $siteAPCount

    Write-Verbose "Retrieving APs for store $Store"
    $uri = "https://cpist/webacs/api/v3/data/AccessPointDetails.json?.group=$Store"
    Write-Verbose "Request: $uri"
    $global:apIdListReq = Invoke-RestMethod -Uri $uri -ContentType application/json -Headers @{Authorization = $auth}
    $global:apIdList = $apIdListReq.queryResponse.entityId
    $global:apIdCount = $apIdListReq.'@count'

    Write-Verbose "Found $siteAPCount APs in Sites Database. $apIdCount out of $siteAPCount APs found."
    foreach ($entity in $apIdList)
    {
        Write-Verbose "Making request to $($entity.'@url')"
        Invoke-RestMethod -Uri $entity.'@url' -ContentType application/json -Headers @{Authorization = $auth}
    }
}