如何使用 PowerShell 从资源组获取 Azure 资源?


要使用 PowerShell 从资源组获取可用资源,我们需要使用 Get-AZResource 命令。假设我们有资源组名称 AnsibleTestRG,我们需要从资源组检索资源,然后我们将使用以下命令。

示例

Get-AzResource -ResourceGroupName AnsibleTestRG

要筛选输出,

输出

Get-AzResource -ResourceGroupName AnsibleTestRG | Select Name, ResourceType, Location

输出

如果特定订阅中有多个资源组,我们可以使用以下命令将资源从资源组导出到 CSV 文件。

示例

$ErrorActionPreference = "Stop"
try {
    Connect-AZAccount
    Set-AzContext -SubscriptionName 'Your Subscription Name'
    $rgs = Get-AzResourceGroup

    foreach ($rg in $rgs.ResourceGroupName) {
        Write-Output "Checking Resource Group: $rg"
        Get-AzResource -ResourceGroupName $rg | Select Name, ResourceGroupName, Type, Location | Export-Csv .\AzureResources.csv -Append -Force -NoTypeInformation
    }
}
catch {
    Write-Host "$($_.Exception.Message)" -BackgroundColor DarkRed
}

更新日期:2021 年 4 月 12 日

2K+ 浏览量

开启你的 职业

完成课程,获得认证

开始学习
广告
© . All rights reserved.