Solved

add list of users into Global Security Group

Posted on 2016-07-27
6
26 Views
Last Modified: 2016-07-27
Hi

I have a Global Security group that I need to import over 100 users and I tried this. Where am I going wrong with this?

import-module activedirectory

$list =import-csv "C:\test\export\userlist.csv"

forEach ($user in $list)

{

add-ADGroupMember -identity mytestgroup  -member $list

}
0
Comment
Question by:kuzum
  • 3
  • 2
6 Comments
 
LVL 82

Expert Comment

by:oBdA
ID: 41731230
Maybe stating the way-too-obvious here, but you're using "$List" as member to add, not "$User".
0
 
LVL 82

Expert Comment

by:oBdA
ID: 41731242
On closer inspection, you're using the import incorrectly as well.
Either your csv is a real csv with a header row, in which case you need to use the property (column) of the rows you imported, for example "-member $User.DistinguishedName", or you're using a flat file without header, in which case you need to use "Get-Content" instead of "Import-Csv".
0
 

Author Comment

by:kuzum
ID: 41731262
hmm, thanks. I changed it to get-content and error changed. I also attached the csv file? did it need to be .txt?
Add-ADGroupMember : Cannot find an object with identity: 'SamAccountName' under: 'DC=xx,DC=xx,DC=local'.
At line:9 char:18
+ add-ADGroupMember <<<<  -identity mytestGroup -member $user
    + CategoryInfo          : ObjectNotFound: (SamAccountName:ADPrincipal) [Add-ADGroupMember], ADIdentityNotFoundException
    + FullyQualifiedErrorId : SetADGroupMember.ValidateMembersParameter,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember
doc10.docx
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 82

Accepted Solution

by:
oBdA earned 500 total points
ID: 41731279
The attachment is not a csv (and, sorry, I'm not opening downloaded Office documents).
But if you didn't change the 'SamAccountName', then it seems to be a real csv, in which case you need to the property you want to use. Could be like this:
import-module activedirectory
$list =import-csv "C:\test\export\userlist.csv"
forEach ($user in $list) {
	add-ADGroupMember -identity mytestgroup  -member $user.SamAccountName
}

Open in new window

0
 
LVL 12

Expert Comment

by:Dustin Saunders
ID: 41731324
@kazum-

Yes, generally you'd want to either paste the contents of the CSV into a code block or attach a .txt or .csv file (office documents can execute macros when downloaded so most experts won't open them.)  You can also use a screenshot (png, jpg, etc)

To echo oBdA:

when you use this line:

forEach ($user in $list)

Open in new window

You're creating each $user as a row in the CSV.  So you use . and then the column name to reference the field.

For example, if your CSV is:
First,Last,FavoriteColor
John,Test,Blue

Open in new window


This code would do the following:
$list = import-csv "C:\test\export\userlist.csv"
foreach ($user in $list) {
$output = $user.First + " " + $user.Last + "'s favorite color is " + $user.FavoriteColor + "."
Write-Host $output
}

Open in new window

Would write "John Test's favorite color is Blue."
0
 

Author Closing Comment

by:kuzum
ID: 41732002
thank you for your excellent support gents
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In this article, we will see the basic design consideration while designing a Multi-tenant web application in a simple manner. Though, many frameworks are available in the market to develop a multi - tenant application, but do they provide data, cod…
This article will help you understand what HashTables are and how to use them in PowerShell.
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

708 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

17 Experts available now in Live!

Get 1:1 Help Now