-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCreate-Groups.ps1
38 lines (30 loc) · 1.13 KB
/
Create-Groups.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
$CSVPath = ".\group-creation.csv"
$NameHeader = "Name"
$PathHeader = "Path"
$GroupCategoryHeader = "GroupCategory"
$GroupScopeHeader = "GroupScope"
function Test-Group ($GroupName) {
$ErrorActionPreference = "silentlycontinue"
$Group = Get-ADGroup -Identity $GroupName
if ($Group -eq $null) {
return $false
}
else {
return $true
}
}
# iterate through each entry of the csv
Import-Csv $CSVPath | ForEach-Object {
# check if group already exists and skips that loop if it does
if (Test-Group $_.$NameHeader) {
Write-Host "$($_.$NameHeader) exists, skipping."
return
}
# prompt action and create group
Write-Host "Creating $($_.$NameHeader) at $($_.$PathHeader), category $($_.$GroupCategoryHeader), scope $($_.$GroupScopeHeader)"
New-ADGroup -Name $($_.$NameHeader) -Path $($_.$PathHeader) -GroupCategory $($_.$GroupCategoryHeader) -GroupScope $($_.$GroupScopeHeader)
# check if existant, prompt if not
if (! (Test-Group $_.$NameHeader)) {
Write-Host -ForegroundColor Red -BackgroundColor Black "ERROR : Group $($_.$NameHeader) was not created"
}
}