使用 powershell 进行 API 迭代

问题描述 投票:0回答:1
        We do have Sentinelone API from which we are trying to fetch applications list total records are more than 2000.
        API Uri has 1000 as a limit which gives 1000 entries in Json format.
        Json format gives data for pagination and nextCursor.
        
        How to get All the records with powershell.
        
        Following codes givens output of 1000 items in JSON format-
        
        $params=@{
        "Uri"="https://SentineloneURL/web/api/v2.1/application-management/risks?endpointTypes=%22server%22&osTypes=%22windows%22&limit=1000"
        "Method" ="GET"
        "Headers" = @{
            "Content-Type" = 'application/json'
            "Authorization" = 'APIToken KEY'}
    }$res=Invoke-RestMethod @params

这里 $res 给出了 json 格式的数据,我们可以通过 foreach 循环通过 $res.data 获取应用程序名称。 我们得到下一个的光标 - @{nextCursor=cursorkey;总物品数=3000}

powershell
1个回答
0
投票

我没有办法测试这个,但是与此非常接近的东西应该可以工作:

$Cursor = ''
$CursorUri = ''
$Data = do {
   if ($Cursor) {
      $CursorUri = "&cursor=$Cursor"
   }
   $params = @{
      'Uri'     = "https://SentineloneURL/web/api/v2.1/application-management/risks?endpointTypes=%22server%22&osTypes=%22windows%22&limit=1000$CursorUri"
      'Method'  = 'GET'
      'Headers' = @{
         'Content-Type'  = 'application/json'
         'Authorization' = 'APIToken KEY'
      }
   }
   $res = Invoke-RestMethod @params
   $res.data
} While ($Cursor = $res.pagination.nextCursor)

完成后,变量

$Data
应包含所有应用程序。

© www.soinside.com 2019 - 2024. All rights reserved.