快速入门:使用 Azure PowerShell 对 Resource Graph 查询结果进行分页

本快速入门介绍如何使用 Azure PowerShell 运行 Azure Resource Graph 查询并对输出进行分页。 默认情况下,Azure Resource Graph 为每个查询返回最多 1000 条记录。 可以使用 Search-AzGraph cmdlet 的 skipToken 参数来调整每个请求返回的记录数。

先决条件

安装此模块

安装 Az.ResourceGraph 模块,以便可以使用 Azure PowerShell 运行 Azure Resource Graph 查询。 Azure Resource Graph 模块需要 PowerShellGet 2.0.1 或更高版本。 如果安装了最新版本的 PowerShell 和 Azure PowerShell,则已有所需的版本。

  1. 验证 PowerShellGet 版本:

    Get-Module -Name PowerShellGet
    

    如果需要更新,请转到 PowerShellGet

  2. 安装 模块:

    Install-Module -Name Az.ResourceGraph -Repository PSGallery -Scope CurrentUser
    

    该命令将模块安装在 CurrentUser 范围内。 如果需要在 AllUsers 范围内安装,请从管理 PowerShell 会话运行安装。

  3. 验证是否已安装模块:

    Get-Command -Module Az.ResourceGraph -CommandType Cmdlet
    

    该命令显示 Search-AzGraph cmdlet 版本,并将模块加载到 PowerShell 会话中。

连接到 Azure

从 Visual Studio Code 终端会话连接到 Azure。 如果具有多个订阅,请运行命令以将上下文设置为你的订阅。 将 <subscriptionID> 替换为你的 Azure 订阅 ID。

Connect-AzAccount -Environment AzureChinaCloud

# Run these commands if you have multiple subscriptions
Get-AzSubScription
Set-AzContext -Subscription <subscriptionID>

对 Azure Resource Graph 查询结果进行分页

这些示例运行基于租户的 Resource Graph 查询来列出虚拟机,然后更新命令以返回为每个请求批处理五条记录的结果。

在每个示例中都使用相同的查询:

Resources |
join kind=leftouter (ResourceContainers | where type=='microsoft.resources/subscriptions' |
  project subscriptionName = name, subscriptionId)
  on subscriptionId |
where type =~ 'Microsoft.Compute/virtualMachines' |
project VMResourceId = id, subscriptionName, resourceGroup, name

Search-AzGraph 命令会运行一个查询,返回与给定 Azure 租户关联的所有订阅中所有虚拟机列表:

Search-AzGraph -Query "Resources | join kind=leftouter (ResourceContainers | where
type=='microsoft.resources/subscriptions' | project subscriptionName = name, subscriptionId) on
subscriptionId | where type =~ 'Microsoft.Compute/virtualMachines' | project VMResourceId = id,
subscriptionName, resourceGroup, name"

下一步将更新 Search-AzGraph 命令,为每个批处理请求返回五条记录。 该命令使用 while 循环、变量和 skipToken 参数。

$kqlQuery = "Resources | join kind=leftouter (ResourceContainers | where
type=='microsoft.resources/subscriptions' | project subscriptionName = name, subscriptionId) on
subscriptionId | where type =~ 'Microsoft.Compute/virtualMachines' | project VMResourceId = id,
subscriptionName, resourceGroup, name"

$batchSize = 5
$skipResult = 0

[System.Collections.Generic.List[string]]$kqlResult

while ($true) {

  if ($skipResult -gt 0) {
    $graphResult = Search-AzGraph -Query $kqlQuery -First $batchSize -SkipToken $graphResult.SkipToken
  }
  else {
    $graphResult = Search-AzGraph -Query $kqlQuery -First $batchSize
  }

  $kqlResult += $graphResult.data

  if ($graphResult.data.Count -lt $batchSize) {
    break;
  }
  $skipResult += $skipResult + $batchSize
}

清理资源

若要从 PowerShell 会话中删除 Az.ResourceGraph 模块,请运行以下命令:

Remove-Module -Name Az.ResourceGraph

若要从计算机中卸载 Az.ResourceGraph 模块,请运行以下命令:

Uninstall-Module -Name Az.ResourceGraph

可能会显示“模块 Az.ResourceGraph 当前正在使用”的消息。 如果是这样,则需要关闭 PowerShell 会话并启动新会话。 然后运行命令,从计算机中卸载模块。

若要退出登录 Azure PowerShell 会话,请执行以下操作:

Disconnect-AzAccount

后续步骤

本快速入门介绍了如何使用 Azure PowerShell 对 Azure Resource Graph 查询结果进行分页。 若要了解详细信息,请转到以下文章: