如何解决使用两个PowerShell数组重命名AD组
我正在尝试以这种方式重命名现有的广告组。
以#开头的AD组将重命名为没有#的相同名称。例如,我有#dl1 ,我希望将其重命名为 dl1 (省略#)
我正在尝试重命名以下四个用户。
我以这种方式写了两个数组。 ( $ myArray 和 $ myArray2 )。
$myArray =@(
$data = Get-ADGroup -Filter {name -like "#*"} |select samaccountname
$data.samaccountname | foreach {$_.split("#")[1]
}
)
$myArray2 =@(
$assdf=Get-ADGroup -Filter {name -like "#*"}
$myArray2 =@($assdf)
$num=0
foreach($a in $assdf)
{
$myArray2[$num]
$num=$num+1
}
)
如果我打印 $ myarray ,它将以这种方式给出准确的结果。
,如果我打印 $ myarray2 ,它也会以这种方式给出所需的结果,
难题的缺失部分是将这两个数组结合起来以运行最终的命令
set-adgroup -identity (members indide $myArray2) -samaccountname (members indide $myArray)
几个小时以来,我尝试了多种方法来获取 set-adgroup ..用于每个循环等。
例如
$a=0
foreach ($item in $myArray2)
{
$nameto_replace=$myArray[$a]
Set-adgroup -identity $item.samaccountname -samaccountname $nameto_replace
$a=$a+1
}
请问有人可以帮忙一下吗?我现在完全没有主意了。预先感谢
解决方法
无需执行两次Get-ADGroup
,您只需使用一次即可在ForEach-Object循环中遍历结果:
已根据Aravinda's helpful observation
更新Get-ADGroup -Filter "Name -like '#*'" | ForEach-Object {
$newName = $_.Name.TrimStart('#')
Write-Host "Renaming group $($_.Name).. to '$newName'"
# replace only the SamAccountName
$_ | Set-ADGroup -SamAccountName $newName
# or replace multiple properties at the same time.
# You need to use the LDAP names here,so mind the casing !
# See http://www.selfadsi.org/group-attributes.htm
# $_ | Set-ADGroup -Replace @{sAMAccountName = $newName; displayName = $newName}
}
如果需要,可以通过将OU的DistinguishedName添加到-SearchBase
参数来将搜索限制为指定的OU
Theo的答案很棒!
以下是我从theo的答案中得出的最后一个答案。
Get-ADGroup -Filter "Name -like '#*'" | ForEach-Object {
$newName = $_.Name.TrimStart('#')
$_ | Set-ADGroup -Replace @{sAMAccountName = $newName;displayName = $newName}
$_ | Rename-ADObject -NewName $newName
}
如果您尝试使用set-adgroup更改“名称” 和“ CN” ,则会出现以下错误。
"Set-ADGroup : The directory service cannot perform the requested operation on the RDN attribute of an object"
要更改多个属性,尤其是名称和 CN ,可以使用Rename-ADObject
和Set-ADGroup
的组合。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。