Groups/Remove-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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
function Remove-GPPGroupMember { [OutputType([System.Void])] Param ( [Parameter(ParameterSetName = 'ByNameGPONameGroupName', Mandatory)] [Parameter(ParameterSetName = 'ByNameGPOIdGroupName', Mandatory)] [Parameter(ParameterSetName = 'ByNameGPONameGroupSID', Mandatory)] [Parameter(ParameterSetName = 'ByNameGPOIdGroupSID', Mandatory)] [Parameter(ParameterSetName = 'ByNameGPONameGroupUID', Mandatory)] [Parameter(ParameterSetName = 'ByNameGPOIdGroupUID', Mandatory)] [string]$Name, [Parameter(ParameterSetName = 'BySIDGPONameGroupName', Mandatory)] [Parameter(ParameterSetName = 'BySIDGPOIdGroupName', Mandatory)] [Parameter(ParameterSetName = 'BySIDGPONameGroupSID', Mandatory)] [Parameter(ParameterSetName = 'BySIDGPOIdGroupSID', Mandatory)] [Parameter(ParameterSetName = 'BySIDGPONameGroupUID', Mandatory)] [Parameter(ParameterSetName = 'BySIDGPOIdGroupUID', Mandatory)] [System.Security.Principal.SecurityIdentifier]$SID, [Parameter(ParameterSetName = 'ByNameGPONameGroupName', Mandatory)] [Parameter(ParameterSetName = 'ByNameGPOIdGroupName', Mandatory)] [Parameter(ParameterSetName = 'BySIDGPONameGroupName', Mandatory)] [Parameter(ParameterSetName = 'BySIDGPOIdGroupName', Mandatory)] [string]$GroupName, [Parameter(ParameterSetName = 'ByNameGPONameGroupSID', Mandatory)] [Parameter(ParameterSetName = 'ByNameGPOIdGroupSID', Mandatory)] [Parameter(ParameterSetName = 'BySIDGPONameGroupSID', Mandatory)] [Parameter(ParameterSetName = 'BySIDGPOIdGroupSID', Mandatory)] [System.Security.Principal.SecurityIdentifier]$GroupSID, [Parameter(ParameterSetName = 'ByNameGPONameGroupUID', Mandatory)] [Parameter(ParameterSetName = 'ByNameGPOIdGroupUID', Mandatory)] [Parameter(ParameterSetName = 'BySIDGPONameGroupUID', Mandatory)] [Parameter(ParameterSetName = 'BySIDGPOIdGroupUID', Mandatory)] [guid]$GroupUID, [Parameter(ParameterSetName = 'ByNameGPONameGroupName', Mandatory)] [Parameter(ParameterSetName = 'ByNameGPONameGroupSID', Mandatory)] [Parameter(ParameterSetName = 'ByNameGPONameGroupUID', Mandatory)] [Parameter(ParameterSetName = 'BySIDGPONameGroupName', Mandatory)] [Parameter(ParameterSetName = 'BySIDGPONameGroupSID', Mandatory)] [Parameter(ParameterSetName = 'BySIDGPONameGroupUID', Mandatory)] [string]$GPOName, [Parameter(ParameterSetName = 'ByNameGPOIdGroupName', Mandatory)] [Parameter(ParameterSetName = 'ByNameGPOIdGroupSID', Mandatory)] [Parameter(ParameterSetName = 'ByNameGPOIdGroupUID', Mandatory)] [Parameter(ParameterSetName = 'BySIDGPOIdGroupName', Mandatory)] [Parameter(ParameterSetName = 'BySIDGPOIdGroupSID', Mandatory)] [Parameter(ParameterSetName = 'BySIDGPOIdGroupUID', Mandatory)] [guid]$GPOId, [Parameter(ParameterSetName = 'ByNameGPONameGroupName')] [Parameter(ParameterSetName = 'ByNameGPOIdGroupName')] [Parameter(ParameterSetName = 'ByNameGPONameGroupSID')] [Parameter(ParameterSetName = 'ByNameGPOIdGroupSID')] [Parameter(ParameterSetName = 'ByNameGPONameGroupUID')] [Parameter(ParameterSetName = 'ByNameGPOIdGroupUID')] [Parameter(ParameterSetName = 'BySIDGPONameGroupName')] [Parameter(ParameterSetName = 'BySIDGPOIdGroupName')] [Parameter(ParameterSetName = 'BySIDGPONameGroupSID')] [Parameter(ParameterSetName = 'BySIDGPOIdGroupSID')] [Parameter(ParameterSetName = 'BySIDGPONameGroupUID')] [Parameter(ParameterSetName = 'BySIDGPOIdGroupUID')] [GPPContext]$Context = $ModuleWideDefaultGPPContext ) if (-not $GPOId) { $GPOId = Convert-GPONameToID -Name $GPOName } $GroupsSection = Get-GPPSection -GPOId $GPOId -Context $Context -Type ([GPPType]::Groups) if ($GroupsSection) { $GetGPPGroupParameters = @{ GPPSection = $GroupsSection } if ($GroupUID) { $GetGPPGroupParameters.Add('UID', $GroupUID) } elseif ($GroupSID) { $GetGPPGroupParameters.Add('SID', $GroupSID) } else { $GetGPPGroupParameters.Add('LiteralName', $GroupName) } $FilteredGroups = Get-GPPGroup @GetGPPGroupParameters if ($FilteredGroups) { $WorkDone = $false foreach ($FilteredGroup in $FilteredGroups) { if ($FilteredGroup.Properties.Members) { $FilterScript = if ($SID) { {$_.sid -eq $SID} } else { {$_.name -eq $Name} } $FilteredMembers = $FilteredGroup.Properties.Members | Where-Object -FilterScript $FilterScript if ($FilteredMembers) { $WorkDone = $true foreach ($FilteredMember in $FilteredMembers) { [void]$FilteredGroup.Properties.Members.Remove($FilteredMember) } } } } if ($WorkDone) { Set-GPPSection -InputObject $GroupsSection -GPOId $GPOId -Context $Context -Type ([GPPType]::Groups) } } } } |