Solved

Use Powershell to Create Groups and then add description

Posted on 2009-05-19
14
2,113 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
  • 8
  • 6
14 Comments
 
LVL 70

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 70

Expert Comment

by:Chris Dent
ID: 24426750

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

Chris
0
 
LVL 70

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 70

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 70

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 70

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 70

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 70

Expert Comment

by:Chris Dent
ID: 24434540

You're most welcome :)

Chris
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

896 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