10
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Power Platform でメーリングリスト作成申請を自動化してみる

Last updated at Posted at 2025-01-07

はじめに

Microsoft 365 を導入している組織では、Teams のチーム作成、ゲストユーザー招待、メーリングリストの作成といった作業を申請ベースで受け付けて処理している場合が多いかと思います。

これらの業務は、Power Platform を活用することで自動化が可能です。
今回はその一例として、メーリングリスト作成申請業務を Power Platform で自動化する方法をご紹介します。

アプローチ考察

個人的に、メーリングリスト作成含め、Power Platform で Micrsoft 365 関連の運用業務を自動化する場合、
Microsoft Graph で API が存在するかを確認しています。

当然出来るものと思っていたのですが、リファレンスを見ると、Microsoft 365 グループだといけるのですが、メールが有効なセキュリティグループは Microsoft Graph では作成できないようです。

image.png

実際にテストしてみると、以下のようなエラーで失敗します。この辺は Exchange Online との関係性もあるのかもしれないですが、個人的には Microsoft Graph で作成出来てほしかったです。

image.png

そのため、Exchange Online 側に対してリクエストするアプローチが考えられます。大分前に書いた以下の記事のように、Azure Automation との連携になりそうです。

image.png

こちらのアプローチについて深堀していきます。

全体像

構成部品の全体像は以下のような感じです。

image.png

例えば、申請用のアプリは以下のような感じです。

image.png

image.png

技術的にコアな部分は以下の 2 つです。それぞれについて詳しく説明します。

  • Azure Automation で Exchange Online PowerShell を無人実行する
  • Power Automate から Azure Automation を利用する

Azure Automation で Exchange Online PowerShell を無人実行する

まず、ずいぶん前に Exchange Online の PowerShell は基本認証が廃止されたため、こちらを参考に証明書ベースで認証するようにしました。

Azure Automation では、証明書を共有リソースとして追加でき、PowerShell から利用できます。

image.png

そして、Azure Automation から Exchange Online 管理シェルを実行するためにモジュールをインポートします。最初ランタイムバージョン 7.2 をインポートしたのですが、何度やっても上手くいかんかったため、最終的に、5.1 にしました。依存関係のあるモジュールもすべて 5.1 のものをインポートする必要があるため、ここでちょっとはまりました。。

image.png

image.png

PowerShell 用の Runbook を作成し、そちらにコードを書きます。追加した証明書を選ぶとコードを追加できます。

image.png

image.png

param(
    [Parameter(Mandatory=$true)]
    [string]$DisplayName,
    [Parameter(Mandatory=$true)]
    [string]$Alias,
    [Parameter(Mandatory=$true)]
    [string]$PrimarySmtpAddress,
    [Parameter(Mandatory=$true)]
    [string]$OwnerAddress,
    [Parameter(Mandatory=$true)]
    [array]$MemberEmails # メンバーのメールアドレスを受け取る
)

$ErrorActionPreference = "Stop"

# Exchange Online PowerShellに接続

$cert = Get-AutomationCertificate -Name 'ExchangeCert'
$appId = "*****"
$orgName = "*****.onmicrosoft.com"

Write-Host "Exchange Online PowerShellに接続しています..."

Connect-ExchangeOnline -appid $appId -certificateThumbprint $cert.thumbprint -organization $orgName

try {
    # メールが有効なセキュリティグループの作成
    Write-Output "メールが有効なセキュリティグループを作成しています: $DisplayName"
    $group = New-DistributionGroup -Name $DisplayName -Alias $Alias -PrimarySmtpAddress $PrimarySmtpAddress -Type Security -ManagedBy $OwnerAddress

    # 作成したグループのIDを取得
    $groupId = (Get-DistributionGroup -Identity $group.Alias).ExternalDirectoryObjectId
    Write-Output "グループが正常に作成されました。グループID: $groupId"

    # グループにメンバーを追加
    foreach ($email in $MemberEmails) {
        Write-Output "メンバーを追加しています: $email"
        Add-DistributionGroupMember -Identity $group.Alias -Member $email -BypassSecurityGroupManagerCheck
    }
    Write-Output "すべてのメンバーが正常に追加されました。"

} catch {
   
    $ErrorMessage = $_.Exception.Message
    Write-Output "Error Occurred: Message: $ErrorMessage"

    # ジョブが Failed になるようにする
    throw "Runbook failed with error: $ErrorMessage"

} finally {
    # Exchange Online PowerShellから切断
    Disconnect-ExchangeOnline -Confirm:$false
    Write-Host "Exchange Online PowerShellから切断しました。"
}


テストウインドウからテストしましょう。上手く動きました。

image.png

image.png

ちなみに、失敗したときは以下のような感じでエラーメッセージが出力されます。

image.png

ここまでで Azure Automation 側の準備が出来ました。

Power Automate から Azure Automation を利用する

まず、PowerAutomate の全体像は以下のような感じです。今回は、あくまでテストのため承認プロセスは入れていませんが、必要に応じて入れてあげてください。

image.png

申請があったら、メンバー一覧を取得します。メンバー一覧は配列情報を渡します。

image.png

image.png

ジョブの状態で結果を判定します。

image.png

最初、実際にはコマンドの実行に失敗し、グループの作成に失敗しているのですが、ジョブの状態が Failed ではなく、Completed になってしまい、色々上手くいかなかったのですが、以下を入れることで、コマンドの実行に失敗した際、Failed になりました。

image.png

image.png

image.png

こちらで上手く連携できました。

まとめ

今回は、Power Platform を活用し、Azure Automation を経由して Exchange Online に PowerShell 接続し、メーリングリスト作成申請を自動化するアプローチをご紹介しました。

運用業務によっては、Exchange Online への PowerShell 接続が必要となる場合もあります。このようなシナリオにおいて、本アプローチが業務効率化に役立つ可能性があります。

ちなみに、以下を見る限り、相当な時間動かさなければ費用は発生しなさそうです。

image.png

10
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?