首页 > 解决方案 > 从 AD 组中删除成员而不使用模块

问题描述

我在不允许在服务器中安装/使用 powershell 模块(如活动目录模块)的环境中工作。

我要做的是删除存储在数组中的组成员。

因此,假设包含要删除的成员的数组称为 tobermoved:

$toberemoved = @("COMPUTER1","COMPUTER2","COMPUTER5")

和组被称为:全球计算机组:

$computergroup = "Global Computer Group"

我试图做的是:

$findgroup=[adsi]"LDAP://$computergroup"
foreach($item in $toberemoved) {
    $findgroup.Remove("LDAP://$item")
}

我也试过这个:

$findgroup=[adsi]"LDAP://$computergroup"

foreach($item in $toberemoved) {
     $base = "CN=$item,DC=Domain,OU=Computers,OU=local"
     $findgroup.Remove("LDAP://$item")
}

没有成功。

尝试在 PowerShell ISE 中运行它时,我得到的是服务器错误。关于可能出错的任何想法,或者在不使用 AD 模块的情况下执行此操作的其他想法?

标签: powershellactive-directory

解决方案


您是否尝试过使用完整的 ADsPath 名称?看看上面写的这篇很棒的文章。https://www.petri.com/managing-active-directory-groups-adsi-powershell

TLDR?

干得好:

[adsi]$Group = "LDAP://CN=GroupName,OU=OrganizationalUnit,DC=Your,DC=Domain,DC=com"
foreach($User in $toberemoved) { #Again, full ADsPaths
    $Group.Remove($User)
}

推荐阅读