【问题标题】:Remove AD-user in domain A from AD-group in domain B while my client is in domain C in PowerShell当我的客户端在 PowerShell 中的域 C 中时,从域 B 中的 AD 组中删除域 A 中的 AD 用户
【发布时间】:2021-10-06 15:57:51
【问题描述】:

假设我在域 A 中有一个 AD 用户,该用户在域 B 的 AD 组中,而我的客户端在域 C 中。
我想在 PowerShell 中从组中删除用户,但我真的不知道如何使用 Remove-ADGroupMemberRemove-ADPrincipalGroupMembership 解决这个问题,因为我只能在那里传递一个域。

【问题讨论】:

  • 理论上,如果您使用对象的专有名称,它应该可以工作。我无法测试这个atm,但可以这样尝试:Get-ADgroup -Identity <DN of the group> | Remove-ADGroupMember -Members <DN of the user> 另外看看Remove-ADGroupMember 的官方文档:docs.microsoft.com/en-us/powershell/module/addsadministration/…
  • 我应该提到我知道这一点,但我的 SamAccountNames 和域是由数据库请求产生的,我想避免额外的请求以找到 DN。
  • 好吧,如果您至少有域的 DN,您可以使用 Partition 参数尝试它:Get-ADGroup -Identity <SAM in B> -Partition <DN of B> | Remove-ADGroupMember -Identity <SAM in A> -Partition <DN of A> 同样,我无法尝试这个,但它看起来可以工作:D
  • 怎么样:Get-ADGroup -Identity <SAM in B> -Server <domain B> | Remove-ADGroupMember -Members <SAM in A> -Server <domain C>

标签: powershell active-directory cross-domain


【解决方案1】:

我假设域 A 和 B 不在同一个林中,但是是受信任的。

问题在于您没有删除对用户的直接引用:您正在删除外部安全主体,它是与引用受信任域中的帐户的组位于同一域中的对象。我在我写的一篇文章中更详细地讨论了这一点:What makes a member a member?

似乎没有任何 PowerShell cmdlet 能够处理这些。但是您可以使用 .NET 的 DirectoryEntry 类来执行此操作。这是一个示例($username$groupname 是帐户和组的名称):

$u = Get-ADUser -Server domainA.com $username
#Get the Foreign Security Principal object for the user
$fsp = Get-ADObject -Server domainB.com -Filter "objectSid -eq '$($u.SID)'"

#Get the group
$g = Get-ADGroup -Server domainB.com $groupname

#Get a DirectoryEntry for the group (which is what the [ADSI] notation creates)
$group = [ADSI]"LDAP://domainB.com/$($g.DistinguishedName)"

#Remove the FSP from the group
$group.Properties["member"].Remove($fsp.DistinguishedName)
$group.CommitChanges()

【讨论】:

    【解决方案2】:

    您可以使用以下 cmdlet 从组中删除外部主体对象

    set-adgroup -remove @{member='cn=S1-5-blah-blah-blah'}
    

    步骤: 获取外来主要成员的 DistinguishedName

    $mem = (get-ADGroup 'GroupName' -property members).members |where {$_ -like "CN=S-1-5-21-BLAHBLAH"}
    
    set-ADGroup -remove @{member=$mem}
    

    您还可以使用Set-ADgroup -clean member 来清空组成员身份,包括外部主体对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-14
      • 2020-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-04
      • 1970-01-01
      相关资源
      最近更新 更多