Avatar of compdigit44
compdigit44
 asked on

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.
Active DirectoryPowershell

Avatar of undefined
Last Comment
yo_bee

8/22/2022 - Mon
yo_bee

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

compdigit44

ASKER
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?
yo_bee

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

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
compdigit44

ASKER
Thanks!!!! for my own understanding, why do I need to do a foreach loop
yo_bee

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?
yo_bee

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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
footech

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.
yo_bee

Thanks for the catch.
compdigit44

ASKER
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

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
yo_bee

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

compdigit44

ASKER
Same error ...:-(
yo_bee

can you post your Txt File?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
compdigit44

ASKER
Here you go..
devies.txt
yo_bee

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
ASKER CERTIFIED SOLUTION
yo_bee

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
compdigit44

ASKER
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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
yo_bee

Do you have the AD powershell installed on the computer you are running it from?
yo_bee

Please post your script as well.
compdigit44

ASKER
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

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
yo_bee

Change $workstation to $DN
yo_bee

On the the line with ADD-Adgroupmember
yo_bee

The $grp and $dn is getting the DistinguishedName attribute parsed for the Add-Groupmember cmdlet part so you do not get those errors
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
compdigit44

ASKER
Getting same exact message as I posted before and this is after making the changes you recommended
compdigit44

ASKER
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
yo_bee

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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
compdigit44

ASKER
I ran the script as well. I just though whatif would like on screen all devices that it would be adding... :o)
yo_bee

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
compdigit44

ASKER
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..
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
yo_bee

Glad to help.