Link to home
Start Free TrialLog in
Avatar of MilesLogan
MilesLoganFlag for United States of America

asked on

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

Avatar of oBdA
oBdA

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

Avatar of MilesLogan

ASKER

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: ....  "
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi oBdA .. that was it .. thank you much .. appreciate the help as always .