将特定AD OU的用户添加到另一个AD组的脚本

时间:2019-03-27 16:15:11

标签: powershell active-directory

以管理员身份运行Powershell

我想要一个每天运行的脚本,以将用户从“ cn = users,dc = costco,dc = com”添加到AD组“ groupname”中。CN= groupname,OU = Groups,DC = costco,DC = com“

$When = (Get-Date).AddDays(-1).Date
Get-ADUser -SearchBase 'cn=users,dc=costco,dc=com' -Filter { whenCreated -ge $When } | add-adgroupmember -MemberOf 'groupname'

出现错误

  

Add-ADGroupMember:找不到与参数匹配的参数   名称为“ MemberOf”。在第2行char:111   + ... ilter {whenCreated -ge $ When} | add-adgroupmember -MemberOf'groupname ...   + ~~~~~~~~~~       + CategoryInfo:InvalidArgument:(:) [Add-ADGroupMember],ParameterBindingException       + FullyQualifiedErrorId:NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember

我也曾在Redditor的帮助下尝试过

When = (Get-Date).AddDays(-1).Date
Get-ADUser -SearchBase 'CN=users,dc=costo,dc=com' -Filter { whenCreated -ge $When } | ForEach-Object { Add-ADGroupMember -Identity 'Groupname' -Members $_ }

错误:

  

Add-ADGroupMember:访问权限不足,无法执行   操作在第2行char:109   + ... ach-Object {Add-ADGroupMember -Identity'groupname'-Members $ _} ...   + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~       + CategoryInfo:未指定:(groupname:ADGroup)[Add-ADGroupMember],ADException       + FullyQualifiedErrorId:ActiveDirectoryServer:8344,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember

1 个答案:

答案 0 :(得分:0)

要使第一个示例正常工作,您需要向命令提供管道值,而不是尝试在实际管道中传递它。

尝试:

TIMESTAMP