Solved

Problems Adding Devices from File Using Add-ADGroupMember

Posted on 2016-10-17
7
25 Views
Last Modified: 2016-10-19
I had question open for this same issue ( https://www.experts-exchange.com/questions/28976646/Error-Importing-Computer-Accounts-From-a-Text-File-using-Add-ADGroupMember.html)  before which Yo_Bee helped with with and did get it working. Thanks Yo_Bee!!!  

 When I tried to run the script again this morning. I am getting the following error..

Get-ADComputer : Cannot convert 'System.Object[]' to the type 'Microsoft.ActiveDirectory.Management.ADComputer'
required by parameter 'Identity'. Specified method is not supported.
At C:\AddingDevicesFromFileToGroup.ps1:9 char:33
+ $DN = (Get-ADComputer -Identity $devices).distinguishedname
+                                 ~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADComputer], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.GetADComputer

In my text file as soon as I narrow down my list to one device it works. It is almost like the foreach loop in not working

Import-Module ActiveDirectory

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

Foreach ($dn in $devices)

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

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

Open in new window

0
Comment
Question by:compdigit44
  • 3
  • 3
7 Comments
 
LVL 38

Expert Comment

by:Adam Brown
Comment Utility
The $DN = line is trying to pull the entire array in. Also, it's replacing the value of the $dn object you are creating in ($dn in $devices)

Try this out:
Import-Module ActiveDirectory

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

Foreach ($device in $devices)

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

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

Open in new window

0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
You got somewhat confused concerning the variables ...
Import-Module ActiveDirectory

$devices = Get-Content 'C:\workstations.txt'
$grp = (Get-ADGroup -Identity '8021xEnabledDevices').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
Comment Utility
Thank you both so very much... I must be tied and missing the but how does this different from the syntax I was using before?

I am trying to understanding this so I can become better in Powershell
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
Everything was fine up until and including line 6:
Foreach ($dn in $devices)
Then came line 9 with two errors:
$DN = (Get-ADComputer -Identity $devices).distinguishedname
1. You already used $dn as loop variable, and you're reassigning it here as $DN.
2. You used the $devices array (containing the contents of 'C:\workstations.txt') instead of the loop variable as Identity for Get-ADComputer.

Alternatively, you can fill an array with the members and add them in one go:
Import-Module ActiveDirectory

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

$Members = @()
Foreach ($device in $devices) {
	$Members += (Get-ADComputer -Identity $device).distinguishedname
}
Add-ADGroupMember -Identity $grp -Members $Members

Open in new window


Finally, since you can use samAccountName as member identity, and a computer's samAccountName is its host name with a trailing $, you can even do without the Get-ADComputer to speed things up:
Import-Module ActiveDirectory

$grp = (Get-ADGroup -Identity '8021xEnabledDevices').distinguishedname
$Members = Get-Content 'C:\workstations.txt' | ForEach-Object {$_ + "`$"}

Add-ADGroupMember -Identity $grp -Members $Members

Open in new window

0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
Wow great explaination and thank you very much.. I ran the script this morning using whatif first and worked perfect. Once I removed whatif I go the error message below

Get-ADComputer : Cannot bind parameter 'Identity' to the target. Exception setting "Identity": "Cannot validate
argument on parameter: 'Identity'. The argument is null or empty. Supply an argument that is not null or empty and
then try the command again."

The odd part if even though I got this error for each device on the list. It did end of adding them to the group?  Any thoughts on why I got the error?
0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
Could be empty lines in devices.txt; how as that file generated? If in doubt, check with Notepad++ and "View > Show Symbol >  Show End Of Line" enabled.
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
good point...

I did a copy and paste of text from Excel so it could have extra  return characters...
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
A procedure for exporting installed hotfix details of remote computers using powershell
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

743 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

16 Experts available now in Live!

Get 1:1 Help Now