> For the complete documentation index, see [llms.txt](https://docs.realmjoin.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.realmjoin.com/ja/zi-dong-hua/connecting-azure-automation.md).

# Azure Automation の接続

RealmJoin PowerShell モジュールを使用して Azure Automation Account を RealmJoin Portal に接続し、RealmJoin が Tenant 内で runbook を展開、実行、監視できるようにします。

## 概要

RealmJoin ポータルで配信できるようにするには [runbook](/ja/zi-dong-hua/runbooks.md) 日常業務を自動化するための runbook を使用するには、 [Azure Automation](https://learn.microsoft.com/en-us/azure/automation/overview) アカウントを接続する必要があります。これは runbook のホストとして機能し、 [権限](/ja/zi-dong-hua/connecting-azure-automation/azure-ad-roles-and-permissions.md) を提供して、runbook が環境内で動作できるようにします。

Automation Account は次のものを使ってセットアップされます。 [RealmJoin PowerShell module](/ja/zhan-kai/onboarding-realmjoin-portal/advanced-setup.md)。Portal は、すぐにコピーできるコマンドを生成します。作成するアプリ登録も、管理するクライアント シークレットもありません。

{% hint style="info" %}
すでに従来の **RealmJoin Runbook Management** アプリ登録で、Tenant 向けに runbook が構成されていますか？その場合は、 [RealmJoin managed app への切り替え](/ja/zi-dong-hua/connecting-azure-automation/managed-app-migration.md)を参照してください。これにより Automation Account と runbook は保持され、Tenant ごとのクライアント シークレットが削除されます。
{% endhint %}

## 考慮事項

Automation Account の [Managed Identity](https://learn.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/overview) には広範な [権限](/ja/zi-dong-hua/connecting-azure-automation/azure-ad-roles-and-permissions.md) 権限が必要です。たとえば、Entra ID でグループやユーザー オブジェクトを変更したり、Exchange Online でメールボックスを管理したりできる必要があります。これらの権限の悪用を防ぐため、このアカウントへの管理アクセスは制限してください。

既存の Automation Account を使用する場合、RealmJoin Portal が [runbook の共有オンライン リポジトリ](https://github.com/realmjoin/realmjoin-runbooks)から取得した runbook を自動的に作成、更新、削除することに注意してください。この機能は既存の Automation Account ではサポートされない場合があります。確信が持てない場合は、RealmJoin runbook 用に専用の Azure Automation Account を作成することを推奨します。セットアップ コマンドはそれを自動で行います。

## セットアップで展開される内容

セットアップ コマンドを実行すると、次のものが作成されます。

* Tenant 内の **RealmJoin Azure Resources** service principal（まだ存在しない場合）
* 1 つの **Automation Account** 。これはリソース グループのリージョンに system-assigned managed identity を持つか、名前を指定した場合は既存の Automation Account を使用します。
* **Contributor** が、RealmJoin service principal に対してその Automation Account にスコープ指定された形で付与されるため、RealmJoin は runbook を同期して開始できます。
* Tenant 内の **アプリケーション権限** は、モジュールに含まれる managed identity 用のもので、Microsoft Graph、Office 365 Exchange Online、Windows Defender ATP、SharePoint Online です。

RealmJoin は独自のマルチテナント アプリケーションを使って Azure に認証します。Tenant にはアプリ登録もクライアント シークレットも作成されず、RealmJoin はその Automation Account 以外の権限を受け取りません。

{% hint style="warning" %}
managed identity の **Entra ID directory roles** — Intune Administrator、Exchange Administrator、User Administrator など — は **付与されません** 。これらは別途付与する必要があります。 [ロールと権限の付与](/ja/zi-dong-hua/connecting-azure-automation/azure-ad-roles-and-permissions.md#granting-roles-and-permissions)を参照してください。これらがないと、共有 runbook の大部分は動作しません。
{% endhint %}

## 前提条件

### RealmJoin Portal では

* 設定エリアへのアクセス権を付与するロール（`CanReadSettingsDetails`、 [利用可能な権限](/ja/to/permission/custom-roles/available-permissions.md)を参照）。設定ページは、短期間有効なオンボーディング トークンを含むセットアップ コマンドを生成します。

### Azure では

* Azure サブスクリプションと **既存のリソース グループ** が必要です。モジュールはリソース グループを作成しません。Automation Account 用に別のリソース グループを用意することを推奨します。
* また、既存の Automation Account を任意で使用できます。名前を指定しなければ、アカウントが作成されます。

### PowerShell を実行するマシンでは

* **PowerShell 5.1** 以降（Windows PowerShell または PowerShell 7）
* へのアクセス [PowerShell Gallery](https://www.powershellgallery.com/packages/RealmJoin) が必要です。RealmJoin module をインストールするためです
* Tenant 内の `Az.Accounts`, `Az.Resources` および `Az.Automation` モジュール。RealmJoin module は、現在のユーザー向けに固定されたバージョンを自動的にインストールします。

### サインインするアカウントの権限

* **Microsoft Entra ID:** service principals を作成する権限 **とアプリケーション権限を付与する権限** — セットアップは Graph、Exchange、Defender、SharePoint の app roles を managed identity に割り当てます。実際には、これは *Global Administrator* または *Privileged Role Administrator*.
* **Azure サブスクリプション:** リソース グループへ展開する権限 **および** 、およびそこでロール割り当てを作成する権限。たとえば *Owner*、または *Contributor* に加えて *User Access Administrator* / *Role Based Access Control Administrator*.
* サインインは `Connect-AzAccount`を通じて行われ、 **Azure PowerShell** の first-party application を使用します。ユーザーの同意を制限している Tenant では、最初にそのアプリケーションに管理者同意を付与する必要がある場合があります。

{% hint style="warning" %}
コマンドは、 **新しく開いたローカルの** PowerShell セッションで実行し、 **Azure Cloud Shell**は避けてください。RealmJoin module には `Az.*` モジュールの正確なバージョンが必要です。別のバージョンがすでにセッションに読み込まれている場合（Cloud Shell では通常そうです）、モジュールはバージョン競合で停止し、新しいセッションを開くことでしか解決できません。
{% endhint %}

## セットアップ

{% stepper %}
{% step %}

#### リソース グループを準備する

Azure Portal で、Automation Account を保持するリソース グループを作成または選択します。その名前と、存在するサブスクリプションの ID を控えてください。

使用する **既存の** Automation Account がある場合は、その名前も控えてください。
{% endstep %}

{% step %}

#### RealmJoin Portal からコマンドをコピーする

RealmJoin Portal で、 **Settings → Runbooks** (<https://portal.realmjoin.com/settings/runbooks-configuration>).

このページには、runbook の構成が RealmJoin PowerShell module を介して管理されることが示され、コマンド ブロックが表示されます。 **Copy** ボタンを使ってコピーしてください。

```powershell
$latest = [version](Find-Module RealmJoin -Repository PSGallery -ErrorAction Stop).Version
if (-not (Get-Module -ListAvailable RealmJoin | Where-Object Version -ge $latest)) {
    Install-Module RealmJoin -Force -AllowClobber -ErrorAction Stop
}
Import-Module RealmJoin -Force
Set-RJAutomationAccount -Token "<Token>" 6>&1
```

{% hint style="info" %}
**これらのコマンドについて `6>&1`:** は、PowerShell の Information stream に進捗が報告されます。 `6>&1` このリダイレクトにより、その出力がコンソールに表示されます。これがないと進捗メッセージは表示されません。
{% endhint %}
{% endstep %}

{% step %}

#### 対象パラメーターを追加する

`Set-RJAutomationAccount` は、どこに展開するかを知る必要があります。実行する前に最後の行を完成させてください:

```powershell
Set-RJAutomationAccount -ResourceGroupName "rg-realmjoin" -SubscriptionId "<SubscriptionId>" -Token "<Token>" 6>&1
```

* `-ResourceGroupName` は必須です。省略すると、PowerShell が入力を求めます。
* `-SubscriptionId` は任意です。指定しない場合は現在の Azure コンテキストのサブスクリプションが使われ、複数ある場合は選択を求められます。
* `-AutomationAccountName` は任意です。指定すると既存の **既存の** Automation Account

{% hint style="warning" %}
を使用します。指定しない場合は、生成された名前で新しいものが作成されます。既存の Automation Account を対象にすると、RealmJoin が期待する設定、つまり system-assigned managed identity、Basic SKU、public network access も満たされることになります。再利用する前に、それが要件に合っているか確認してください。
{% endhint %}
{% endstep %}

{% step %}

#### コマンドを実行する

ブロックを新しい PowerShell セッションに貼り付けて実行します。トークンの有効期限はおよそ 1 時間です。期限切れの場合は、設定ページを再読み込みしてコマンドを再度コピーしてください。

でサインインするよう求められます `Connect-AzAccount`。アカウントが複数の Tenant や subscription へのアクセス権を持っている場合、モジュールが選択を求めます。

展開は service principal、Automation Account、権限の 3 ステップで実行され、作成されるたびに各リソースが報告されます。通常、1 回の実行には数分かかります。

{% hint style="info" %}
先に何が起こるか見たいですか？ `-WhatIf` を追加すると、Azure Resource Manager の What-If プレビューになります。サインインと `Az` モジュールのインストールは引き続き行われる点に注意してください。プレビューには両方が必要だからです。
{% endhint %}
{% endstep %}

{% step %}

#### managed identity に Entra ID ロールを割り当てる

セットアップは managed identity にアプリケーション権限を付与しますが、Entra ID directory roles は付与しません。今それらを割り当ててください。現在の一覧と実行用スクリプトは [ロールと権限の付与](/ja/zi-dong-hua/connecting-azure-automation/azure-ad-roles-and-permissions.md#granting-roles-and-permissions) を参照してください。

managed identity の object ID は、Azure Portal の Automation Account の Account Settings > Identity に表示されます。

<figure><img src="https://3438325150-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MkrcM7cKOpXKri1kVrh%2Fuploads%2F4ecZSM3NW9PWJJlaNmR4%2Fimage.png?alt=media&amp;token=e5d5b56b-50f3-4f35-a41b-7cb4230dd3f3" alt=""><figcaption><p>Automation Account の managed identity</p></figcaption></figure>
{% endstep %}

{% step %}

#### Portal で確認して最初の sync を開始する

戻って **Settings → Runbooks** 選択し **更新**。RealmJoin が新しい構成を取得し、初回の runbook import を開始します。sync が完了するまでページを開いたままにしてください。

Subscription、resource group、Automation Account 名、location は now shown read-only under **Azure connection** — これらは今後 PowerShell module によって管理されます。
{% endstep %}
{% endstepper %}

## Portal で管理する設定

2 つの設定は、セットアップ後も **Settings → Runbooks** で編集可能なままです。

* **Branch** — 次の [shared runbook repository](https://github.com/realmjoin/realmjoin-runbooks) のブランチです。セットアップではこれを `production`に設定します。特別な理由がない限り、これが適切です。
* **Hybrid Runbook Worker sync を有効にする** と使用する worker group については、 [Hybrid Runbook Worker](/ja/zi-dong-hua/runbooks/hybrid-runbook-worker.md)を参照してください。その group は、ここで選択できるようになる前に Automation Account に登録されている必要があります。

どちらかを保存すると runbook sync がトリガーされます。

後で構成や権限の問題に遭遇した場合は、 **Show cmdlet** ボタンを Azure connection の下で使うとコマンドを再表示できます。再実行は安全で、不足しているものだけを適用します。

## 従来のセットアップ（Tenant ごとの app registration）

{% hint style="warning" %}
以下のセットアップでは、 **RealmJoin Runbook Management** Tenant 内に client secret を持つ app registration を作成します。これは、過去にこの方法で構成された Tenant の参照用としてのみ記載されています。新しい構成には使用しないでください。上記の [セットアップ](#setup) に従うか、 [既存の構成を managed app に切り替えてください](/ja/zi-dong-hua/connecting-azure-automation/managed-app-migration.md).
{% endhint %}

<details>

<summary>従来のセットアップを表示する</summary>

この方法では、Automation Account は Azure Portal で手動作成され、その権限は 2 つの外部スクリプトで割り当てられ、RealmJoin は Tenant ごとの app registration と client secret で認証していました。

**Part 1 — Azure Automation Account を作成する**

1. 次へ移動して [Azure Portal > Automation Accounts](https://portal.azure.com/#create/Microsoft.AutomationAccount) を開き、新しい Automation Account を作成します。
2. Basics タブで、Subscription、Resource Group、Automation Account Name、Region を選択します。
3. Advanced タブで、System Assigned Managed Identity が有効になっていることを確認します。
4. Review + Create を選択して Automation Account を作成します。
5. Automation Account を含む resource group に移動し、IAM タブで Automation Account に Contributor を割り当てます。

![Automation Account の作成](https://3438325150-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MkrcM7cKOpXKri1kVrh%2Fuploads%2FqjBLc2zH3XCCqfAViEsU%2Fimage.png?alt=media\&token=257816e0-eb8a-478c-973f-fb288324d8c8)

![system assigned managed identity の有効化](https://3438325150-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MkrcM7cKOpXKri1kVrh%2Fuploads%2FM9N3OxNzrzQXFxSaKIpP%2Fimage.png?alt=media\&token=1e7c4d5b-d604-415b-89e0-4816a7f04a26)

**Part 2 — Automation Account に権限を割り当てる**

managed identity の権限は Azure Portal から付与できません。 [approle-and-directoryrole-granter](https://github.com/Workplace-Foundation/approle-and-directoryrole-granter) スクリプトは、 [runbooks repository](https://github.com/realmjoin/realmjoin-runbooks/tree/production/docs/other/json):

```powershell
に公開されている JSON ファイルから、app permissions と Entra ID roles の両方を割り当てます。
. .\GrantAppPermToEntApp.ps1 -enterpriseAppObjId "<ManagedIdentityObjectId>" -permissionsTemplate .\AllRealmJoinRunbooks_collected_permissions.json
```

**Part 3 — RealmJoin 用の service principal を作成する**

Portal の Settings > Runbooks では、まず Tenant ID、Subscription ID、Resource Group を入力しました。その後、Azure CLI のスニペットが表示され、 **RealmJoin Runbook Management** resource group に Contributor 権限を持つ app registration が作成されました:

```
az provider register --namespace Microsoft.Automation
az ad sp create-for-rbac -n "RealmJoin Runbook Management" --role contributor --scopes /subscriptions/<SubscriptionId>/resourceGroups/<ResourceGroup>
```

**Part 4 — ウィザードを完了する**

Portal に戻り、 `appId` および `password` の出力を Automation Account 名、shared runbook repository のブランチ（`production`）および Automation Account の location とともに入力しました。保存すると、初回の runbook import が開始されました。

![RealmJoin Portal の Automation Account connection](https://3438325150-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MkrcM7cKOpXKri1kVrh%2Fuploads%2FA0X3M2c9d13pxVt87Pf8%2Fimage.png?alt=media\&token=8e2f2b18-c3b1-4704-8cc8-eb4d43297dd1)

</details>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.realmjoin.com/ja/zi-dong-hua/connecting-azure-automation.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
