Azure の各管理グループに所属しているサブスクリプション一覧を csv で出力してみる

Azure の各管理グループに所属しているサブスクリプション一覧を csv で出力してみる

2026.01.30

Azure サブスクリプションがどの管理グループに所属しているかを一括で確認したいと思い、簡単なシェルスクリプトを作成してみました。csv ファイル形式で管理グループ毎の所属サブスクリプションを出力します。下記の出力イメージです。

ManagementGroupID,SubscriptionDisplayName,SubscriptionID
"mg-sandbox","sandbox-01","eab80a4f-1fd9-464d-b71c-example11111"
"mg-sandbox","sandbox-02","465ee6aa-a0cc-4975-87e6-example22222"
"mg-security","security","a78bc022-adf8-405b-868c-example33333"

管理グループ毎にサブスクリプションを出力するスクリプト

管理グループ毎に所属するサブスクリプションの一覧を出力するシェルスクリプト例です。Azure CloudShell でも実行できます。

#!/bin/bash

echo "ManagementGroupID,SubscriptionDisplayName,SubscriptionID"

az account management-group list --no-register --query "[].name" -o tsv | while read mg; do
  az account management-group show \
    --name "$mg" \
    --expand \
    --no-register \
    --query "children[?type=='/subscriptions'].{
      ManagementGroupID: '$mg',
      SubscriptionDisplayName: displayName,
      SubscriptionID: name
    }" -o json | jq -r --arg mg "$mg" '.[] | [.ManagementGroupID, .SubscriptionDisplayName, .SubscriptionID] | @csv'
done

出力例です。

ManagementGroupID,SubscriptionDisplayName,SubscriptionID
"mg-sandbox","sandbox-01","eab80a4f-1fd9-464d-b71c-example11111"
"mg-sandbox","sandbox-02","465ee6aa-a0cc-4975-87e6-example22222"
"mg-security","security","a78bc022-adf8-405b-868c-example33333"

標準出力ではなく、ファイルとして保存したい場合は次のコマンドで実現できます。この例では上述したシェルスクリプトのファイル名を list-mg-subscriptions.sh として保存しています。

sh list-mg-subscriptions.sh > output.csv

シェルスクリプトは最初に管理グループの一覧を取得して、次に管理グループ毎に所属するサブスクリプションを確認するといったシンプル内容です。
管理グループグループの一覧は az account management-group list コマンドで取得できます。Root 管理グループはテナント ID が出力されます。

$ az account management-group list --no-register --query "[].name" -o tsv
00000000-0000-0000-0000-000000000000
mg-intermediate-root
mg-log-archive
mg-sandbox
mg-security
mg-security-tools
mg-workloads
mg-workloads-external
mg-workloads-internal

管理グループを指定してサブスクリプション一覧を取得する場合は az account management-group show コマンドです。expand オプションを利用することで子(children)の情報を出力でき、子の中にサブスクリプション情報が含まれます。

$ az account management-group show \
  --name mg-sandbox \
  --expand
{
  "children": [
    {
      "children": null,
      "displayName": "sandbox-01",
      "id": "/subscriptions/eab80a4f-1fd9-464d-b71c-example11111",
      "name": "eab80a4f-1fd9-464d-b71c-example11111",
      "type": "/subscriptions"
    },
    {
      "children": null,
      "displayName": "sandbox-02",
      "id": "/subscriptions/465ee6aa-a0cc-4975-87e6-example22222",
      "name": "465ee6aa-a0cc-4975-87e6-example22222",
      "type": "/subscriptions"
    }
  ],
  "details": {
    "managementGroupAncestors": null,
    "managementGroupAncestorsChain": null,
    "parent": {
      "displayName": "IntermediateRoot",
      "id": "/providers/Microsoft.Management/managementGroups/mg-intermediate-root",
      "name": "mg-intermediate-root"
    },
    "path": null,
    "updatedBy": "11111111-2222-3333-5555-6666666666666",
    "updatedTime": "2025-08-14T15:04:42.000221+00:00",
    "version": 3
  },
  "displayName": "Sandbox",
  "id": "/providers/Microsoft.Management/managementGroups/mg-sandbox",
  "name": "mg-sandbox",
  "tenantId": "00000000-0000-0000-0000-000000000000",
  "type": "Microsoft.Management/managementGroups"
}

expand オプションを利用していない場合は子(children)の情報は表示されません。

$ az account management-group show \
  --name mg-sandbox
{
  "children": null,
  "details": {
    "managementGroupAncestors": null,
    "managementGroupAncestorsChain": null,
    "parent": {
      "displayName": "IntermediateRoot",
      "id": "/providers/Microsoft.Management/managementGroups/mg-intermediate-root",
      "name": "mg-intermediate-root"
    },
    "path": null,
    "updatedBy": "11111111-2222-3333-5555-6666666666666",
    "updatedTime": "2025-08-14T15:04:42.000221+00:00",
    "version": 3
  },
  "displayName": "Sandbox",
  "id": "/providers/Microsoft.Management/managementGroups/mg-sandbox",
  "name": "mg-sandbox",
  "tenantId": "00000000-0000-0000-0000-000000000000",
  "type": "Microsoft.Management/managementGroups"
}

さいごに、jq コマンドで csv にしています。

さいごに

Azure サブスクリプションがどの管理グループに所属しているかを一覧で確認したく、簡単なシェルスクリプトを作ってみました。棚卸しなどに利用できると思います。
以上、このブログがどなたかのご参考になれば幸いです。

この記事をシェアする

FacebookHatena blogX

関連記事