PR

WindowsPowershellでAzure操作:管理グループを作成

powershell Azure
記事内に広告が含まれています。

Azure 管理グループ

皆さん、管理グループを使っていますか?
管理グループを使うと複数あるサブスクリプションをグルーピング出来るので便利です

例えば、管理グループ配下のサブスクリプションに対して RBAC やポリシーをまとめて設定できたりします

管理グループの詳細については公式ドキュメントをご覧ください

管理グループを使用してリソースを整理する - Azure Governance - Azure governance
管理グループ、そのアクセス許可のしくみ、その使用方法について説明します。

今回も Powershell で作成してみたいと思います

前提条件

いつも通り Windows Powershell 5 で行います。そろそろ Core にした方がいいのかなあ・・・

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.19041.1023
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.1023
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1


PS C:\>

準備

いつも通り、Azure Powershellを使用します。インストール、接続方法についてはこちらの記事をご覧ください。

管理グループの作成

本題です。まずはまだ管理グループが無いので作成します
New-AzManagementGroup コマンドで作成
パラメータはGroupIDと表示名になります

PS C:\> Connect-AzAccount
<...出力は省略>
PS C:\> New-AzManagementGroup -GroupId mg-test01 -DisplayName "管理グループテスト"
警告: Upcoming breaking changes in the cmdlet 'New-AzManagementGroup' :
- The parameter : 'GroupName' is being replaced by parameter : 'GroupId'.
- Change description : We will repleace GroupName with GroupId to make it more clear.
Note : Go to https://aka.ms/azps-changewarnings for steps to suppress this breaking change warning, and other
information on breaking changes in Azure PowerShell.


Id                : /providers/Microsoft.Management/managementGroups/mg-test01
Type              : /providers/Microsoft.Management/managementGroups
Name              : mg-test01
TenantId          : xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
DisplayName       : 管理グループテスト
UpdatedTime       : 2021/07/02 5:41:22
UpdatedBy         : 44a7bd2e-508f-456e-b300-bcf9bb3e1dd9
ParentId          : /providers/Microsoft.Management/managementGroups/6ccf4e8f-f1e3-4350-b981-2e8063a20128
ParentName        : 6ccf4e8f-f1e3-4350-b981-2e8063a20128
ParentDisplayName : Tenant Root Group



PS C:\>

おっと、警告が出ました
パラメータの -GroupID は少し前まで -GroupName だったようです
試したところ両方使えますが、 -GroupID にした方が良さそうですね

管理グループの確認

作成した管理グループが出来ているかを確認します
Get-AzManagementGroup コマンドを使用します

PS C:\> Get-AzManagementGroup


Id          : /providers/Microsoft.Management/managementGroups/mg-test01
Type        : /providers/Microsoft.Management/managementGroups
Name        : mg-test01
TenantId    : xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
DisplayName : 管理グループテスト



PS C:\>

大丈夫そうですね
Azure Portal でも[管理グループ]の画面に表示されていました
表示されていない場合は暫く時間をおいてから画面を更新してみてください
管理グループやサブスクリプション等はそんなに変更が無いもののせいかキャッシュが効いてたりします

アクセス制御(IAM)の設定

今回は実際に動かしませんが、管理グループのIAMにロールを割り当てるには、New-AzRoleAssignment を使います。例はこんな感じです

PS C:\> New-AzRoleAssignment -Scope "/providers/Microsoft.Management/managementGroups/mg-test01" -RoleDefinitionName Owner -SignInName "hoge@hoge.com"

管理グループにサブスクリプションを紐づける

先ほど作成した管理グループの配下にサブスクリプションが所属するイメージで紐づけを行います。
紐づけは New-AzManagementGroupSubscription コマンドを使います

PS C:\> New-AzManagementGroupSubscription -GroupId "mg-test01" -SubscriptionId "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
警告: Upcoming breaking changes in the cmdlet 'New-AzManagementGroupSubscription' :
- The parameter : 'GroupName' is being replaced by parameter : 'GroupId'.
- Change description : We will repleace GroupName with GroupId to make it more clear.
Note : Go to https://aka.ms/azps-changewarnings for steps to suppress this breaking change warning, and other
information on breaking changes in Azure PowerShell.
PS C:\>

また同様の警告が出ていますが無視しましょう

実際に紐づけ出来たか確認します
先ほど使用した、Get-AzManagementGroup に -Expand パラメータを付けると、管理グループ配下のオブジェクトが Children に入ります
今回は”Azureサブスクリプション 1″が入っていることがわかります

ちなみに Children はコレクションなので複数紐づけることができます

PS C:\> Get-AzManagementGroup -GroupId "mg-test01" -Expand
警告: Upcoming breaking changes in the cmdlet 'Get-AzManagementGroup' :
- The parameter : 'GroupName' is being replaced by parameter : 'GroupId'.
- Change description : We will repleace GroupName with GroupId to make it more clear.
Note : Go to https://aka.ms/azps-changewarnings for steps to suppress this breaking change warning, and other
information on breaking changes in Azure PowerShell.


Id                : /providers/Microsoft.Management/managementGroups/mg-test01
Type              : /providers/Microsoft.Management/managementGroups
Name              : mg-test01
TenantId          : xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
DisplayName       : 管理グループテスト
UpdatedTime       : 2021/07/02 5:41:22
UpdatedBy         : 44a7bd2e-508f-456e-b300-bcf9bb3e1dd9
ParentId          : /providers/Microsoft.Management/managementGroups/6ccf4e8f-f1e3-4350-b981-2e8063a20128
ParentName        : 6ccf4e8f-f1e3-4350-b981-2e8063a20128
ParentDisplayName : Tenant Root Group
Children          : Azure サブスクリプション 1



PS C:\>

終わりに

例によってこれらの操作はすべて Azure Portal でも Azure CLI 等でも実現できますが、今回もPowershell で試してみました

コメント

タイトルとURLをコピーしました