Functions/New-MgGroupMemberCustom.ps1

function New-MgGroupMemberCustom {
    <#
    .SYNOPSIS
        A short one-line action-based description, e.g. 'Tests if a function is valid'
    .DESCRIPTION

    .NOTES

    .LINK

    .EXAMPLE
        $Members = @("Device","username@test.com","TestGroup2")
        New-MgGroupMemberCustom -GroupName "TestGroup" -Members $Members
    .EXAMPLE
        New-MgGroupMemberCustom -GroupName "TestGroup" -Members "Username@test.com"
    #>



    [CmdletBinding()]
    param (
        [Parameter(Mandatory)] [string] $GroupName,
        [Parameter(Mandatory)] [string[]] $Members
    )

    $Result = @()

    if (!(Get-MgContext)) {
        Write-Error "No connection to MgGraph"
        break
    }
    try {
        $MgGroup = Get-MgGroup -Filter "displayname eq '$($GroupName)'"
    } catch {
        $_
        break
    }
    $MgGroupMembers = Get-MgGroupMember -GroupId $MgGroup.Id

    $MgMember = @()
    foreach ($m in $Members) {
        try {
            $MgMember += Get-MgUser -UserId $m -ea 0
        } catch {
            # $_
        }
        try {
            $MgMember += Get-MgDevice -Filter "displayname eq '$($m)'"
        } catch {
            # $_
        }
        try {
            $MgMember += Get-MgGroup -Filter "displayname eq '$($m)'"
        } catch {
            # $_
        }
    }



    foreach ($m in $Members) {

        # Check if AzureAD objct was found
        if (($MgMember.DisplayName -contains $m) -or ($MgMember.UserPrincipalName -contains $m)) {

            # Select current member from group
            $CurrentMgMember = $MgMember | Where-Object DisplayName -EQ $m
            if (!($CurrentMgMember)) {
                $CurrentMgMember = $MgMember | Where-Object UserPrincipalName -EQ $m
            }

            if ($MgGroupMembers.Id -contains $CurrentMgMember.Id) {
                $r = [PSCustomObject]@{
                    Result            = "AlreadyMember"
                    Id                = $CurrentMgMember.Id
                    DisplayName       = $CurrentMgMember.DisplayName
                    UserPrincipalName = $CurrentMgMember.UserPrincipalName
                }
                $Result += $r
            } else {
                try {
                    New-MgGroupMember -GroupId $MgGroup.Id -DirectoryObjectId $CurrentMgMember.Id
                    $r = [PSCustomObject]@{
                        Result            = "Added"
                        Id                = $CurrentMgMember.Id
                        DisplayName       = $CurrentMgMember.DisplayName
                        UserPrincipalName = $CurrentMgMember.UserPrincipalName
                    }
                    $Result += $r
                } catch {
                    Write-Error $_
                }

            }
        } else {
            $r = [PSCustomObject]@{
                Result = "NotFound"
                Id     = $m
            }
            $Result += $r
        }

    }

    $Result = $Result | Sort-Object Result

    return $Result
    #Debug
    # $MgGroup | Format-Table
    # $MgMember | Select-Object Id, DisplayName, UserPrincipalName | Format-Table

}