如何使用 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+ 瀏覽量

開啟你的 職業

完成課程,獲得認證

開始學習
廣告