Functions/Groups/New-GraphGroupMember.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Function New-GraphGroupMember
{
    [CmdletBinding()]
    Param(      
        [Parameter(Mandatory = $true,
                   ValueFromPipeline = $true,
                   ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [string[]]$MemberId,

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$GroupId
    )

    process
    {

        foreach ($singleMemberId in $MemberId)
        {

            $body = @{
                "@odata.id" = "https://graph.microsoft.com/v1.0/directoryObjects/$singleMemberId"
            }

            $uri = $global:PowerGraph_BaseUrl + "groups/$GroupId/members/`$ref"

            Write-Verbose "Adding member with Id $singleMemberId to group with Id $GroupId ($($body | ConvertTo-Json))"

            Invoke-MSGraphRequest -Uri $uri -Method Post -Body ($body | ConvertTo-Json)
        }

    }
}