|
| 1 | +--- |
| 2 | +title: Workarounds |
| 3 | +ms.author: chrisda |
| 4 | +author: chrisda |
| 5 | +manager: dansimp |
| 6 | +ms.date: |
| 7 | +ms.audience: Admin |
| 8 | +audience: Admin |
| 9 | +ms.topic: article |
| 10 | +ms.service: exchange-powershell |
| 11 | +ms.reviewer: |
| 12 | +ms.localizationpriority: normal |
| 13 | +ms.collection: Strat_EX_Admin |
| 14 | +ms.custom: |
| 15 | +ms.assetid: |
| 16 | +search.appverid: MET150 |
| 17 | +description: "Learn about the alternatives to Invoke-Command commands in REST API connections using the EXO V3 module." |
| 18 | +--- |
| 19 | + |
| 20 | +# Workarounds |
| 21 | + |
| 22 | +In multiple remote PowerShell connections to Exchange Online in the same window, you use the [Invoke-Command](/powershell/module/microsoft.powershell.core/invoke-command) cmdlet to run scripts or commands in specific remote PowerShell sessions. But, the **Invoke-Command** cmdlet doesn't work in [REST API connections](exchange-online-powershell-v2.md#updates-for-the-exo-v3-module) to Exchange Online PowerShell. |
| 23 | + |
| 24 | +This article offers REST API alternatives for scenarios that that use **Invoke-Command** commands. |
| 25 | + |
| 26 | +## Scenario 1: Run Exchange Online cmdlets |
| 27 | + |
| 28 | +This example finds the identity of any other user (`$Us = $User.Identity`). |
| 29 | + |
| 30 | +- **In a remote PowerShell session**: Use the **Get-PSSession** cmdlet to store the remote PowerShell session details in the variable named `$Session`, and then run the following command: |
| 31 | + |
| 32 | + ```powershell |
| 33 | + Invoke-Command -Session $Session -ScriptBlock {Get-User $Using:Us | Select-Object DistinguishedName, ExternalDirectoryObjectId} -ErrorAction SilentlyContinue |
| 34 | + ``` |
| 35 | + |
| 36 | +- **In a REST API session**: Run the following command: |
| 37 | + |
| 38 | + ```powershell |
| 39 | + Get-User $Us | Format-List DistinguishedName, ExternalDirectoryObjectId |
| 40 | + ``` |
| 41 | + |
| 42 | +This example finds the identity of a group member: |
| 43 | + |
| 44 | +- **In a remote PowerShell session**: Use the **Get-PSSession** cmdlet to store the remote PowerShell session details in the variable named `$Session`, and then run the following command: |
| 45 | + |
| 46 | + ```powershell |
| 47 | + Invoke-Command -Session $Session -ScriptBlock {Get-Recipient -Filter "Members -eq '$($User.DistinguishedName)'" -RecipientTypeDetails MailUniversalDistributionGroup | Select-Object DisplayName, ExternalDirectoryObjectId, RecipientTypeDetails} -ErrorAction SilentlyContinue -HideComputerName |
| 48 | + ``` |
| 49 | + |
| 50 | +- **In a REST API session**: Run the following command: |
| 51 | + |
| 52 | + ```powershell |
| 53 | + Get-Recipient -Filter "Members -eq '$($User.DistinguishedName)'" -RecipientTypeDetails MailUniversalDistributionGroup | Format-List DisplayName, ExternalDirectoryObjectId, RecipientTypeDetails |
| 54 | + ``` |
| 55 | + |
| 56 | +## Scenario 2: Run Exchange Online cmdlets and expand specific properties |
| 57 | + |
| 58 | +This example gets all mailboxes where the GrantSendOnBehalfTo permission is set, and returns the users who have this permission on the mailbox. |
| 59 | + |
| 60 | +- **In a remote PowerShell session**: Use the **Get-PSSession** cmdlet to store the remote PowerShell session details in the variable named `$Session`, and then run the following command: |
| 61 | + |
| 62 | + ```powershell |
| 63 | + Invoke-Command -Session $Session -ScriptBlock {Get-Mailbox -Filter "GrantSendOnBehalfTo -ne `$null" -ErrorAction SilentlyContinue | Select-Object ExternalDirectoryObjectId, GrantSendOnBehalfTo -ExpandProperty GrantSendOnBehalfTo} |
| 64 | + ``` |
| 65 | + |
| 66 | +- **In a REST API session**: Run the following command: |
| 67 | + |
| 68 | + ```powershell |
| 69 | + $mailboxes = Get-Mailbox -Filter "GrantSendOnBehalfTo -ne `$null" |
| 70 | +
|
| 71 | + foreach ($mailbox in $mailboxes) |
| 72 | +
|
| 73 | + { |
| 74 | + $users = $mailbox | Select-Object GrantSendOnBehalfTo -ExpandProperty GrantSendOnBehalfTo | Get-User |
| 75 | +
|
| 76 | + $users | Select-Object Name, Guid |
| 77 | + } |
| 78 | + ``` |
| 79 | + |
| 80 | +## Scenario 3: Run Exchange Online cmdlets in a specific PowerShell session when multiple sessions are present |
| 81 | + |
| 82 | +This example shows how to create two PowerShell sessions in the same window and run the **Get-Mailbox** cmdlet in each session. |
| 83 | + |
| 84 | +- **In a remote PowerShell session**: |
| 85 | + 1. Use the **Get-PSSession** cmdlet to store the first remote PowerShell session details in the variable named `$Session1`. |
| 86 | + 2. Use the **Get-PSSession** cmdlet to store the second remote PowerShell session details in the variable named `$Session2`. |
| 87 | + 3. Run the following commands: |
| 88 | + |
| 89 | + ```powershell |
| 90 | + Invoke-Command -Session $Session1 -ScriptBlock {Get-Mailbox -ResultSize 1} |
| 91 | +
|
| 92 | + Invoke-Command -Session $Session2 -ScriptBlock {Get-Mailbox -ResultSize 1} |
| 93 | + ``` |
| 94 | +
|
| 95 | +- **In a REST API session**: |
| 96 | + 1. In the first **Connect-ExchangeOnline** command, use the parameter _Prefix_ with the value C1. |
| 97 | + 2. Store the first REST API connection details in the variable named `$ConnectionInfo1` by running the following command: |
| 98 | +
|
| 99 | + ```powershell |
| 100 | + $ConnectionInfo1 = Get-ConnectionInformation | Where-Object { $_.ModulePrefix -eq "C1"} |
| 101 | + ``` |
| 102 | +
|
| 103 | + 3. In the second **Connect-ExchangeOnline** command, use the parameter _Prefix_ with the value C2. |
| 104 | + 4. Store the second REST API connection details in the variable named `$ConnectionInfo2` by running the following command: |
| 105 | +
|
| 106 | + ```powershell |
| 107 | + $ConnectionInfo1 = Get-ConnectionInformation | Where-Object { $_.ModulePrefix -eq "C2"} |
| 108 | + ``` |
| 109 | +
|
| 110 | + 5. Now you can run commands in either session. For example: |
| 111 | +
|
| 112 | + ```powershell |
| 113 | + $CommandStr1 = "Get-$($ConnectionInfo1.ModulePrefix)Mailbox -ResultSize 1" |
| 114 | +
|
| 115 | + Invoke-Expression $CommandStr1 |
| 116 | + ``` |
| 117 | +
|
| 118 | + Or |
| 119 | +
|
| 120 | + ```powershell |
| 121 | + $CommandStr2 = "Get-$($ConnectionInfo2.ModulePrefix)Mailbox -ResultSize 1" |
| 122 | +
|
| 123 | + Invoke-Expression $CommandStr2 |
| 124 | + ``` |
0 commit comments