ForEach 调用-RestMethod

ForEach Invoke-RestMethod

我正在尝试向 Cisco Prime API 的 AccessPointsDetails 端点发出请求,然后循环获取所有 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

}
}

您遇到的最大问题是错误地访问了返回对象的成员。 Invoke-RestMethod 正在向您返回 JSON 应用程序 returns 的 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}
    }
}