Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 84
  • Last Modified:

Error Importing Computer Accounts From a Text File using Add-ADGroupMember

I have a text file with over 100 computer accounts which I am trying to add to a Global group. I have already declared my variable that reads the context of the text file and confirmed it is working.  When I use the syntax below though to added the account to a group I get the message: "Add-ADGroupMember : Cannot find an object with identity: 'AR641G' under: 'DC=Domain,DC=Domain1,DC=COM'

Add-ADGroupMember -Identity GlobalGroup1 -Member $devices -WhatIf

I have an empty parent domain and all counts are in the child domain which I am running the command from. I have read article online stating the -members property can only accept: DN,GUID or SID's and not just the name field is this correct and another stating that an array needs to be used? Which is correct? I am still not that strong in powershell so if someone could point me in the right direction would be very much appreciated.
0
compdigit44
Asked:
compdigit44
  • 17
  • 11
1 Solution
 
yo_beeDirector of ITCommented:
Can you post your full script.

Here is an example that you want to follow.

$grp = 'GroupName'

Import-Module ActiveDirectory 
$comps=Get-Content names.txt 

$grpDN = (get-adgroup $grp).distinguishedname

foreach ($comp in $comps)
{$dns=get-aduser $comp
$b=$dns.distinguishedname
Add-ADGroupMember -Identity  $grpDN -member $dns 
}

Open in new window

0
 
compdigit44Author Commented:
My whole script is are the two lines below
$devices =get-content C:\workstationlist.txt
Add-ADGroupMember -Identity GlobalGroup1 -Member $devices

Open in new window


What do I need to do a foreach loop? Can I run the script you listed using -whatif first?
0
 
yo_beeDirector of ITCommented:
$devices =get-content C:\workstationlist.txt
Foreach ($device in $devices) 
{Add-ADGroupMember -Identity GlobalGroup1 -Member $devices
}

Open in new window

0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
compdigit44Author Commented:
Thanks!!!! for my own understanding, why do I need to do a foreach loop
0
 
yo_beeDirector of ITCommented:
The $devices is your first step to build your array and the foreach loop goes through the array.

So $devices contains all the usernames you want to apply.

By you doing what you had without the loop actions the $devices is one big string that does not match any AD objects.

Does that help?
0
 
yo_beeDirector of ITCommented:
Also the -whatif would need to be nested at the end of the Add-adgroupmemeber -identity groupname -name $device -whatif. The -whatif is for testing and nothing applies when you run it.
0
 
footechCommented:
The help for the cmdlet describes what is needed for the -Identity and -Members parameters.  Here's a couple extracted bits.  And yes, you can submit an array for the -Members.
You can identify a group by its distinguished name (DN), GUID, security identifier (SID) or Security Accounts Manager (SAM) account name.

 You can identify a new member by its distinguished name (DN), GUID, security identifier (SID) or SAM account name.

It's more efficient to submit all the members as a single array in one Add-ADGroupMember command, rather than running the command once for each member.  However, since you're getting the members from a file, I see the possibility that some of the entries may not be valid (depending on how the file came to be).  And if that's the case, I haven't tested (or at least I haven't in recent memory) whether an invalid member(s) causes the entire command to fail, or if it just fails adding the invalid member.  If the entire command fails, then looping through the (potential) members one at a time and adding them at least allows the command to succeed for all valid members.

BTW, in yo_bee's last code post, on line 3, $devices should be $device.
0
 
yo_beeDirector of ITCommented:
Thanks for the catch.
0
 
compdigit44Author Commented:
Wow thank you both... I was under the assumption that if I read a text file into memory I could easily reference it in Add-ADGroupMember but I guess not.

I tried using the syntax that yo_bee posted earlier and not the same error..
$devices =get-content C:\workstationlist.txt
Foreach ($device in $devices) 
{Add-ADGroupMember -Identity GlobalGroup1 -Member $devices
}

Open in new window

0
 
yo_beeDirector of ITCommented:
You need to loop through that data.  This would be the case with any scripting.
Anytime you have a list of items you need to say "I want to do this for each item".

Good luck
Edited:

$devices =get-content C:\workstationlist.txt
Foreach ($device in $devices) 
{Add-ADGroupMember -Identity GlobalGroup1 -Member $device
}

Open in new window

0
 
compdigit44Author Commented:
Same error ...:-(
0
 
yo_beeDirector of ITCommented:
can you post your Txt File?
0
 
compdigit44Author Commented:
Here you go..
devies.txt
0
 
yo_beeDirector of ITCommented:
Are you using standard powershell or Active Dircetory version?

If you are using the standard Powershell you need to import the ActiveDirectory Module

Add to the top your script.
Import-module ActiveDirectory
0
 
yo_beeDirector of ITCommented:
Try this
Import-Module ActiveDirectory

$devices = Get-Content -Path 'C:\Users\xxxxx\Downloads\Computers.txt'
$grp = (Get-ADGroup -Identity 'Trial_Team').distinguishedname

Foreach ($Device in $devices)

{
$DN = (Get-ADComputer -Identity $device).distinguishedname

Add-ADGroupMember -Identity $grp -Members $dn}

Open in new window

0
 
compdigit44Author Commented:
Thanks here is the message I am getting now...

Get-ADComputer : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for th
argument, and then try running the command again.
At C:\AddingDevicesFromFileToGroup.ps1:9 char:33
+ $DN = (Get-ADComputer -Identity $device).distinguishedname
+                                 ~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-ADComputer], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADCom
   uter

Add-ADGroupMember : Cannot find an object with identity: 'A43TR' under: 'DC=Domain,DC=Domain1,DC=COM'.
At C:\AddingDevicesFromFileToGroup.ps1:11 char:1
+ Add-ADGroupMember -Identity $grp -Members $workstation -whatif}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (A43TR1:ADPrincipal) [Add-ADGroupMember], ADIdentityNotFoundException
    + FullyQualifiedErrorId : SetADGroupMember.ValidateMembersParameter,Microsoft.ActiveDirectory.Management.Commands
   AddADGroupMember
0
 
yo_beeDirector of ITCommented:
Do you have the AD powershell installed on the computer you are running it from?
0
 
yo_beeDirector of ITCommented:
Please post your script as well.
0
 
compdigit44Author Commented:
Yes AD powershell is installed on my workstation... Below is the content of the powershell script I am using...

Import-Module ActiveDirectory

$devices = Get-Content -Path 'C:\workstations.txt'
$grp = (Get-ADGroup -Identity 'ExternalTESTing').distinguishedname

Foreach ($workstation in $devices)

{
$DN = (Get-ADComputer -Identity $device).distinguishedname

Add-ADGroupMember -Identity $grp -Members $workstation -whatif}

Open in new window

0
 
yo_beeDirector of ITCommented:
Change $workstation to $DN
0
 
yo_beeDirector of ITCommented:
On the the line with ADD-Adgroupmember
0
 
yo_beeDirector of ITCommented:
The $grp and $dn is getting the DistinguishedName attribute parsed for the Add-Groupmember cmdlet part so you do not get those errors
0
 
compdigit44Author Commented:
Getting same exact message as I posted before and this is after making the changes you recommended
0
 
compdigit44Author Commented:
I got it I had a typo!!!

I found when doing the -whatif it only states performing set action then the DN of the group but does not list the devices that would have been added
0
 
yo_beeDirector of ITCommented:
If you have concern, I would create a test group and run it against that group to see if it adds the objects.

I ran my test script successfully so you should have no issues if you follow my script.
0
 
compdigit44Author Commented:
I ran the script as well. I just though whatif would like on screen all devices that it would be adding... :o)
0
 
yo_beeDirector of ITCommented:
I just tested the script with -whatif it just states it is set perform on the group.

I would try this on a test group without the -whatif
0
 
compdigit44Author Commented:
The script does work as I already testing it I was just confused as to the results of the whatif and expected more output...

Thanks again..
0
 
yo_beeDirector of ITCommented:
Glad to help.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 17
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now