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

LVL 2
MilesLoganAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MilesLoganAuthor Commented:
Hi oBdA .. that was it .. thank you much .. appreciate the help as always .
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.