Solved

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

Posted on 2016-10-15
29
39 Views
Last Modified: 2016-10-16
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
Comment
Question by:compdigit44
  • 17
  • 11
29 Comments
 
LVL 21

Expert Comment

by:yo_bee
ID: 41845093
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
 
LVL 19

Author Comment

by:compdigit44
ID: 41845236
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
 
LVL 21

Expert Comment

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

Open in new window

0
 
LVL 19

Author Comment

by:compdigit44
ID: 41845260
Thanks!!!! for my own understanding, why do I need to do a foreach loop
0
 
LVL 21

Expert Comment

by:yo_bee
ID: 41845263
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
 
LVL 21

Expert Comment

by:yo_bee
ID: 41845264
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
 
LVL 39

Expert Comment

by:footech
ID: 41845291
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
 
LVL 21

Expert Comment

by:yo_bee
ID: 41845294
Thanks for the catch.
0
 
LVL 19

Author Comment

by:compdigit44
ID: 41845669
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
 
LVL 21

Expert Comment

by:yo_bee
ID: 41845673
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
 
LVL 19

Author Comment

by:compdigit44
ID: 41845675
Same error ...:-(
0
 
LVL 21

Expert Comment

by:yo_bee
ID: 41845686
can you post your Txt File?
0
 
LVL 19

Author Comment

by:compdigit44
ID: 41845692
Here you go..
devies.txt
0
 
LVL 21

Expert Comment

by:yo_bee
ID: 41845695
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 21

Accepted Solution

by:
yo_bee earned 500 total points
ID: 41845699
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
 
LVL 19

Author Comment

by:compdigit44
ID: 41845712
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
 
LVL 21

Expert Comment

by:yo_bee
ID: 41845718
Do you have the AD powershell installed on the computer you are running it from?
0
 
LVL 21

Expert Comment

by:yo_bee
ID: 41845719
Please post your script as well.
0
 
LVL 19

Author Comment

by:compdigit44
ID: 41845727
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
 
LVL 21

Expert Comment

by:yo_bee
ID: 41845730
Change $workstation to $DN
0
 
LVL 21

Expert Comment

by:yo_bee
ID: 41845731
On the the line with ADD-Adgroupmember
0
 
LVL 21

Expert Comment

by:yo_bee
ID: 41845733
The $grp and $dn is getting the DistinguishedName attribute parsed for the Add-Groupmember cmdlet part so you do not get those errors
0
 
LVL 19

Author Comment

by:compdigit44
ID: 41845746
Getting same exact message as I posted before and this is after making the changes you recommended
0
 
LVL 19

Author Comment

by:compdigit44
ID: 41845753
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
 
LVL 21

Expert Comment

by:yo_bee
ID: 41845768
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
 
LVL 19

Author Comment

by:compdigit44
ID: 41845775
I ran the script as well. I just though whatif would like on screen all devices that it would be adding... :o)
0
 
LVL 21

Expert Comment

by:yo_bee
ID: 41845779
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
 
LVL 19

Author Comment

by:compdigit44
ID: 41845794
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
 
LVL 21

Expert Comment

by:yo_bee
ID: 41845795
Glad to help.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now