套用獨立 EOP 設定至多個租用戶的範例指令碼
本文中的範例 PowerShell 腳本適用于管理多個獨立Exchange Online Protection (EOP) 租使用者的系統管理員。 系統管理員可以使用本文中的腳本來檢視和/或將其組態設定套用至多個獨立 EOP 租使用者。
對多個承租人執行指令碼或指令程式
如果您尚未安裝,請安裝 Exchange Online PowerShell 模組。
使用試算表應用程式 (例如 Excel) ,建立包含下列詳細資料的.csv檔案:
- UserName 資料行:您將用來連線 (的帳戶,例如)
admin@contoso.onmicrosoft.com
。 - Cmdlet 資料行:例如
Get-AcceptedDomain
,要執行 (或) 的 Cmdlet 或Get-AcceptedDomain | Format-Table Name
命令。
.csv檔案看起來會像這樣:
UserName,Cmdlet admin@contoso.onmicrosoft.com,Get-AcceptedDomain | Format-Table Name admin@fabrikam.onmicrosoft.com,Get-AcceptedDomain | Format-Table Name
- UserName 資料行:您將用來連線 (的帳戶,例如)
將.csv檔案儲存在容易找到的位置 (例如,c:\scripts\inputfile.csv) 。
將 RunCmdletOnMultipleTenants.ps1 腳本複製到記事本,然後將檔案儲存到容易找到的位置 (例如 c:\scripts) 。
使用下列語法執行指令碼:
& "<file path>\RunCmdletOnMultipleTenants.ps1" "<file path>\inputfile.csv"
以下為範例:
& "c:\scripts\RunCmdletOnMultipleTenants.ps1" "c:\scripts\inputfile.csv"
每個租使用者都會登入,並執行腳本。
RunCmdletOnMultipleTenants.ps1
# This script runs Windows PowerShell cmdlets on multiple tenants.
#
# Usage: RunCmdletOnMultipleTenants.ps1 inputfile.csv
#
# .csv input file sample:
#
# UserName,Cmdlet
# admin@contoso.onmicrosoft.com,Get-AcceptedDomain | FT Name
# admin@fabrikam.onmicrosoft.com,Get-AcceptedDomain | FT Name
# Get the .csv file name as an argument to this script.
$FilePath = $args[0]
# Import the UserName and Cmdlet values from the .csv file.
$CompanyList = Import-CSV $FilePath
# Load the Exchange Online PowerShell module
Import-Module ExchangeOnlineManagement
# Loop through each entry from the .csv file.
ForEach ($Company in $CompanyList) {
# Get the current entry's UserName.
$UserName = $Company.UserName
# Get the current entry's Cmdlet.
$Cmdlet = $Company.Cmdlet
# Connect to EOP PowerShell by using the current entry's UserName. Prompt for the password.
Connect-ExchangeOnline -UserPrincipalName $UserName
# Here's where the script to be run on the tenant goes.
# In this example, the cmdlet in the .csv file runs.
Invoke-Expression $Cmdlet
# End the current PowerShell session.
Disconnect-ExchangeOnline -Confirm:$false
}