• Status: Solved
  • Priority: Medium
  • Security: Private
  • Views: 39
  • Last Modified:

Add error checking to New-ADGroup

Hi EE

Can someone please help me ..
1) add error checking on this .. if the group already exist , for it to show in the console any groups that already existed.
2) also .. the group properties have an e-Mail field .. what would I add to the CSV and also the script so it adds that when the group is created?

Import-Module ActiveDirectory
$groups = Import-Csv 'C:\test\CreateGroups.csv'
foreach ($group in $groups) {
New-ADGroup -Name $group.name -Path “MYOU” -GroupCategory Security -GroupScope Global -Description $group.description}

Open in new window

0
MilesLogan
Asked:
MilesLogan
  • 2
  • 2
1 Solution
 
oBdACommented:
Add a column "Email" to the csv and set it to the email address to use for the group:
Import-Module ActiveDirectory
$groups = Import-Csv 'C:\test\CreateGroups.csv'
ForEach ($group in $groups) {
	If (Get-ADGroup -Identity $group.Name) {
		Write-Host "Group '$($group.Name)' exists already." -ForegroundColor Yellow
	} Else {
		New-ADGroup -Name $group.Name -Path "MYOU" -GroupCategory Security -GroupScope Global -Description $group.Description -OtherAttributes @{'mail' = $group.Email}
	}
}

Open in new window

0
 
MilesLoganAuthor Commented:
Hi oBdA

The email update does work ! thank you .

but the check if the group exist not really .. so its giving the normal error below .. because the groups do no exist because they have not been created .. can we suppress that ? if it does not exist ..it will create it and if it does exist it will output on the screen as its currently doing .

" Get-ADGroup : Cannot find an object with identity: ....  "
0
 
oBdACommented:
Sorry, that should have been a filter:
Import-Module ActiveDirectory
$groups = Import-Csv 'C:\test\CreateGroups.csv'
ForEach ($group in $groups) {
	If (Get-ADGroup  -Filter "Name -eq '$($group.Name)'") {
		Write-Host "Group '$($group.Name)' exists already." -ForegroundColor Yellow
	} Else {
		New-ADGroup -Name $group.Name -Path "MYOU" -GroupCategory Security -GroupScope Global -Description $group.Description -OtherAttributes @{'mail' = $group.Email}
	}
}
New-ADGroup -GroupCategory Security -Name "FooGroup" -OtherAttributes @{'mail' = 'foogroup@acme.com'} -GroupScope Global

Open in new window

0
 
MilesLoganAuthor Commented:
Hi oBdA .. that was it .. thank you much .. appreciate the help as always .
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now