REST API 연결의 Invoke-Command 시나리오에 대한 해결 방법

동일한 창에서 Exchange Online 또는 보안 & 준수 PowerShell에 대한 여러 연결에서 Invoke-Command cmdlet을 사용하여 특정 원격 PowerShell 세션에서 스크립트 또는 명령을 실행합니다. 그러나 Invoke-Command cmdlet은 Exchange Online 또는 보안 & 준수 PowerShell에 대한 REST API 연결에서 작동하지 않습니다.

이 문서에서는 Invoke-Command 명령을 사용하는 시나리오에 대한 REST API 대안을 제공합니다.

시나리오 1: Exchange Online cmdlet 실행

이 예제에서는 다른 사용자의 ID($Us = $User.Identity)를 찾습니다.

및 의 값을 $User 가져오기 위해 다른 명령이 필요했기 때문에 $Us입니다. 이러한 명령은 중요하지 않습니다. 사용되는 전반적인 접근 방식은 중요합니다.

  • 원격 PowerShell 세션에서: Get-PSSession cmdlet을 사용하여 라는 $Session변수에 원격 PowerShell 세션 세부 정보를 저장한 다음, 다음 명령을 실행합니다.

    Invoke-Command -Session $Session -ScriptBlock {Get-User $Using:Us | Select-Object DistinguishedName, ExternalDirectoryObjectId} -ErrorAction SilentlyContinue
    
  • REST API 세션에서: 다음 명령을 실행합니다.

    Get-User $Us | Format-List DistinguishedName, ExternalDirectoryObjectId
    

이 예제에서는 그룹 멤버의 ID를 찾습니다.

  • 원격 PowerShell 세션에서: Get-PSSession cmdlet을 사용하여 라는 $Session변수에 원격 PowerShell 세션 세부 정보를 저장한 다음, 다음 명령을 실행합니다.

    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을 사용하여 라는 $Session변수에 원격 PowerShell 세션 세부 정보를 저장한 다음, 다음 명령을 실행합니다.

    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을 사용하여 라는 $Session1변수에 첫 번째 원격 PowerShell 세션 세부 정보를 저장합니다.

    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. 다음 명령을 실행하여 라는 $ConnectionInfo1 변수에 첫 번째 REST API 연결 세부 정보를 저장합니다.

      $ConnectionInfo1 = Get-ConnectionInformation | Where-Object { $_.ModulePrefix -eq "C1"}
      
    3. 두 번째 Connect-ExchangeOnline 명령에서 C2 값과 함께 매개 변수 접두 사를 사용합니다.

    4. 다음 명령을 실행하여 라는 $ConnectionInfo2 변수에 두 번째 REST API 연결 세부 정보를 저장합니다.

      $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