Public/Find-GraphGroup.ps1

# Module: Orbit
# Function: Lookup
# Author: David Eberhardt
# Updated: 24-JAN-2021
# Status: Live

#TODO Rework to Find-GraphGroup - evaluate if even needed - Get-MgGraph has Filter and Search - do these work like this function?


function Find-GraphGroup {
  <#
  .SYNOPSIS
    Returns an Object if an Graph Group has been found
  .DESCRIPTION
    Simple lookup - does the Group Object exist - to avoid TRY/CATCH statements for processing
  .PARAMETER Identity
    Mandatory. String to search. Provide part or full DisplayName, MailAddress or MailNickName
    Returns all matching groups
  .EXAMPLE
    Find-GraphGroup [-Identity] "My Group"
 
    Will return all Groups that have "My Group" in the DisplayName, ObjectId or MailNickName
  .EXAMPLE
    Find-GraphGroup -Identity "MyGroup@domain.com"
 
    Will return all Groups that match "MyGroup@domain.com" in the DisplayName, ObjectId or MailNickName
  .INPUTS
    System.String
  .OUTPUTS
    System.Object
  .NOTES
    None
  .COMPONENT
    UserManagement
  .FUNCTIONALITY
    Queries Group Objects in Azure Ad with different mechanics
  .LINK
    https://github.com/DEberhardt/Orbit/tree/main/docs/Orbit.Groups/Find-GraphGroup.md
  .LINK
    https://github.com/DEberhardt/Orbit/tree/main/docs/about/about_UserManagement.md
  .LINK
    https://github.com/DEberhardt/Orbit/tree/main/docs/
  #>


  [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '', Justification = 'Required for performance. Removed with Disconnect-Me')]
  [CmdletBinding()]
  [OutputType([System.Object])]
  param(
    [Parameter(Mandatory, Position = 0, ValueFromPipeline, HelpMessage = 'This is the Name or MailAddress of the Group')]
    [Alias('GroupName', 'Name')]
    [string]$Identity
  ) #param

  begin {
    Show-OrbitFunctionStatus -Level Live
    Write-Verbose -Message "[BEGIN ] $($MyInvocation.MyCommand)"

    # Asserting Graph Connection
    if ( -not (Test-GraphConnection) ) { throw 'Connection to Microsoft Graph not established. Please validate connection' }

    # Loading all Groups
    if ( -not $global:OrbitQueryTenantGraphGroups) {
      Write-Verbose -Message 'Groups not loaded yet, depending on the size of the Tenant, this will run for a while!' -Verbose
      $global:OrbitQueryTenantGraphGroups = Get-MgGroup -All -WarningAction SilentlyContinue -ErrorAction SilentlyContinue
    }

    $Groups = $null

  } #begin

  process {
    Write-Verbose -Message "[PROCESS] $($MyInvocation.MyCommand)"
    foreach ($ID in $Identity) {
      [System.Collections.Generic.List[object]]$Groups = @()

      $Groups += $global:OrbitQueryTenantGraphGroups | Where-Object DisplayName -Like "*$ID*"
      $Groups += $global:OrbitQueryTenantGraphGroups | Where-Object Description -Like "*$ID*"
      $Groups += $global:OrbitQueryTenantGraphGroups | Where-Object Id -Like "*$ID*"
      $Groups += $global:OrbitQueryTenantGraphGroups | Where-Object Mail -Like "*$ID*"

      $MailNickName = $ID.Split('@')[0]
      $Groups += $global:OrbitQueryTenantGraphGroups | Where-Object Mailnickname -Like "*$MailNickName*"

      # Output - Filtering objects
      if ( $Groups ) {
        $Groups | Sort-Object -Unique -Property ObjectId | Get-Unique
      }
    }
  } #process

  end {
    Write-Verbose -Message "[END ] $($MyInvocation.MyCommand)"
  } #end
} # Find-GraphGroup