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=C OM'
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.
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.
ASKER
My whole script is are the two lines below
What do I need to do a foreach loop? Can I run the script you listed using -whatif first?
$devices =get-content C:\workstationlist.txt
Add-ADGroupMember -Identity GlobalGroup1 -Member $devices
What do I need to do a foreach loop? Can I run the script you listed using -whatif first?
$devices =get-content C:\workstationlist.txt
Foreach ($device in $devices)
{Add-ADGroupMember -Identity GlobalGroup1 -Member $devices
}
ASKER
Thanks!!!! for my own understanding, why do I need to do a foreach loop
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?
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?
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.
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.
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.
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.
Thanks for the catch.
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..
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
}
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:
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
}
ASKER
Same error ...:-(
can you post your Txt File?
ASKER
Here you go..
devies.txt
devies.txt
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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:\AddingDevicesFromFileTo Group.ps1: 9 char:33
+ $DN = (Get-ADComputer -Identity $device).distinguishedname
+ ~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-ADComputer], ParameterBindingValidation Exception
+ FullyQualifiedErrorId : ParameterArgumentValidatio nError,Mic rosoft.Act iveDirecto ry.Managem ent.Comman ds.GetADCo m
uter
Add-ADGroupMember : Cannot find an object with identity: 'A43TR' under: 'DC=Domain,DC=Domain1,DC=C OM'.
At C:\AddingDevicesFromFileTo Group.ps1: 11 char:1
+ Add-ADGroupMember -Identity $grp -Members $workstation -whatif}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~
+ CategoryInfo : ObjectNotFound: (A43TR1:ADPrincipal) [Add-ADGroupMember], ADIdentityNotFoundExceptio n
+ FullyQualifiedErrorId : SetADGroupMember.ValidateM embersPara meter,Micr osoft.Acti veDirector y.Manageme nt.Command s
AddADGroupMember
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:\AddingDevicesFromFileTo
+ $DN = (Get-ADComputer -Identity $device).distinguishedname
+ ~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-ADComputer], ParameterBindingValidation
+ FullyQualifiedErrorId : ParameterArgumentValidatio
uter
Add-ADGroupMember : Cannot find an object with identity: 'A43TR' under: 'DC=Domain,DC=Domain1,DC=C
At C:\AddingDevicesFromFileTo
+ Add-ADGroupMember -Identity $grp -Members $workstation -whatif}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (A43TR1:ADPrincipal) [Add-ADGroupMember], ADIdentityNotFoundExceptio
+ FullyQualifiedErrorId : SetADGroupMember.ValidateM
AddADGroupMember
Do you have the AD powershell installed on the computer you are running it from?
Please post your script as well.
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}
Change $workstation to $DN
On the the line with ADD-Adgroupmember
The $grp and $dn is getting the DistinguishedName attribute parsed for the Add-Groupmember cmdlet part so you do not get those errors
ASKER
Getting same exact message as I posted before and this is after making the changes you recommended
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
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
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.
I ran my test script successfully so you should have no issues if you follow my script.
ASKER
I ran the script as well. I just though whatif would like on screen all devices that it would be adding... :o)
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
I would try this on a test group without the -whatif
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..
Thanks again..
Glad to help.
Here is an example that you want to follow.
Open in new window