Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
powershell中的多页xml Rest调用_Xml_Powershell_Vcloud Director Rest Api - Fatal编程技术网

powershell中的多页xml Rest调用

powershell中的多页xml Rest调用,xml,powershell,vcloud-director-rest-api,Xml,Powershell,Vcloud Director Rest Api,因此,我很快就会面临一个问题,我目前在Vcloud director Rest API中的查询(我让它自动执行)无法获得所有结果,因为它将超过100项页面大小限制 我可以硬编码来搜索第2页,但实际上我只想能够检查是否有多个页面,如果有,有多少个页面,然后调用所有页面并输出所有页面。我不想每次需要扩展到另一个页面时都要硬编码 有以下几点 $adminvm = Invoke-RestMethod -Uri "https://vcloud.example.com/api/query?type=admi

因此,我很快就会面临一个问题,我目前在Vcloud director Rest API中的查询(我让它自动执行)无法获得所有结果,因为它将超过100项页面大小限制

我可以硬编码来搜索第2页,但实际上我只想能够检查是否有多个页面,如果有,有多少个页面,然后调用所有页面并输出所有页面。我不想每次需要扩展到另一个页面时都要硬编码

有以下几点

$adminvm = Invoke-RestMethod -Uri "https://vcloud.example.com/api/query?type=adminVM&page=1&pageSize=10&format=records" -Method Get -Headers $headers
注意第1页

我通过解析到$adminvm.QueryResultRecords

xmlns          : http://www.vmware.com/vcloud/v1.5
name           : adminVM
page           : 1
pageSize       : 100
total          : 62
href           : https://vcloud.example.com/api/query?type=adminVM&page=1&pageSize=100&format=records
type           : application/vnd.vmware.vcloud.query.records+xml
xsi            : http://www.w3.org/2001/XMLSchema-instance
schemaLocation : http://www.vmware.com/vcloud/v1.5 http://vcloud.example.com/api/v1.5/schema/master.xsd 
Link           : {Link, Link}
AdminVMRecord  : {TBGRCFS01, Windows Server 2008 R2 Datacenter, sagebe01, LAB-CC-DC01...}
现在,当$adminvm.QueryResultRecords.pagesize小于$adminvm.QueryResultRecords.total时:

$adminvm.QueryResultRecords.link来自

rel       href                                            
---       ----                                         
alternate https://vcloud.example.com/api/query?type=adminVM&page=1&pageSize=100&format=references 
alternate https://vcloud.example.com/api/query?type=adminVM&page=1&pageSize=100&format=idrecords  

对于那些不熟悉Vcloud Rest组织的人来说,所有内容往往都在$Restcall.QueryRecords.SomethingRecord中,其中将包含一个页面的数据。。。因此,在本例中,它将是$adminvm.QueryResultRecords.AdminVMRecord

现在我该怎么解决这个问题…即使是作为一个函数。。我被卡住了

我应该如何处理这个问题

目标是让它自动检查是否有多个页面,并为每个额外页面获取数据,并将所有数据一起输出


我想我应该从页面大小小于总大小开始。。。但不确定的方法。。。。我的意思是我知道我必须在每一页上做一次休息。。但是如何公式化地做呢

将调用包装在
do{}while()
循环中,然后检查当前结果中是否存在
nextPage
链接:

# Set the initial URL
$url = "https://vcloud.example.com/api/query?type=adminVM&page=1&pageSize=10&format=records"

$results = do {
    # Request page
    $adminvm = Invoke-RestMethod -Uri $url -Method Get -Headers $headers

    # Output results
    $adminvm.QueryResultRecords.AdminVMRecord

} while(($url = $adminvm.QueryResultRecords.SelectSingleNode('//link[@rel = "nextPage"]')))
$results
现在包含所有查询的结果


请注意,
SelectSingleNode()
的XPath表达式参数是区分大小写的,因此
@rel=“nextPage”
可以工作,但是
@rel=“nextPage”
不会

非常感谢您。。。实际上,我是通过
而($url=($adminvm.QueryResultRecords.link | Where{$\u0.rel-like“next*”}).href))
完成的,这很好。。。真不敢相信我完全忘记了做什么,而。。。唉。。有一天
# Set the initial URL
$url = "https://vcloud.example.com/api/query?type=adminVM&page=1&pageSize=10&format=records"

$results = do {
    # Request page
    $adminvm = Invoke-RestMethod -Uri $url -Method Get -Headers $headers

    # Output results
    $adminvm.QueryResultRecords.AdminVMRecord

} while(($url = $adminvm.QueryResultRecords.SelectSingleNode('//link[@rel = "nextPage"]')))