-
Notifications
You must be signed in to change notification settings - Fork 0
/
ManageUsers.ps1
163 lines (117 loc) · 5.45 KB
/
ManageUsers.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
Param ([string] $filename="")
function check([string]$Check)
{
$GG="That user already exists"
$WP="That user does not exist .... creating User"
Get-ADUser $Check -ErrorAction SilentlyContinue -ErrorVariable $errorUser
if ($errorUser.count -eq 1)
{
return $GG
}
else{
$WP
}
}
$bool = Test-Path $filename
$GroupExists="That Group already exists"
<#if ($bool -eq $false)
{
Write-Host "That file does not exist"
#>
$x = [xml](Get-Content $filename)
##Adds Groups and checks whether or not they exist
foreach($i in $x.root.userManagement.user)
{
$GroupArray=@($i.memberof.group)
$CheckGroup = Get-ADGroup $GroupArray[0] -ErrorAction SilentlyContinue
$CheckGroup2 = Get-ADGroup $GroupArray[1] -ErrorAction SilentlyContinue
if($CheckGroup -eq $null)
{
if($GroupArray[0] -ne $null)
{
New-ADGroup -GroupScope Global -Name $GroupArray[0]
Write-Host "Added new group" $GroupArray[0] -ForegroundColor Yellow
New-ADGroup -GroupScope Global -Name $GroupArray[1]
Write-Host "Added new group" $GroupArray[1] -ForegroundColor Yellow
}
}
}
$DC=Get-ADDomain | select -ExpandProperty DistinguishedName
##Checks Organizational Unit and adds if doesnt exists
$CheckOU=(Get-ADOrganizationalUnit -filter {(Name -eq "$i.ou")})
if($CheckOU -ne $null)
{
Write-Host "That OU already exists"
}
else
{
New-ADOrganizationalUnit -Name $i.ou -Path "$DC" -ProtectedFromAccidentalDeletion 0
}
#Adds Users
foreach($i in $x.root.userManagement.user)
{
$tempou=$i.ou
if($i.manager -eq '')
{
New-ADUser -Name $i.account -GivenName $i.firstname -Surname $i.lastname -Description $i.description -Path "OU=$tempou, $DC" -AccountPassword(ConvertTo-SecureString -AsPlainText $i.password -force) -Enabled $true
Write-Host "Added new user"$i.account -ForegroundColor Green
}
else{
New-ADUser -Name $i.account -GivenName $i.firstname -Surname $i.lastname -Description $i.description -Path "OU=$tempou, $DC" -AccountPassword(ConvertTo-SecureString -AsPlainText $i.password -force) -Enabled $true -Manager $Manager
Write-Host "Added new user"$i.account -ForegroundColor Green
}
}
##Adds users to groups
foreach($i in $x.root.userManagement.user)
{
$GArray=@($i.memberof.group)
if($GArray[0] -ne $null)
{
Add-ADGroupMember -Identity $GArray[0] -Member $i.account
}
elseif($GArray[1] -ne $null)
{
Add-ADGroupMember -Identity $GArray[1] -Member $i.account
}
}
#Makes local groups and adds the users to groups
foreach($i in $x.root.localGroupManagement.localGroup)
{
New-ADGroup -GroupScope Global -Name $i.name
Add-ADGroupMember -Identity $i.name -Member $i.members.group
}
<#
foreach($i in $x.root.userManagement.user)
{
$tempaccount = $i.account
$tempfirstname = $i.firstname
$templastname = $i.lastname
$tempdescription = $i.description
$temppassword = $i.password
$tempmanager = $i.manager
$tempou = $i.ou
check $tempaccount
if ($tempmanager -eq '')
{
New-ADUser -Name $tempaccount -GivenName $tempfirstname -Surname $templastname -Description $tempdescription -Path "OU=$temp.ou, DC=esage, DC=us" -AccountPassword(ConvertTo-SecureString -AsPlainText $temppassword -force) -Enabled $true
}
elseif($tempmanager -ne '')
{
New-ADUser -Name $tempaccount -GivenName $tempfirstname -Surname $templastname -Description $tempdescription -Path "OU=$temp.ou, DC=esage, DC=us" -AccountPassword(ConvertTo-SecureString -AsPlainText $temppassword -force) -Enabled $true -Manager $tempmanager
}
}
##Adds users to groups
foreach($i in $x.root.userManagement.user)
{
$GArray=@($i.memberof.group)
if($GArray[0] -ne $null)
{
Add-ADGroupMember -Identity $GArray[0] -Member $i.account
}
elseif($GArray[1] -ne $null)
{
Add-ADGroupMember -Identity $GArray[1] -Member $i.account
}
}
#>