Forum Discussion

Himanshu Singh's avatar
Himanshu Singh
Iron Contributor
Apr 17, 2020

Microsoft Teams Report extracting owner email address

Hello,

 

It is sad and strange that most of the Exchange online commandlets do not show users email addresses

for e.g. Get-UnifiedGroups or Get-Recipient etc....

Also at the time as the number of teams increase on the tenant it is impossible to generate reports showing Team details and its owner details in email format ???

BR,

/HS

5 Replies

  • Hello Himanshu Singh , what you mean by not displaying the emails? have you tried selecting the desired object attribute like:

     

    get-recipient | select PrimarySMTPAddress

     

    For the Unified groups, you get the unified group primary SMTP address the same way

    Get-UnifiedGroup | select PrimarySMTPAddress

     

    and for the members, you can pipe it like this.

     

    Get-UnifiedGroup | Get-UnifiedGroupLinks -LinkType Member | select PrimarySMTPAddress

     

    For your last question, you can use this code to generate the report displaying the owner's email address using the above examples. 

     

    $Groups = Get-UnifiedGroup | select managedby, DisplayName #Here you can select and filter the groups that you need a report 
    $Result = @()
    
    Foreach($Group in $Groups)
     {
      $MailsOwners = @()
     
      Foreach($Owner in $Group.ManagedBy)
       {
        $MailOwner   = $null
        $MailOwner   = Get-Recipient $Owner | select PrimarySMTPAddress
        $MailsOwners += $MailOwner.PrimarySMTPAddress
       }
      
      $MailsOwners = $MailsOwners -join ";"
      $Props = $null
      $Props = [ordered]@{
                          GroupDisplayName = $Group.DisplayName
                          GroupOwnersMails = $MailsOwners   
                         }
    
      $Outputtmp  =  New-Object PSObject -Property $Props
      $Result    +=  $Outputtmp
     }
    
     $Result | Export-Csv "Report.csv" -NoTypeInformation -Encoding UTF8

     

    Please let me know if you have any further questions about this. 

     

    Regards

    Erick A. Moreno

    • Himanshu Singh's avatar
      Himanshu Singh
      Iron Contributor

      Many thanks i am testing your script, however my real issue is the time this reports takes to finish i have more 10K teams or say even more O365 groups it takes more  2-3 hrs for this report if it completes, as for last few times powershell session disconnects before the script can generate all details

       

      however to save sometime i have attempted to take only one owner for each group see following and suggest further optimization if possible

       

      Import-Module $((Get-ChildItem -Path $($env:LOCALAPPDATA+"\Apps\2.0\") -Filter Microsoft.Exchange.Management.ExoPowershellModule.dll -Recurse ).FullName|?{$_ -notmatch "_none_"}|select -First 1)
      $Session=New-ExoPSSession
      Import-PSSession $Session -AllowClobber -Verbose
      $teamsgroups = (Get-UnifiedGroup -ResultSize Unlimited | Select DisplayName,Alias,Name,ResourceProvisioningOptions,AccessType,Classification,Language,ExchangeGuid,ManagedBy)
      #notification for powershell
      Write-Host "Getting Office 365 Groups created with MS Teams... " -ForegroundColor Green
      #generate array for teamsgroups
      $teams = @()
      #loop to get information
      ForEach ($group in $teamsgroups){
      #get-members of group
      $identity = (($group).ExchangeGuid)
      $owners = (Get-UnifiedGroupLinks -Identity "$identity" -LinkType Owners | Select-Object PrimarySMTPAddress)
      $owner = (($owners[0]).primarySMTPAddress) | Out-String
      # Adding pscustomobjets entries to array
      $teams += [pscustomobject]@{
      DisplayName = ($group).DisplayName
      Name = ($group).Name
      Alias = ($group).Alias
      AccessType = ($group).AccessType
      Classification=($group).classification
      CreationApp=($group).resourceprovisioningoptions
      Language = ($group).Language
      Owner = ("$owner")
      }
      }
      #out put in GridView (external window)
      Write-Host "Display in Grid View Window " -ForegroundColor Green
      $teams | Out-GridView -Title "All Office365 Groups created in MS Teams"
      #out put in powershell
      $teams | Export-Csv C:\tools\TeamsClassi.csv -NoTypeInformation

       

      BR,

      /HS

       

      • Erick A. Moreno R.'s avatar
        Erick A. Moreno R.
        Iron Contributor

        Himanshu Singh, You can check for the PSsession status before you get the owners every iteration. 

         

        $PSSessionStatus = $(Get-PSSession -Name $Session.Name ).State 

         

        If($PSSessionStatus -eq Opened)

         {

          Run the code to obtain de owners

         }

        Else

         {

         #Create a new session and run the code to obtain the owners

        }

         

        As an alternative, you can create a counter that increments every time you iterate inside the loop and every N number of iterations you close and reopen the PSsession (Refreshing it). 

         

        Also to reduce loading times I will recommend only import the cmdlets that you are using. 

        Import-PSSession $Session -AllowClobber -Verbose -CommandName "Get-UnifiedGroupLinks"

         

        Regards

        Erick A. Moreno

         

Resources