見出し画像

M365全ユーザーの予定表アクセス権限を「すべての詳細を表示」にPowerShellで一括変更(準備編)

Microsoft365(M365)の予定表アクセス権限は、初期設定では「AvailabilityOnly」になっています。この状態だとほかの人のスケジュールはすべて「予定あり」としか表示されず詳細を見ることはできません

弊社では一部例外社員を除き、予定は詳細まで見ることができよう「Reviewer」に設定変更しています。(もちろん予定作成時に非公開にすると、その詳細を見ることはできませんが)

予定表アクセス権限をユーザー自身に変更させる場合はOutlookからできますが、管理者が変更する場合はPowerShellを使う必要があります

まずは、予定表アクセス権限を変更しない例外社員の入力するため、全ユーザー名とPrimarySmtpAddressを抽出し、例外社員の入力する欄があるエクセルファイルを生成します。

PowerShell

※ 必要なモジュールは適宜インストールしてください。

# スクリプトファイルがある場所のパスを取得
$CurrentFolder = Split-Path $MyInvocation.MyCommand.Path -Parent

# 「99_バックアップデータ」がなければ作成
if (-not(Test-Path $CurrentFolder"\99_バックアップデータ")){
    New-Item $CurrentFolder"\99_バックアップデータ" -ItemType Directory
}

# Excelを操作する為の宣言
$excel = New-Object -ComObject Excel.Application
# 起動したExcelを表示しない
$excel.Visible = $false
$excel.DisplayAlerts = $false
# Excelファイルのフルパスを取得
$excelFile = (Get-ChildItem $CurrentFolder"\予定表権限_*.xlsx").FullName

# 前回のデータがあれば「例外」の値を取得
if (-not ([string]::IsNullOrEmpty($excelFile))){
    # ブックを開く
    $book = $excel.Workbooks.Open($excelFile, 0, $true)
    # エクセルファイルのデータを取得
    $sheet = $book.Worksheets("Sheet1")
    # A-D列の値を削除
    $lastrow_A = $sheet.Cells(2, 1).End([Microsoft.Office.Interop.Excel.XlDirection]::xlDown).Row
    $sheet.Range("A3:D"+$lastrow_A).Clear()
    # 前回のExcelファイルをバックアップに移動
    Move-Item $CurrentFolder"\予定表権限_*.xlsx" $CurrentFolder"\99_バックアップデータ"
}else{
    # 新しいブックを作成
    $book = $excel.Workbooks.Add()
    # ワークシートを番号で指定し、接続する
    $sheet = $excel.Worksheets.Item(1)
    # 指定したセルに文字列を入力し、装飾する
    $sheet.Cells.Item(1, 1) = "※予定表アクセス権限をReviewer(すべての詳細を表示)に変更します。例外にする者は「例外」列にユーザー名をコピペしてください。"
    $sheet.Cells.Item(1, 1).font.color = 0x0000ff
    $sheet.Cells.Item(2, 1) = "ユーザー名"
    $sheet.range("A2:D2").interior.color = 0xC0C0C0
    $sheet.range("A2:D2").HorizontalAlignment = -4108
    $sheet.Cells.Item(2, 2) = "PrimarySmtpAddress"
    $sheet.Cells.Item(2, 3) = "FolderType"
    $sheet.Cells.Item(2, 4) = "アクセス権限"
    $sheet.Cells.Item(2, 6) = "例外"
    $sheet.range("F2").font.color = 0xffffff
    $sheet.range("F2").interior.color = 0x666666
    $sheet.range("F2").HorizontalAlignment = -4108
    # 列幅を調整
    $sheet.Range("A1:F1").columnwidth = 20
    $sheet.Columns.item(5).columnwidth = 2
}

# 認証情報を入力
$Credential = Get-Credential

# ExchangeOnlineに接続
Connect-ExchangeOnline -Credential $credential

$GetMBs = Get-Mailbox | Select-Object DisplayName, PrimarySmtpAddress
$total = $GetMBs.Length
# 最新情報を入力
for ($i = 0; $i -lt $total ; $i++) {
    $sheet.Cells.Item($i+3, 1).value = $GetMBs[$i].DisplayName
    $sheet.Cells.Item($i+3, 2).value = $GetMBs[$i].PrimarySmtpAddress
    $per = $i / $total * 100
    Write-Progress -activity "書き込み中" -status $i"/"$total -percentComplete $per
}

# 名前をつけて保存
$book.SaveAs("$CurrentFolder\予定表権限_$(Get-Date -Format "yyyyMMdd_HHmmss").xlsx")
# Excelを閉じる
$excel.Visible = $True
$excel.DisplayAlerts = $True
$excel.Quit()
# プロセスを解放する
$excel = $null
[GC]::Collect()

# 接続を切断
Disconnect-ExchangeOnline -Confirm:$false

※ 本コードを利用する場合は必ず以下の免責事項をご確認ください。

おまけ

見っけ!

この記事が参加している募集

#仕事について話そう

109,949件

この記事が気に入ったらサポートをしてみませんか?