powershell 获取特定Azure管理组(如“ABC”)中的所有Azure资源组

6yt4nkrj  于 2023-10-18  发布在  Shell
关注(0)|答案(2)|浏览(87)

我试图通过尝试一些命令来完成这一点,但当我开始与“获取AzResourceGroup”我得到下面的错误:
错误:Get-AzSubscription : Method 'get_SerializationSettings' in type 'Microsoft.Azure.Management.Internal.Resources.ResourceManagementClient' from assembly 'Microsoft.Azure.PowerShell.Clients.ResourceManager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation. At line:1 char:1
'Az.Accounts'版本为2.13.0。我也试着把它降级到2.12.1,但运气不好,资源的版本是6.10.0。
任何你建议的powershell脚本都将受到高度赞赏。

4jb9z9bj

4jb9z9bj1#

获取特定Azure管理组(如“ABC”)中的所有Azure资源组
要获取特定Azure management group中的所有资源组,以下是更新后的PowerShell代码。

Powershell

$managementGroups = Get-AzManagementGroup
    $managementGroupNameToFind = "Samplegroup1"
    $foundManagementGroup = $managementGroups | Where-Object { $_.Name -eq $managementGroupNameToFind -or $_.DisplayName -eq $managementGroupNameToFind }
    if ($foundManagementGroup) {
        $subscriptions = Get-AzManagementGroupSubscription -GroupName $foundManagementGroup.Name
        $result = @()
        foreach ($subscription in $subscriptions) {
            Write-Host "Subscription Name: $($subscription.DisplayName)"
            $subscriptionId = ($subscription.Id -split '/')[-1]
            $set = Set-AzContext -SubscriptionId $subscriptionId
            $resourceGroups = Get-AzResourceGroup
            foreach ($resourceGroup in $resourceGroups) {
                $result += [PSCustomObject] @{
                    Subscription = $subscription.DisplayName
                    SubscriptionId = $subscriptionId
                    ResourceGroup = $resourceGroup.ResourceGroupName
                }
            }
        }
    
        $result | Format-Table -AutoSize
    }

如果我运行上面的脚本,它将检索所有订阅并获取指定Azure Management Group下的所有Resource Group详细信息。输出:

mnemlml8

mnemlml82#

我能够使它在较低版本的PS中使用较短的脚本,如下所述:

Get-AzManagementGroup -GroupName <MgmtGroup name> -Recurse | ForEach-Object {  Get-AzResourceGroup}

相关问题