Powershellを利用してExchange Onlineのコマンドを使用する方法

概要

Powershellファイル「.ps1」のファイルを作成して、Exchange Onlineのコマンドを利用するための一括コマンドの作成方法

 

事前準備

 

Office365接続のパスワードの暗号化

① パスワード用のファイルをテキストで作成します。

② テキストにMicrosoft365管理者のパスワードを入力し保存。テキストの拡張子「.txt」は消して保存します。

③ Powershellを起動し、以下のコマンドを実行し、パスワードのファイルを暗号化します。

$LiveCred = Get-Credential
$LiveCred.Password | ConvertFrom-SecureString | Set-Content "(テキストを保存したファイルパス)"

Exchange Online Management用のモジュールのインストール

① Powershellを管理者権限で起動します。

② Exchangeを管理するためのコマンドを実行します。

Install-Module PowerShellGet -Force
Update-Module PowershellGet
Set-ExecutionPolicy RemoteSigned
exit

 

③ 再度Powershellを管理者権限で起動し、コマンドを実行します。

Install-Module -Name ExchangeOnlineManagement

 

自動実行用Powershellの作成

Exchange OnlineへログインするためにPowershellファイルの作成をします。

① テキストを開き、以下のコマンドを貼り付けしてください。

# Microsoft365自動ログイン
#------------------------------------------------------------------------
# Microsoft365管理者パスワードを取得
$Password  = Get-Content "(暗号化パスワードファイルパス)" | ConvertTo-SecureString

# Microsoft365管理者のID、PWを取得しログイン
$Credential = New-Object System.Management.Automation.PSCredential "(Microsoft365の管理者アカウント)",$Password
Connect-ExchangeOnline -Credential $credential -ShowProgress $true

# Returnを実行しPowershellを閉じないようにする
return

 

② 名前を付けて保存で「.txt」から「.ps1」に変更してください。

③ 作成したPowershellのファイルを実行すると、自動的にExchange Onlineのコマンド入力が可能になります。