> 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 Portal で配信できるようにするには [ランブック](/ja/zi-dong-hua/runbooks.md) 日常タスクを自動化するには、 [Azure Automation](https://learn.microsoft.com/en-us/azure/automation/overview) アカウントを接続する必要があります。これはランブックのホストとして機能し、 [権限](/ja/zi-dong-hua/connecting-azure-automation/azure-ad-roles-and-permissions.md) ランブックが環境内で動作するために必要なものを提供します。

Automation Account には [RealmJoin PowerShell モジュール](/ja/zhan-kai/onboarding-realmjoin-portal/advanced-setup.md)が設定されています。ポータルはそのまま貼り付けられるコマンドを生成します — 作成するアプリ登録も、管理するクライアント シークレットもありません。

{% hint style="info" %}
お使いのテナントには、従来の **RealmJoin Runbook Management** アプリ登録でランブックがすでに構成されていますか？その場合は次を参照してください [RealmJoin 管理アプリへの切り替え](/ja/zi-dong-hua/connecting-azure-automation/managed-app-migration.md)。これにより Automation Account とランブックは保持され、テナントごとのクライアント シークレットは削除されます。
{% 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 は [ランブックの共有オンライン リポジトリ](https://github.com/realmjoin/realmjoin-runbooks)から取得されるランブックを自動的に作成、更新、削除することに注意してください。この機能は既存の Automation Account ではサポートされない場合があります。確信が持てない場合は、RealmJoin のランブック用に専用の Azure Automation Account を作成することを推奨します — セットアップ コマンドがそれを行います。

## セットアップでデプロイされるもの

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

* 次の **RealmJoin Azure Resources** サービス プリンシパル（まだ存在しない場合）
* 1つの **Automation Account** リソース グループのリージョンにあるシステム割り当ての Managed Identity を持ち、または名前を指定した場合は既存の Automation Account
* **Contributor** RealmJoin サービス プリンシパルに対するもので、対象はその Automation Account にスコープされます。これにより RealmJoin はランブックを同期および開始できます
* 次の **アプリケーションのアクセス許可** モジュールに同梱される Managed Identity 向けのものです — Microsoft Graph、Office 365 Exchange Online、Windows Defender ATP、SharePoint Online

RealmJoin は独自のマルチテナント アプリケーションで Azure に対して認証します。テナント内にはアプリ登録もクライアント シークレットも作成されず、RealmJoin にその Automation Account の外で権限が付与されることもありません。

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

## 前提条件

### RealmJoin Portal では

* Settings 領域へのアクセスを付与するロール（`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 モジュールをインストールするための
* 次の `Az.Accounts`, `Az.Resources` と `Az.Automation` モジュール — RealmJoin モジュールは、現在のユーザー向けに固定されたバージョンでこれらを自動的にインストールします

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

* **Microsoft Entra ID:** サービス プリンシパルを作成する権限 **およびアプリケーションのアクセス許可を付与する権限** — セットアップでは Managed Identity に Graph、Exchange、Defender、SharePoint のアプリ ロールを割り当てます。実際には、これは *Global Administrator* または *Privileged Role Administrator*.
* **Azure サブスクリプション:** リソース グループにデプロイする権限 **と** その上でロール割り当てを作成する権限 — たとえば *Owner*、または *Contributor* と組み合わせた *User Access Administrator* / *Role Based Access Control Administrator*.
* サインインは次を通じて行われます `Connect-AzAccount`、これは次を使用します **Azure PowerShell** のファースト パーティ アプリケーションです。ユーザー同意を制限しているテナントでは、まずそのアプリケーションに管理者同意を付与する必要がある場合があります。

{% hint style="warning" %}
コマンドは次の環境で実行してください **開いたばかりのローカル** PowerShell セッションで実行し、回避してください **Azure Cloud Shell**。RealmJoin モジュールは、次の正確なバージョンを必要とします `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>).

このページでは、ランブックの構成が RealmJoin PowerShell モジュールによって管理されていることが示され、コマンド ブロックが表示されます。次の **コピー** ボタンを使ってコピーします。

```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 ストリームに進行状況を出力します。次の `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 の名前を指定するか、 **既存の** Account を使用するために渡します。指定しない場合は、生成された名前で新しいものが作成されます。

{% hint style="warning" %}
コマンドを既存の Automation Account に向けると、RealmJoin が想定する設定も適用されます。つまり、システム割り当ての Managed Identity、Basic SKU、パブリック ネットワーク アクセスです。アカウントを再利用する前に、これが要件に合っているか確認してください。
{% endhint %}
{% endstep %}

{% step %}

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

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

次でサインインするよう求められます `Connect-AzAccount`。アカウントが複数のテナントまたはサブスクリプションにアクセスできる場合、モジュールが選択を求めます。

デプロイは 3 つの手順 — サービス プリンシパル、Automation Account、権限 — で実行され、作成されるたびに各リソースを報告します。1 回の実行は通常数分かかります。

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

{% hint style="info" %}
**実行後にサインアウトするには:** Azure のサインインはコマンド完了後も有効なままです。作業が終わったら、 `Disconnect-AzAccount` を実行して既存のセッションからサインアウトしてください。
{% endhint %}
{% endstep %}

{% step %}

#### Managed Identity に Entra ID ロールを割り当てる

セットアップは Managed Identity にアプリケーションのアクセス許可を付与しますが、Entra ID ディレクトリ ロールは付与しません。今それらを割り当ててください — 次を参照してください [ロールと権限の付与](/ja/zi-dong-hua/connecting-azure-automation/azure-ad-roles-and-permissions.md#granting-roles-and-permissions) 最新の一覧と、それを行うスクリプトについては

Managed Identity のオブジェクト 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 で確認し、最初の同期を開始する

に戻り **Settings → Runbooks** を選択します **更新**。RealmJoin が新しい構成を取得して、初期ランブックのインポートを開始します。同期が完了するまでページを開いたままにしてください。

サブスクリプション、リソース グループ、Automation Account 名、および場所が、Azure 接続の下に読み取り専用で表示されるようになりました **Azure 接続** — これらは今後 PowerShell モジュールによって管理されます。
{% endstep %}
{% endstepper %}

## Portal で管理する設定

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

* **ブランチ** — 次のブランチ [共有ランブック リポジトリ](https://github.com/realmjoin/realmjoin-runbooks) に追従したいものです。セットアップではこれが次に設定されます `production`、特別な理由がない限りこれが適切な選択です。
* **Hybrid Runbook Worker の同期を有効にする** と使用するワーカー グループについては、次を参照してください [Hybrid Runbook Worker](/ja/zi-dong-hua/runbooks/hybrid-runbook-worker.md)。ここで選択する前に、そのグループを Automation Account に登録しておく必要があります。

どちらかを保存すると、ランブックの同期がトリガーされます。

後で構成または権限の問題に直面した場合は、 **コマンドレットを表示** Azure 接続の下にあるボタンでコマンドを再表示できます — 再実行しても安全で、不足しているものだけが適用されます。

## 従来のセットアップ（テナントごとのアプリ登録）

{% hint style="warning" %}
以下のセットアップでは **RealmJoin Runbook Management** テナント内にクライアント シークレット付きのアプリ登録を作成します。これは、過去にこの方法で構成されたテナント向けの参考情報としてのみ記載されています。新しい構成には使用しないでください — 次に従ってください [セットアップ](#setup) 上記、または [既存の構成を管理アプリに切り替える](/ja/zi-dong-hua/connecting-azure-automation/managed-app-migration.md).
{% endhint %}

<details>

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

この手順では、Automation Account は Azure Portal で手動作成され、その権限は 2 つの外部スクリプトで割り当てられ、RealmJoin はテナントごとのアプリ登録とクライアント シークレットで認証していました。

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

1. 次へ移動します [Azure Portal > Automation Accounts](https://portal.azure.com/#create/Microsoft.AutomationAccount) に移動し、新しい Automation Account を作成します。
2. 基本タブで、Subscription、Resource Group、Automation Account Name、Region を選択します。
3. 詳細設定タブで、System Assigned Managed Identity が有効になっていることを確認します。
4. \[Review + Create] を選択して、Automation Account を作成します。
5. Automation Account を含むリソース グループに移動し、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)

![システム割り当ての 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) スクリプトは、次の公開 JSON ファイルからアプリのアクセス許可と Entra ID ロールの両方を割り当てます [ランブック リポジトリ](https://github.com/realmjoin/realmjoin-runbooks/tree/production/docs/other/json):

```powershell
.\\GrantAppPermToEntApp.ps1 -enterpriseAppObjId "<ManagedIdentityObjectId>" -permissionsTemplate .\\AllRealmJoinRunbooks_collected_permissions.json
.\\AssignAzureADRoleToEntApp.ps1 -objectId "<ManagedIdentityObjectId>" -rolesTemplate .\\AllRealmJoinRunbooks_collected_rbacroles.json
```

**Part 3 — RealmJoin のサービス プリンシパルを作成する**

Portal の Settings > Runbooks で、まず Tenant ID、Subscription ID、Resource Group を入力しました。その後、実行する Azure CLI スニペットが表示され、次のものが作成されました **RealmJoin Runbook Management** リソース グループに対する Contributor 権限を持つアプリ登録:

```
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 名、共有ランブック リポジトリのブランチ（`production`）および Automation Account の場所とともに入力しました。保存すると初期ランブックのインポートが開始されました。

![RealmJoin Portal の Automation Account 接続](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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
