I am trying to write a script that will take user input and copy the computer group memberships from one PC to another. However, it keeps failing to locate the PCs. Here is the code:
$sourcepc = Read-Host "Please enter the name of the PC being replaced"
$destpc = Read-Host "Please enter the name of the new PC"
# Get the memberships from the source computer account
$memberships = Get-ADComputer $sourcepc -Properties memberof | Select-Object -ExpandProperty memberof
$destpcname = Get-ADComputer $destpc -Properties SamAccountName | Select-Object -ExpandProperty SamAccountName
# Apply the memberships to the destination computer account
Add-ADGroupMember -Identity $destpcname -Members $memberships
Here is the error I get:
Add-ADGroupMember : Cannot find an object with identity: 'HINTONPC08-NEW$' under: 'DC=LCMH,DC=COM'.
At \\gogo\Software\HELP\NewBuild\PreDomain\Sham scripts\MembershipCopy.ps1:10 char:1
+ Add-ADGroupMember -Identity $destpcname -Members $memberships
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (HINTONPC08-NEW$:ADGroup) [Add-ADGroupMember], ADIdentityNotFoundExcepti
on
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember
You have the parameters mixed-up for the Add-ADGroupMember
cmdlet.
The -Identity
parameter is the id of the group and the -Members
parameter is for the (in this case) computer object.
Because Add-ADGroupMember can only take one single group identity you need to loop over the groups.
# Apply the memberships to the destination computer account
$memberships | ForEach-Object {
$_ | Add-ADGroupMember -Members $destpcname
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.