Solved

Use Powershell to Create Groups and then add description

Posted on 2009-05-19
14
2,122 Views
Last Modified: 2012-06-27
Hey guys!!

I have PowerShell script to create groups and put them in different OUs (this one is for domain local groups):

$OU = "OU=File Access Groups,OU=Domain Groups,DC=MYDOMAIN,DC=com"
$GroupType = "Security"
$GroupScope = "DomainLocal"

Get-Content "FileAccessGroupList.txt" | %{ If ($_ -ne "") { `
  New-QADGroup -Name $_ -ParentContainer $OU -GroupType $GroupType -GroupScope $GroupScope } }


I was wondering how I could append this script to fill in the Description while the groups are being created?


I have found user management scripts (like below) but they require special formatting of a separate csv:


http://myitforum.com/cs2/blogs/yli628/archive/2007/09/11/one-step-further-powershell-script-to-modify-multiple-users-property-in-active-directory.aspx



In my previous script, I showed how we can add or modify the user propertites in Active Directory for single user. Now as a by request script, I will go one step further and try to do the samething for multiple users. The trick is you need to have a csv file ready and the import-csv cmdlet.

I have a sample users.csv file and it looks like this:

DN                                                                                       Telephonenumber       
CN=UserA,OU=X,OU=Y,OU=Z,DC=what,DC=ever,DC=com      xxx-yyy-zzz       
CN=UserB,OU=A,OU=B,OU=C,DC=what,DC=ever,DC=com     aaa-bbb-ccc       
CN=UserC,OU=L,OU=M,OU=N,DC=what,DC=ever,DC=com     lll-mmm-nnn       

 

Once you have the csv file ready  you could run the below script against it (You need to have the approriate right to your domain!)

$users = import-csv users.csv
foreach($row in $users)
{
$dn = $row.dn
$user=[ADSI]"LDAP://$dn"
$tel = $row.telephonenumber
$user.put("telephoneNumber", $tel)
$user.SetInfo()
}


Thanks for the help!!

0
Comment
Question by:brianroma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
14 Comments
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24423521

Sure, it can be modified. Is the description in FileAccessGroupList.txt? If so, how is that formatted? Tab Delimited would work pretty well. It isn't as neat as CSV formatting, but it'll work well enough.

Chris
0
 

Author Comment

by:brianroma
ID: 24425155
FileAccessGroupList.txt looks like this inside it now:

InfoTech-M-FileDL
InfoTech-RE-FileDL
Infra-M-FileDL
Infra-RE-FileDL


I would prefer to put description in the same text if it is possible. I am open to tab or csv...

Thanks for the help, Chris!
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24426750

That's fine, either will work well with the above. Which do you prefer?

Chris
0
How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

 
LVL 71

Expert Comment

by:Chris Dent
ID: 24429807

Some samples so you can choose :)

If the format is:

GroupName,Description
InfoTech-M-FileDL,M File DL
InfoTech-RE-FileDL,RE File DL

Then we could do:

Import-CSV "FileAccessGroupList.csv" | %{
  New-QADGroup -Name $_.GroupName -ParentContainer $OU `
    -GroupType $GroupType -GroupScope $GroupScope -Description $_.Description
}

Or if the format is tab delimited:

InfoTech-M-FileDL    M File DL
InfoTech-RE-FileDL    RE File DL

Then it becomes:

Get-Content "FileAccessGroupList.txt" | %{ If ($_ -ne "") {
  $GroupInfo = $_.Split("`t")
  New-QADGroup -Name $GroupInfo[0] -ParentContainer $OU `
    -GroupType $GroupType -GroupScope $GroupScope -Description $GroupInfo[1]
}}

The first (import CSV) is more flexible as you don't have to worry about the order of your fields, they're referenced by field name. The second relies on a static format, you must know the position (starting from 0) of each of the fields.

Chris
0
 

Author Comment

by:brianroma
ID: 24431592
Ok so I tried:

Get-Content "FileAccessGroupList.csv" | %{ If ($_ -ne "") { `
  New-QADGroup -Name $_ -ParentContainer $OU -GroupType $GroupType -GroupScope $GroupScope } }

Import-CSV "FileAccessGroupList.csv" | %{
  New-QADGroup -Name $_.GroupName -ParentContainer $OU `
    -GroupType $GroupType -GroupScope $GroupScope -Description $_.Description
}


With this CSV:

InfoTech-M-FileDL,Created 20 May 2009
InfoTech-RE-FileDL,RE File DL
Infra-M-FileDL
Infra-RE-FileDL

I get this error in PowerGUI script editor:


A constraint violation occurred. (Exception from HRESULT: 0x8007202F)
At :line:7 char:14
+   New-QADGroup <<<<  -Name $_ -ParentContainer $OU -GroupType $GroupType -GroupScope $GroupScope } }


Thanks again Chris!


0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24431627

You need to have the headers, when you use Import-CSV it uses the first line as the field names.

So in your example it would have these field names:

InfoTech-M-FileDL,Created 20 May 2009

You can see what I mean if you run:

Import-CSV "FileAccessGroupList.csv"

You should see something like:

InfoTech-M-FileDL      Created 20 May 2009
----------------------      ---------------------------
InfoTech-RE-FileDL    RE File DL
Infra-M-FileDL
Infra-RE-FileDL

Instead if you have this:

GroupName,Description
InfoTech-M-FileDL,Created 20 May 2009
InfoTech-RE-FileDL,RE File DL
Infra-M-FileDL
Infra-RE-FileDL

You'll get this instead:

GroupName                Description
---------------                --------------
InfoTech-M-FileDL      Created 20 May 2009
InfoTech-RE-FileDL     RE File DL
Infra-M-FileDL
Infra-RE-FileDL

It won't like you if you try and set a blank Description either, so we'd probably have to do....

Import-CSV "FileAccessGroupList.csv" | %{
  If ($_.Description) {
    New-QADGroup -Name $_.GroupName -ParentContainer $OU `
      -GroupType $GroupType -GroupScope $GroupScope -Description $_.Description
  } Else {
    New-QADGroup -Name $_.GroupName -ParentContainer $OU `
      -GroupType $GroupType -GroupScope $GroupScope
  }
}

Chris
0
 

Author Comment

by:brianroma
ID: 24431652
Sorry-- code wasn't complete:


$OU = "OU=File Access Groups,OU=Domain Groups,DC=MYDOMAIN,DC=com"
$GroupType = "Security"
$GroupScope = "DomainLocal"

Import-CSV "FileAccessGroupList.csv" | %{ If ($_ -ne "") { `
  New-QADGroup -Name $_ -ParentContainer $OU -GroupType $GroupType -GroupScope $GroupScope } }

Import-CSV "FileAccessGroupList.csv" | %{
  New-QADGroup -Name $_.GroupName -ParentContainer $OU `
    -GroupType $GroupType -GroupScope $GroupScope -Description $_.Description
}
0
 
LVL 71

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 24431686

You'll want to drop this one:

Import-CSV "FileAccessGroupList.csv" | %{ If ($_ -ne "") { `
  New-QADGroup -Name $_ -ParentContainer $OU -GroupType $GroupType -GroupScope $GroupScope } }

You only need the one Import command here. It won't like what you're passing for the group name in this case and will cause an error.

Full sample script is below.

Chris
$OU = "OU=File Access Groups,OU=Domain Groups,DC=MYDOMAIN,DC=com"
$GroupType = "Security"
$GroupScope = "DomainLocal"
 
Import-CSV "FileAccessGroupList.csv" | %{
  If ($_.Description) {
    New-QADGroup -Name $_.GroupName -ParentContainer $OU `
      -GroupType $GroupType -GroupScope $GroupScope -Description $_.Description
  } Else {
    New-QADGroup -Name $_.GroupName -ParentContainer $OU `
      -GroupType $GroupType -GroupScope $GroupScope
  }
}

Open in new window

0
 

Author Comment

by:brianroma
ID: 24431759
Using this code, I now have the following error:

The argument cannot be null or empty.
At :line:11 char:22
+     New-QADGroup -Name <<<<  $_.GroupName -ParentContainer $OU `

0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24431796

Run this one on its own:

Import-CSV "FileAccessGroupList.csv"

What do you get back?

Chris
0
 

Author Comment

by:brianroma
ID: 24431855
Console in PowerGUI:


InfoTech-M-FileDL      Created 20 May 2009  
-----------------      -------------------- 
InfoTech-RE-FileDL     RE File DL           
Infra-M-FileDL                              
Infra-RE-FileDL                

Open in new window

0
 
LVL 71

Assisted Solution

by:Chris Dent
Chris Dent earned 500 total points
ID: 24431972

You haven't got the header line, you must have it. It's above, but it's easier to repost :)



You need to have the headers, when you use Import-CSV it uses the first line as the field names.

So in your example it would have these field names:

InfoTech-M-FileDL,Created 20 May 2009

You can see what I mean if you run:

Import-CSV "FileAccessGroupList.csv"

You should see something like:

InfoTech-M-FileDL      Created 20 May 2009
----------------------      ---------------------------
InfoTech-RE-FileDL    RE File DL
Infra-M-FileDL
Infra-RE-FileDL

Instead if you have this in the CSV:

GroupName,Description
InfoTech-M-FileDL,Created 20 May 2009
InfoTech-RE-FileDL,RE File DL
Infra-M-FileDL
Infra-RE-FileDL

You'll get this:

GroupName                Description
---------------                --------------
InfoTech-M-FileDL      Created 20 May 2009
InfoTech-RE-FileDL     RE File DL
Infra-M-FileDL
Infra-RE-FileDL

This one is what we need to be able to use the commands above, we refer to the CSV by the header lines.

Chris
0
 

Author Comment

by:brianroma
ID: 24433856
OK, it took a couple of repeats of information before it sunk in. I now understand the CSV header instructions.

Thanks for the help...

It works!!

B
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24434540

You're most welcome :)

Chris
0

Featured Post

What, When and Where - Security Threats from Q1

Join Corey Nachreiner, CTO, and Marc Laliberte, Information Security Threat Analyst, on July 26th as they explore their key findings from the first quarter of 2017.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

623 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