🦁

配布グループをPowerShellで作成

2023/05/18に公開約2,800字

はじめに

Microsoft365で配布グループを作成をGUIで行うとしんどいですしミスも出るので、PowerShellで行う方法を紹介します。

目次

環境

  • Powershell: 5.1.19041.2673
  • モジュール:Exchange Online PowerShell V2 モジュール

配布グループ作成

項目
Name 作成する配布グループの名前
ManagedBy 配布グループの管理者
PrimarySmtpAddress 配布グループのメールアドレス
MemberJoinRestriction メンバーの出入りの可否
RequireSenderAuthenticationEnabled 内部送信者からのメッセージを受けれるか否か
Type 配布orメールが有効なセキュリティグループ

使用するCSV

Name ManagedBy PrimarySmtpAddress
sales01 admin@example.com sales01@example.com
sales02 admin@example.com sales02@example.com
sales03 admin@example.com sales03@example.com

使用するPowerShell

#接続
Connect-ExchangeOnline
#CSV読み込み
$CSVs = Import-Csv -Encoding UTF8 -Path "<ファイルへのパス>"
#配布グループ作成
foreach($CSV in $CSVs)
{
New-DistributionGroup `
-Name $CSV.Name `
-ManagedBy $CSV.ManagedBy `
-PrimarySmtpAddress $CSV.PrimarySmtpAddress `
-MemberJoinRestriction open `
-RequireSenderAuthenticationEnabled $false `
-Type Distribution
}

メンバー追加

使用するCSV

Identity Member
sales01@example.com user01@example.com
sales01@example.com user02@example.com
sales01@example.com user03@example.com

使用するPowerShell

#接続
Connect-ExchangeOnline
#CSV読み込み
$CSVs = Import-Csv -Encoding UTF8 `
-Path "<ファイルへのパス>"
#メンバー追加
foreach($CSV in $CSVs)
{
Add-DistributionGroupMember `
-Identity $CSV.Identity `
-Member $CSV.Member
}
#確認
$Groups = Get-DistributionGroup -ResultSize Unlimited
$Groups | ForEach-Object {
$group = $_
Get-DistributionGroupMember -Identity $group.Name -ResultSize Unlimited | ForEach-Object {
    New-Object -TypeName PSObject -Property @{
        Group = $group.DisplayName
        Member = $_.Name
        EmailAddress = $_.PrimarySMTPAddress
        RecipientType= $_.RecipientType
        }
    }
} | Export-CSV "<ファイルへのパス>" -NoTypeInformation -Encoding UTF8

まとめ、所感

  • 確認コマンドはMSのドキュメントのコピペなのでもっと良い方法があるかも

参考

Discussion

ログインするとコメントできます