Groups/New-GPPGroupMember.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
function New-GPPGroupMember {
    [OutputType('GPPItemGroupMember')]
    Param (
        [Parameter(ParameterSetName = 'ByName', Mandatory)]
        [string]$Name,
        [Parameter(ParameterSetName = 'BySID', Mandatory)]
        [System.Security.Principal.SecurityIdentifier]$SID,
        [Parameter(ParameterSetName = 'ByName')]
        [Parameter(ParameterSetName = 'BySID')]
        [GPPItemGroupMemberAction]$Action
    )

    if ($Name) {
        try {
            $SID = ([System.Security.Principal.NTAccount]::new($Name)).Translate([System.Security.Principal.SecurityIdentifier]).Value
        }
        catch {
            # https://github.com/exchange12rocks/PSGPPreferences/issues/31
            # Not all names should be resolved into SIDs. Especially names with GPP variables in them.

            $SID = $null
            if ($_.FullyQualifiedErrorId -ne 'IdentityNotMappedException') {
                throw $_
            }
        }
    }
    else {
        $Name = [System.Security.Principal.SecurityIdentifier]::new($SID).Translate([System.Security.Principal.NTAccount]).Value
    }

    [GPPItemGroupMember]::new($Action, $Name, $SID)
}