我经常需要从多个不同的组中删除多个不同的用户,并且到目前为止一直使用dsmod命令脚本来完成它。但是,我一直在尝试使用一些PowerShell魔法进行升级,并且可能会加快这个过程。我还需要脚本一起或单独输出成功和失败的操作日志。
我正在使用的输入文件是.csv,第一列中的用户ID和第二列中的组名称。我正在使用的测试输入文件同时具有distinguishedname和samaccountname,以便我可以看到它将如何接受输入。另外,我们在同一页面上编辑了输出错误以保护我们的域名信息,但相信那里有什么是公平的表示它应该是什么。
到目前为止,我发现了一些我尝试过的变种。第一个是:
$fileList = import-CSV -Delimiter ',' c:\temp\Logs\RemoveUserInput.csv
foreach($entry in $fileList)
{
$user = $entry.UserID
$group = $entry.Group
remove-adgroupmember -Identity $group -Member $user -Confirm:$false
}
我得到的错误是:
PS C:\Temp\Logs> .\remove_users_from_group.ps1
remove-adgroupmember : Cannot find an object with identity:
'cn=user1,dc=domain,dc=com' under:
'DC=domain,DC=com'.
At C:\Temp\Logs\remove_users_from_group.ps1:7 char:3
+ remove-adgroupmember -Identity $group -Member $user -Confirm:$false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (cn=user1,dc=domain,dc=c
om:ADPrincipal) [Remove-ADGroupMember], ADIdentityNotFoundException
+ FullyQualifiedErrorId : SetADGroupMember.ValidateMembersParameter,Micros
oft.ActiveDirectory.Management.Commands.RemoveADGroupMember
remove-adgroupmember : Cannot find an object with identity: 'user1' under:
'DC=domain,DC=com'.
At C:\Temp\Logs\remove_users_from_group.ps1:7 char:3
+ remove-adgroupmember -Identity $group -Member $user -Confirm:$false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (user1:ADPrincipal) [Remove-ADG
roupMember], ADIdentityNotFoundException
+ FullyQualifiedErrorId : SetADGroupMember.ValidateMembersParameter,Micros
oft.ActiveDirectory.Management.Commands.RemoveADGroupMember
一位同事提供了脚本的变体,建议部分问题可能在于我们的多域林,并从domain2中的组中删除domain1中的用户。脚本变体如下:
Import-Module ActiveDirectory
$FileList = Import-CSV -Delimiter ',' C:\temp\Logs\RemoveUserInput.csv
# Create new ADSearcher Object – could also be done differently
$objForest = [system.directoryservices.activedirectory.forest]::GetCurrentForest()
$ForestDefaultNC = "DC="+$objForest.RootDomain.Name.Replace(".",",DC=")
$ADSearcher = New-Object System.DirectoryServices.DirectorySearcher
$ADSearcher.PageSize = 1000
foreach($entry in $FileList) {
$UserName = $entry.UserID
$GroupName = $entry.Group
$GroupFilter = "(&(objectClass=group)(Name=$GroupName))"
$ADSearcher.Filter = $GroupFilter
$ADSearcher.SearchRoot = New-Object ADSI("GC://$($ForestDefaultNC)")
$Group = $ADSearcher.FindAll()
$UserFilter = "(&(objectCategory=person)(objectClass=user)(Name=$UserName))"
$ADSearcher.Filter = $UserFilter
$ADSearcher.SearchRoot = New-Object ADSI("GC://$($ForestDefaultNC)")
$User = $ADSearcher.FindAll()
Remove-ADGroupMember -Identity $Group -Members $User
}
接下来的错误是:
PS C:\Temp\Logs> .\2remove_users_from_group.ps1
Remove-ADGroupMember : Cannot bind parameter 'Identity'. Cannot convert the
"System.DirectoryServices.SearchResultCollection" value of type
"System.DirectoryServices.SearchResultCollection" to type
"Microsoft.ActiveDirectory.Management.ADGroup".
At C:\Temp\Logs\2remove_users_from_group.ps1:28 char:36
+ Remove-ADGroupMember -Identity $Group -Members $User
+ ~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Remove-ADGroupMember], Par
ameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.ActiveD
irectory.Management.Commands.RemoveADGroupMember
Remove-ADGroupMember : Cannot bind parameter 'Identity'. Cannot convert the
"System.DirectoryServices.SearchResultCollection" value of type
"System.DirectoryServices.SearchResultCollection" to type
"Microsoft.ActiveDirectory.Management.ADGroup".
At C:\Temp\Logs\2remove_users_from_group.ps1:28 char:36
+ Remove-ADGroupMember -Identity $Group -Members $User
+ ~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Remove-ADGroupMember], Par
ameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.ActiveD
irectory.Management.Commands.RemoveADGroupMember
可以提供任何可以提供的帮助。
编辑:根据要求,下面是.csv文件的示例:
它很简单,可能与你想象的完全一样:
UserID,group
"cn=user1,dc=domain,dc=com","cn=group1,dc=domain,dc=com"
user1,group1
答案 0 :(得分:0)
FindAll()方法的结果是SearchResultCollection类型,因为它可以包含多个用户。
要在Remove-ADGroupMember中使用,您需要获取目录条目或传递一些属性,如专有名称。
根据MS Docs,身份可以是以下之一:
您可以通过其可分辨名称(DN),GUID,安全标识符(SID)或安全帐户管理器(SAM)帐户名来标识组。
所以例如:
$GroupDN = ($ADSearcher.FindAll() | select -first 1).Properties.distinguishedname
$UserDN = ($ADSearcher.FindAll() | select -first 1).Properties.distinguishedname
但是如果输入CSV中已经有DN,并且您不想明确检查组/用户是否存在,则可以使用DN调用Remove-ADGroupMember,如果对象不存在则捕获错误。