在與 Exchange Online 或 Security & Compliance PowerShell 的多個連線中,您可以使用 Invoke-Command Cmdlet 在特定的遠端 PowerShell 工作階段中執行指令碼或命令。 但是,Invoke-Command Cmdlet 無法在與 Exchange Online 或 Security & Compliance PowerShell 的 REST API 連線中運作。
本文為使用 Invoke-Command 命令之案例提供 REST API 替代方案。
案例 1:執行 Exchange Online Cmdlet
此範例會尋找任何其他使用者的身分識別 ($Us = $User.Identity) 。
提示
因此需要其他命令才能取得 的$Us值$User。 這些命令並不重要。 正在使用的整體方法是重要的。
在遠端 PowerShell 工作階段中: 使用 Get-PSSession Cmdlet 將遠端 PowerShell 工作階段詳細資料儲存在名為
$Session的變數中,然後執行下列命令:Invoke-Command -Session $Session -ScriptBlock {Get-User $Using:Us | Select-Object DistinguishedName, ExternalDirectoryObjectId} -ErrorAction SilentlyContinue在 REST API 工作階段中: 執行下列命令:
Get-User $Us | Format-List DistinguishedName, ExternalDirectoryObjectId
此範例尋找群組成員的身分識別:
在遠端 PowerShell 工作階段中: 使用 Get-PSSession Cmdlet 將遠端 PowerShell 工作階段詳細資料儲存在名為
$Session的變數中,然後執行下列命令:Invoke-Command -Session $Session -ScriptBlock {Get-Recipient -Filter "Members -eq '$($User.DistinguishedName)'" -RecipientTypeDetails MailUniversalDistributionGroup | Select-Object DisplayName, ExternalDirectoryObjectId, RecipientTypeDetails} -ErrorAction SilentlyContinue -HideComputerName在 REST API 工作階段中: 執行下列命令:
Get-Recipient -Filter "Members -eq '$($User.DistinguishedName)'" -RecipientTypeDetails MailUniversalDistributionGroup | Format-List DisplayName, ExternalDirectoryObjectId, RecipientTypeDetails
案例 2:執行 Exchange Online Cmdlet 並展開特定屬性
此範例會尋找設定 GrantSendOnBehalfTo 權限的所有信箱,然後傳回擁有信箱權限的使用者。
在遠端 PowerShell 工作階段中: 使用 Get-PSSession Cmdlet 將遠端 PowerShell 工作階段詳細資料儲存在名為
$Session的變數中,然後執行下列命令:Invoke-Command -Session $Session -ScriptBlock {Get-Mailbox -Filter "GrantSendOnBehalfTo -ne `$null" -ErrorAction SilentlyContinue | Select-Object ExternalDirectoryObjectId, GrantSendOnBehalfTo -ExpandProperty GrantSendOnBehalfTo}在 REST API 工作階段中: 執行下列命令:
$mailboxes = Get-Mailbox -Filter "GrantSendOnBehalfTo -ne `$null" foreach ($mailbox in $mailboxes) { $users = $mailbox | Select-Object GrantSendOnBehalfTo -ExpandProperty GrantSendOnBehalfTo | Get-User $users | Select-Object Name, Guid }
案例 3:當有多個工作階段存在時,在特定 PowerShell 工作階段中執行 Exchange Online Cmdlet
此範例示範如何在相同的視窗中建立兩個 PowerShell 工作階段,並在每個工作階段中執行 Get-Mailbox Cmdlet。
在遠端 PowerShell 工作階段中:
使用 Get-PSSession Cmdlet 將第一個遠端 PowerShell 工作階段詳細資料儲存在名為
$Session1的變數中。使用 Get-PSSession Cmdlet 將第二個遠端 PowerShell 工作階段詳細資料儲存在名為
$Session2的變數中。執行下列命令:
Invoke-Command -Session $Session1 -ScriptBlock {Get-Mailbox -ResultSize 1} Invoke-Command -Session $Session2 -ScriptBlock {Get-Mailbox -ResultSize 1}
在 REST API 工作階段中:
在第一個 Connect-ExchangeOnline 命令中,使用參數 Prefix 搭配值 C1。
將第一個 REST API 連線詳細資料儲存在執行下列命令所命名
$ConnectionInfo1的變數中:$ConnectionInfo1 = Get-ConnectionInformation | Where-Object { $_.ModulePrefix -eq "C1"}在第二個 Connect-ExchangeOnline 命令中,使用參數 Prefix 搭配值 C2。
將第二個 REST API 連線詳細資料儲存在執行下列命令所命名
$ConnectionInfo2的變數中:$ConnectionInfo1 = Get-ConnectionInformation | Where-Object { $_.ModulePrefix -eq "C2"}現在您可以在任一工作階段中執行命令。 例如:
$CommandStr1 = "Get-$($ConnectionInfo1.ModulePrefix)Mailbox -ResultSize 10" Invoke-Expression $CommandStr1或
$CommandStr2 = "Get-$($ConnectionInfo2.ModulePrefix)Mailbox -ResultSize 10" Invoke-Expression $CommandStr2