REST API 连接中 Invoke-Command 方案的解决方法

在同一窗口中与 Exchange Online 或 Security & Compliance PowerShell 的多个连接中,使用 Invoke-Command cmdlet 在特定的远程 PowerShell 会话中运行脚本或命令。 但是,Invoke-Command cmdlet 在与 Exchange Online 或安全 & 合规性 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 会话中:

    1. 使用 Get-PSSession cmdlet 将第一个远程 PowerShell 会话详细信息存储在名为 $Session1的变量中。

    2. 使用 Get-PSSession cmdlet 将第二个远程 PowerShell 会话详细信息存储在名为 $Session2的变量中。

    3. 运行以下命令:

      Invoke-Command -Session $Session1 -ScriptBlock {Get-Mailbox -ResultSize 1}
      
      Invoke-Command -Session $Session2 -ScriptBlock {Get-Mailbox -ResultSize 1}
      
  • 在 REST API 会话中:

    1. 在第一个 Connect-ExchangeOnline 命令中,将参数 前缀 与值 C1 一起使用。

    2. 将第一个 REST API 连接详细信息存储在通过运行以下命令命名的 $ConnectionInfo1 变量中:

      $ConnectionInfo1 = Get-ConnectionInformation | Where-Object { $_.ModulePrefix -eq "C1"}
      
    3. 在第二个 Connect-ExchangeOnline 命令中,将参数 前缀 与值 C2 一起使用。

    4. 将第二个 REST API 连接详细信息存储在通过运行以下命令命名 $ConnectionInfo2 的变量中:

      $ConnectionInfo1 = Get-ConnectionInformation | Where-Object { $_.ModulePrefix -eq "C2"}
      
    5. 现在,你可以在任一会话中运行命令。 例如:

      $CommandStr1 = "Get-$($ConnectionInfo1.ModulePrefix)Mailbox -ResultSize 10"
      
      Invoke-Expression $CommandStr1
      

      或

      $CommandStr2 = "Get-$($ConnectionInfo2.ModulePrefix)Mailbox -ResultSize 10"
      
      Invoke-Expression $CommandStr2