@@ -11375,6 +11375,128 @@ http://richardspowershellblog.wordpress.com/2008/05/25/system-directoryservices-
1137511375 }
1137611376}
1137711377
11378+ function Remove-DomainGroupMember {
11379+ <#
11380+ .SYNOPSIS
11381+
11382+ Removes a domain user (or group) from an existing domain group, assuming
11383+ appropriate permissions to do so.
11384+
11385+ Author: Will Schroeder (@harmj0y)
11386+ License: BSD 3-Clause
11387+ Required Dependencies: Get-PrincipalContext
11388+
11389+ .DESCRIPTION
11390+
11391+ First binds to the specified domain context using Get-PrincipalContext.
11392+ The bound domain context is then used to search for the specified -GroupIdentity,
11393+ which returns a DirectoryServices.AccountManagement.GroupPrincipal object. For
11394+ each entry in -Members, each member identity is similarly searched for and removed
11395+ from the group.
11396+
11397+ .PARAMETER Identity
11398+
11399+ A group SamAccountName (e.g. Group1), DistinguishedName (e.g. CN=group1,CN=Users,DC=testlab,DC=local),
11400+ SID (e.g. S-1-5-21-890171859-3433809279-3366196753-1114), or GUID (e.g. 4c435dd7-dc58-4b14-9a5e-1fdb0e80d202)
11401+ specifying the group to remove members from.
11402+
11403+ .PARAMETER Members
11404+
11405+ One or more member identities, i.e. SamAccountName (e.g. Group1), DistinguishedName
11406+ (e.g. CN=group1,CN=Users,DC=testlab,DC=local), SID (e.g. S-1-5-21-890171859-3433809279-3366196753-1114),
11407+ or GUID (e.g. 4c435dd7-dc58-4b14-9a5e-1fdb0e80d202).
11408+
11409+ .PARAMETER Domain
11410+
11411+ Specifies the domain to use to search for user/group principals, defaults to the current domain.
11412+
11413+ .PARAMETER Credential
11414+
11415+ A [Management.Automation.PSCredential] object of alternate credentials
11416+ for connection to the target domain.
11417+
11418+ .EXAMPLE
11419+
11420+ Remove-DomainGroupMember -Identity 'Domain Admins' -Members 'harmj0y'
11421+
11422+ Removes harmj0y from 'Domain Admins' in the current domain.
11423+
11424+ .EXAMPLE
11425+
11426+ $SecPassword = ConvertTo-SecureString 'Password123!' -AsPlainText -Force
11427+ $Cred = New-Object System.Management.Automation.PSCredential('TESTLAB\dfm.a', $SecPassword)
11428+ Remove-DomainGroupMember -Identity 'Domain Admins' -Members 'harmj0y' -Credential $Cred
11429+
11430+ Removes harmj0y from 'Domain Admins' in the current domain using the alternate credentials.
11431+
11432+ .LINK
11433+
11434+ http://richardspowershellblog.wordpress.com/2008/05/25/system-directoryservices-accountmanagement/
11435+ #>
11436+
11437+ [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSShouldProcess', '')]
11438+ [CmdletBinding()]
11439+ Param(
11440+ [Parameter(Position = 0, Mandatory = $True)]
11441+ [Alias('GroupName', 'GroupIdentity')]
11442+ [String]
11443+ $Identity,
11444+
11445+ [Parameter(Mandatory = $True, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)]
11446+ [Alias('MemberIdentity', 'Member', 'DistinguishedName')]
11447+ [String[]]
11448+ $Members,
11449+
11450+ [ValidateNotNullOrEmpty()]
11451+ [String]
11452+ $Domain,
11453+
11454+ [Management.Automation.PSCredential]
11455+ [Management.Automation.CredentialAttribute()]
11456+ $Credential = [Management.Automation.PSCredential]::Empty
11457+ )
11458+
11459+ BEGIN {
11460+ $ContextArguments = @{
11461+ 'Identity' = $Identity
11462+ }
11463+ if ($PSBoundParameters['Domain']) { $ContextArguments['Domain'] = $Domain }
11464+ if ($PSBoundParameters['Credential']) { $ContextArguments['Credential'] = $Credential }
11465+
11466+ $GroupContext = Get-PrincipalContext @ContextArguments
11467+
11468+ if ($GroupContext) {
11469+ try {
11470+ $Group = [System.DirectoryServices.AccountManagement.GroupPrincipal]::FindByIdentity($GroupContext.Context, $GroupContext.Identity)
11471+ }
11472+ catch {
11473+ Write-Warning "[Remove-DomainGroupMember] Error finding the group identity '$Identity' : $_"
11474+ }
11475+ }
11476+ }
11477+
11478+ PROCESS {
11479+ if ($Group) {
11480+ ForEach ($Member in $Members) {
11481+ if ($Member -match '.+\\.+') {
11482+ $ContextArguments['Identity'] = $Member
11483+ $UserContext = Get-PrincipalContext @ContextArguments
11484+ if ($UserContext) {
11485+ $UserIdentity = $UserContext.Identity
11486+ }
11487+ }
11488+ else {
11489+ $UserContext = $GroupContext
11490+ $UserIdentity = $Member
11491+ }
11492+ Write-Verbose "[Remove-DomainGroupMember] Removing member '$Member' from group '$Identity'"
11493+ $Member = [System.DirectoryServices.AccountManagement.Principal]::FindByIdentity($UserContext.Context, $UserIdentity)
11494+ $Group.Members.Remove($Member)
11495+ $Group.Save()
11496+ }
11497+ }
11498+ }
11499+ }
1137811500
1137911501function Get-DomainFileServer {
1138011502<#
0 commit comments