-
Notifications
You must be signed in to change notification settings - Fork 1
/
24.Create-Groups.ps1
114 lines (92 loc) · 3.31 KB
/
24.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<#
.Synopsis
Creates Groups in the OU structure
.DESCRIPTION
Reads the AD Configuration XML (Default: ADStructure.xml) and creates all groups within it.
.EXAMPLE
.NOTES
Author : Ben van Zanten
Company: Valid
Date : Dec 2015
Version: 1.0
History: 1.0 Initial version
#>
[CmdletBinding(SupportsShouldProcess=$true,
ConfirmImpact='Medium')]
Param
(
# Name of the input file, default is: ADStructure.xml
[Parameter(Mandatory=$false,Position=1,
ValueFromPipeline=$false,
ValueFromPipelineByPropertyName=$false,
ValueFromRemainingArguments=$false)]
[ValidateScript({Test-Path $_})]
[string]$XmlFile='.\ADStructure.xml',
# Name of the domain. For instance Contoso. If not given, the domain from the XML is used
[Parameter(Mandatory=$False,Position=2)]
[string]$DomainName,
# If set, will change a group (Set-ADGroup) if the group already exists. Otherwise the script will only create new groups and skip existing groups.
[switch]$ChangeExisting
)
Function New-GroupsFromXML ($Element) {
[string]$Path = ConvertFrom-ADXmlToDN $Element
[string]$name=$Element.name
ForEach ($Group in $Element.Group) {
$GroupHT = Convert-XmlToHT $Group
# Add the server name so we can also do remote domains
$GroupHT.Add("Server",$DomainFQDN)
if (Get-ADGroup -Filter "Name -eq '$($Group.name)'" -SearchBase "$($domXML.distinguishedName)" -SearchScope Subtree -Server $DomainFQDN ) {
#
# Existing Group... update the properties.
#
Write-Output "Group already exists ""$($Group.name)"" -Path ""$Path"""
$GroupHT.Remove("name")
$GroupHT["Identity"]=$Group.name
# Remove some extra fields that come from XML and are irrelevant for the Set-* CmdLet
$GroupHT.Remove("#comment")
$GroupHT.Remove("type")
$GroupHT.Remove("OtherAttributes")
# $GroupHT | FT Key,Value
if ($ChangeExisting) {
Set-ADGroup @GroupHT
}
} else {
#
# New Group, create it.
#
Write-Output "New-ADGroup -Name ""$($Group.name)"" -Path ""$Path"""
$GroupHT["Path"]=$Path
# Fill in some final properties if not yet given.
# Remove some extra fields
$GroupHT.Remove("#comment")
# $GroupHT | FT Key,Value
New-ADGroup @GroupHT
}
}
# Use recursion to get all sub-OUs
ForEach ($OU in $Element.OU) {
New-GroupsFromXML $OU
}
# Use recursion to get all sub-Containers
ForEach ($OU in $Element.CN) {
New-GroupsFromXML $OU
}
}
Import-Module .\DeployAdLib.psd1
Import-Module ActiveDirectory
# Test for elevation :
if (-not(Test-AdminStatus)) {
Write-Error "Run this script elevated! This script requires administrative permissions."
break
}
$domName = Get-DomainName -XmlFile $XmlFile -DomainName $DomainName
[xml]$forXML = Get-Content $XmlFile
$domXML = $forXML.forest.domains.domain | ? { $_.name -eq $domName }
$DomainFQDN = $domxml.dnsname
#
# Here starts the real work...
#
$domXML.OUs.OU | ForEach-Object {
$OU = $_
New-GroupsFromXML $OU
}