Solved

Use Powershell to Create Groups and then add description

Posted on 2009-05-19
14
2,111 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
Comment Utility

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
Comment Utility
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
Comment Utility

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

Chris
0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

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
Comment Utility
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
Comment Utility

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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
Comment Utility

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
Comment Utility
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
Comment Utility

Run this one on its own:

Import-CSV "FileAccessGroupList.csv"

What do you get back?

Chris
0
 

Author Comment

by:brianroma
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility

You're most welcome :)

Chris
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
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.

762 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

11 Experts available now in Live!

Get 1:1 Help Now