建立一个脚本,将AD中的组成员从一个用户复制到另一个用户。我试图使用powershell来自动完成这项任务。但是在为用户创建检查时卡住了。换句话说,当我将组成员资格从一个用户复制到另一个用户时,我希望能够在添加之前运行检查以查看用户是否已经是该组的成员,这样做可以避免错误,例如&# 34;此用户已经是该群组的成员,无法再次添加"任何帮助或建议将不胜感激。我现在使用以下脚本。
$copy = Read-host "Enter user to copy from"
$Sam = Read-host " Enter user to copy to"
Function Copymembership {
$members = Get-ADUser -Identity $copyp -Properties memberof
foreach ($groups in $members.memberof){
if ($members -notcontains $groups.sAMAccountname)
{Add-ADGroupMember -Identity $groups -Member $sam -ErrorAction SilentlyContinue
Write-Output $groups}
}
}
copymembership
答案 0 :(得分:7)
为两个用户使用Get-ADUser。然后使用-notcontains运算符来过滤组。
$CopyFromUser = Get-ADUser JSmith -prop MemberOf
$CopyToUser = Get-ADUser MAdams -prop MemberOf
$CopyFromUser.MemberOf | Where{$CopyToUser.MemberOf -notcontains $_} | Add-ADGroupMember -Member $CopyToUser
答案 1 :(得分:2)
你的代码太复杂了。不确定是否可以在没有import-Module AciveDirectory
cmdlet的情况下完成。
导入ActiveDirectory工具并使用内置cmdlet时,这样做更容易。检查我的代码:
# import the Active Directory module in order to be able to use get-ADuser and Add-AdGroupMembe cmdlet
import-Module ActiveDirectory
# enter login name of the first user
$copy = Read-host "Enter username to copy from: "
# enter login name of the second user
$paste = Read-host "Enter username to copy to: "
# copy-paste process. Get-ADuser membership | then selecting membership | and add it to the second user
get-ADuser -identity $copy -properties memberof | select-object memberof -expandproperty memberof | Add-AdGroupMember -Members $paste
答案 2 :(得分:1)
获取用户成员的一行。
Get-ADUser -Identity alan0 -Properties memberof | Select-Object -ExpandProperty memberof
将成员资格从一个用户复制到另一个用户的一行。
Get-ADUser -Identity <UserID> -Properties memberof | Select-Object -ExpandProperty memberof | Add-ADGroupMember -Members <New UserID>
答案 3 :(得分:0)
这样的事情应该告诉你一个团体是否包含一个特定的成员:
If ((Get-ADGroup "Domain Admins" -Properties Members).Members -Contains (Get-ADUser "AdminBob").DistinguishedName) {write-host "Yes"}
可能会有更简单的东西,但这是我想到的第一件事。
答案 4 :(得分:0)
PARAM ( [参数(强制性= $真)] [字符串] $ CopyFromUser, [参数(强制性= $真)] [字符串] $ CopyToUser )
$FromUserGroups = (Get-ADUser $CopyFromUser -Properties MemberOf).MemberOf
$CopyToUser = Get-ADUser $CopyToUser -Properties MemberOf
$FromUserGroups | Add-ADGroupMember -Members $CopyToUser
答案 5 :(得分:0)
如果您想对添加的组进行手动控制,那么这是 Out-GridView 的完美示例。过程与上面的TheMadTechnician所解释的相同,就在将其传递给Add-ADGroupMember之前,您插入Out-GridView。您甚至可以包含组说明或其他参数。
$CopyFromUser = Get-ADUser JSmith -prop MemberOf
$CopyToUser = Get-ADUser MAdams -prop MemberOf
$MissingGroups = Compare-Object $CopyFromUser $CopyToUser -Property MemberOf | ? SideIndicator -eq '<='
$GroupsObj = $MissingGroups.MemberOf | Get-ADGroup –prop Description | Select Name,Description
$GroupsObj | Out-GridView -PassThru | Add-ADGroupMember -Member $CopyToUser