Archive/Add-JAzureADGroupMembers.ps1


function Add-JAzureADGroupMembers {

    [CmdletBinding()]
    param (
        [Parameter(Mandatory)] [string] $GroupName,
        [Parameter(Mandatory)] [array] $Members,
        [Parameter()] [ValidateSet("Users", "Devices")] [string] $MemberType = "Users"
    )



    $AzureADGroup = Get-AzureADGroup -Filter "DisplayName eq '$($GroupName)'"
    # $AzureADGroup
    <# $Members = @()

$Members += "Device01"

 " #>


    foreach ($m in $Members) {

        if ($MemberType -eq "Users") {
            $ThisMember = (Get-AzureADUser -ObjectId $m)
        } elseif ($MemberType -eq "Devices") {
            # $m
            $ThisMember = Get-AzureADDevice -Filter "DisplayName eq '$m'"
        }




        # $ThisMember | Format-Table
        try {
            Add-AzureADGroupMember -ObjectId $AzureADGroup.ObjectId -RefObjectId $ThisMember.ObjectId -ErrorAction Continue
        } catch {
            "Something went wrong, the object may already be a member of the group"
        }

    }


    $GroupMembers = Get-AzureADGroupMember -ObjectId $AzureADGroup.ObjectId

    $GroupMembers.Count


}