-
Notifications
You must be signed in to change notification settings - Fork 0
/
setupnewuser.ps1
394 lines (328 loc) · 13.6 KB
/
setupnewuser.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# This script will set up AD, User Directories, Exchange and Lync, using basic data retrived from a CSV file
# Requires the Active Directory module for Windows Powershell and appropriate credentials
# CSV file with corresponding header and user(s) info:
# Office,UserName,FirstName,LastName,Initial,Department,Role,Title,SetupSameAs,Manager,MailboxAccess,Extension,Mobile
#LOAD POWERSHELL SESSIONS
#------------------------
$exchangeserver = "exchange1.domain.com"
$Lyncserver = "lync1.domain.com.au"
$DC = "dc1.domain.com.au"
$AdminEmail = "[email protected]"
$ScriptUser = $env:username
$usercredential= get-credential -credential domain\AdminUser
cls
write-host -foregroundcolor Green "Loading modules for AD, Exchange and Lync..."
$exchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$exchangeserver/PowerShell/ -Authentication Kerberos -Credential $UserCredential
Import-PsSession $exchangesession
$lyncsession = new-pssession -connectionuri https://$Lyncserver/ocspowershell -credential $usercredential
Import-PSSession $lyncsession
import-module ActiveDirectory
#VARIABLES
#---------
#Get Script User's Name
$ScriptUserDetails = Get-ADUser $ScriptUser -Server $DC -properties givenName | select-object givenName
$ScriptUserName = $ScriptUserDetails.givenName
#Email Communications
$EmailCC1 = "[email protected]"
$EmailCC2 = "[email protected]"
#LOAD USER DATA FROM CSV
#-----------------------
$InputPath = "\\domain.com.au\Scripts\Data\"
$InputFile = $InputPath + "NewUserInfo.csv"
Invoke-Item $inputFile
cls
write-host -foregroundcolor Green "Update user details and save CSV file. Press any key to continue"
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
#DEFINE VARIABLES
#----------------
$inputFile = Import-CSV $inputFile
foreach($line in $inputFile)
{
#Retrieve user details from CSV file
$office = $line.Office
$username = $line.UserName
$firstname = $line.FirstName
$lastname = $line.LastName
$initial = $line.Initial
$department = $line.Department
$Role = $line.Role
$title = $line.Title
$SetupSameAs = $line.SetupSameAs
$manager = $line.Manager
$MailboxAccess = $line.MailboxAccess
$Extension = $line.Extension
$Extension = $Extension.substring($Extension.length -3, 3)
$Mobile = $line.Mobile
$info = ""
cls
#Prompt for any missing details
if ($username -eq "") {$username = read-host -prompt "Please Enter Unique Username"}
if ($office -eq "") {$office = read-host -prompt "Please Enter Office for"}
if ($initial -eq "") {$initial = read-host -prompt "Please Enter Unique User Initials"}
if ($department -eq "") {$department = read-host -prompt "Please Enter Department"}
if ($Role -eq "") {$Role = read-host -prompt "Please Enter Role"}
if ($title -eq "") {$title = read-host -prompt "Please Enter Title"}
if ($SetupSameAs -eq "") {$SetupSameAs = read-host -prompt "Please Enter SetupSameAs"}
if ($manager -eq "") {$manager = read-host -prompt "Please Enter thier Manager's Username"}
if ($Extension -eq "") {$Extension = read-host -prompt "Please Enter 3 Digit Phone Extension"}
#if ($Mobile -eq "") {$Mobile = read-host -prompt "Please Enter Mobile in (+61) 405 123 123 format"}
#Get 'SetupSameAs' Details
$SameAsDetails = Get-ADUser $SetupSameAs -Server $DC -properties Office,DisplayName,sAMAccountName | select-object Office,DisplayName,sAMAccountName
$SameAsOffice = $SameAsDetails.Office
$SameAsDisplayName = $SameAsDetails.DisplayName
#Get Script User's Details
$ScriptUserDetails = Get-ADUser $ScriptUser -Server $DC -properties mail | select-object mail
$ScriptUserMail = $ScriptUserDetails.mail
#Define Fixed Variables
$umpolicy="UMDial Default Policy"
$userou="OU=USERS,DC=domain,DC=com,DC=au"
$companyname="My Company"
$mailboxdatabase="MailDB1"
$sipdomain="domain.com.au"
$Country = "Australia"
$DisplayName = $firstname +" " +$lastname
$SharepointPage = "http://intranet/my/Person.aspx?accountname=domain\" +$username
$HomePath = "\\fileserver\home$\" +$username
$LogFile = $InputPath+"SetupLog_" +$username +".txt"
#Define Generated and Localised Variables
$Name=$Firstname+" "+$Lastname
$accountpassword = read-host -assecurestring -prompt "Please enter temporary password for new user"
$upn = $username+ "@domain.com.au"
$email = $Firstname+"."+$Lastname +"@domain.com.au"
if ($office -eq "City1")
{
$EnableVoice = $True
$Lyncserver="lync1.domain.com.au"
$Telephone = "(+61) 3 1234 5" +$Extension
$Street = "123 My Street"
$City = "City"
$State = "State"
$PostCode = "3000"
$ProfilePath = "\\fileserver\profiles\" +$username
$Fax = "(+61) 3 1234 5678 "
$IPphone = "3" +$Extension
$teluri = "tel:+612345" +$Extension +";ext=" +$IPphone
$info = "random text about user"
$Operator = "1000"
}
elseif ($office -eq "City2")
{
$EnableVoice = $True
$Lyncserver="lync2.domain.com.au"
$Telephone = "(+61) 3 1234 5" +$Extension
$Street = "123 My Street"
$City = "City"
$State = "State"
$PostCode = "3000"
$ProfilePath = "\\fileserver\profiles\" +$username
$Fax = "(+61) 3 1234 5678 "
$IPphone = "3" +$Extension
$teluri = "tel:+612345" +$Extension +";ext=" +$IPphone
$info = "random text about user"
$Operator = "1000"
}
elseif ($office -eq "City3")
{
$EnableVoice = $True
$Lyncserver="lync3.domain.com.au"
$Telephone = "(+61) 3 1234 5" +$Extension
$Street = "123 My Street"
$City = "City"
$State = "State"
$PostCode = "3000"
$ProfilePath = "\\fileserver\profiles\" +$username
$Fax = "(+61) 3 1234 5678 "
$IPphone = "3" +$Extension
$teluri = "tel:+612345" +$Extension +";ext=" +$IPphone
$info = "random text about user"
$Operator = "1000"
}
elseif ($office -eq "City4")
{
$EnableVoice = $True
$Lyncserver="lync4.domain.com.au"
$Telephone = "(+61) 3 1234 5" +$Extension
$Street = "123 My Street"
$City = "City"
$State = "State"
$PostCode = "3000"
$ProfilePath = "\\fileserver\profiles\" +$username
$Fax = "(+61) 3 1234 5678 "
$IPphone = "3" +$Extension
$teluri = "tel:+612345" +$Extension +";ext=" +$IPphone
$info = "random text about user"
$Operator = "1000"
}
else
{
write-host -foregroundcolor Red "Office not recognised. Quitting..."
exit
}
#Optional/Future
# $archivedatabase="Your Database holding online archives"
# $retentionpolicy="Your retention policy"
# $dialplan="Lync Voice Dial Plan"
# $voicepolicy="Lync Voice Policy"
# $locationpolicy="Lync Location Policy"
# $externalaccesspolicy="Lync External Access Policy"
Start-Transcript -path $LogFile -append
cls
write-host -foregroundcolor Green "New user setup starting for:" $firstname $lastname
write-host "`r"
#SETUP EXCHANGE
#--------------
#Create user and enable mailbox
New-Mailbox -DomainController $DC -name $name -userprincipalname $upn -Alias $username -OrganizationalUnit $userou -SamAccountName $username -FirstName $FirstName -Initials $initial -LastName $LastName -Password $accountpassword -ResetPasswordOnNextLogon $true -Database $mailboxdatabase | out-null
#OPTIONAL/FUTURE: -Archive -ArchiveDatabase $archivedatabase -RetentionPolicy $retentionpolicy
#pause for Exchange
write-host -foregroundcolor Green "New mailbox created - Pausing 10 seconds for Exchange changes"
write-host "`r"
Start-Sleep -s 10
#Update Notes info
Get-Mailbox $username -DomainController $DC | Set-User -DomainController $DC -notes $info
#Enable For Unified Messaging
if ($EnableVoice -eq $true)
{
Get-Mailbox $username -DomainController $DC | Enable-UMMailbox -DomainController $DC -ummailboxpolicy $umpolicy -sipresourceidentifier $email -extensions $IPphone
Start-Sleep -s 5
Get-Mailbox $username -DomainController $DC | Set-UMMailbox -OperatorNumber $Operator
write-host -foregroundcolor Green "Unified Messaging Properties updated - Pausing 10 Seconds for Exchange changes"
write-host "`r"
Start-Sleep -s 10
}
#Setup mailbox permissions
if (!($MailboxAccess -eq "")) {Add-MailboxPermission -Identity $manager -User $username -AccessRights 'FullAccess'}
#SETUP ACTIVE DIRECTORY
#-----------------------
#Update user properties
Set-ADUser -server $DC -Identity $username -City $City -Company $companyname -Country "AU" -Department $department -description $title -DisplayName $DisplayName -Division $department -Fax $Fax -HomePage $SharepointPage -Manager $manager -Office $office -OfficePhone $Telephone -postalCode $PostCode -profilePath $ProfilePath -State $State -StreetAddress $Street -Title $title -add @{ipphone=$IPphone}
#Update non-mandatory items
if (!($Mobile -eq "")) {Set-ADUser -server $DC -Identity $username -Replace @{mobile=$Mobile}}
write-host -foregroundcolor Green "Active Directory properties updated"
write-host "`r"
#Add to Groups (using SetupSameAs)
$ds = new-object directoryServices.directorySearcher
$ds.filter = "(&(objectCategory=person)(objectClass=user)(samAccountName="+$SetupSameAs+"))"
$dn = $ds.findOne()
$user = [ADSI]$dn.path
foreach ($group in $user.memberof)
{
Add-ADGroupMember -Identity $group -Members $username
#Trim group names
$GroupTrim = $group.indexof(",OU=")
if ($GroupTrim -gt 0)
{
$GroupName = $group.substring(0,$GroupTrim)
$GroupName = $GroupName -replace ("CN="," `n")
}
$RPTGroups = $RPTGroups+" "+$GroupName
}
#pause for AD changes
write-host -foregroundcolor Green "Active Directory groups and permissions update - Pausing 10 Seconds for all AD changes"
write-host "`r"
Start-Sleep -s 10
#SETUP USER DIRECTORIES
#----------------------
# User profile
$win7Profile = $ProfilePath +".V2"
IF (!(TEST-PATH $win7Profile)) {NEW-ITEM $win7Profile -type Directory}
#Build Access Control Entry List
$colRights = [System.Security.AccessControl.FileSystemRights]"FullControl"
$InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]"ContainerInherit, ObjectInherit"
$PropagationFlag = [System.Security.AccessControl.PropagationFlags]::None
$objType =[System.Security.AccessControl.AccessControlType]::Allow
$objUser = New-Object System.Security.Principal.NTAccount("MyDomain\"+$username)
$objACE = New-Object System.Security.AccessControl.FileSystemAccessRule($objUser, $colRights, $InheritanceFlag, $PropagationFlag, $objType)
#Apply Permissions
$objACL = Get-ACL $win7Profile
$objACL.AddAccessRule($objACE)
Set-ACL $win7Profile $objACL
#Apply Ownership
$objACL = Get-ACL $win7Profile
$objACL.SetOwner($objUser)
Set-Acl -aclobject $objACL -path $win7Profile -passthru
# User Home Drive
IF (!(TEST-PATH $HomePath)) {NEW-ITEM $HomePath -type Directory}
write-host -foregroundcolor Green "User Directories Created"
write-host "`r"
#SETUP LYNC
#----------
#enable for lync and configure settings
Get-mailbox $username -DomainController $DC | Enable-csuser -DomainController $DC -registrarpool $lyncserver -sipaddresstype EmailAddress -sipdomain $sipdomain
#pause for Lync changes
write-host -foregroundcolor Green "User setup for Lync - Pausing 10 Seconds for Lync Changes"
write-host "`r"
Start-Sleep -s 10
#Enable For Enterprise Voice
if ($EnableVoice -eq $true) {Get-mailbox $username -DomainController $DC | Set-CSUser -DomainController $DC -enterprisevoiceenabled $True -lineuri $teluri}
#OPTIONAL/FUTURE (if not <Default>):
#Get-mailbox $username | Grant-CSVoicePolicy -policyname $voicepolicy
#Get-mailbox $username | Grant-CSDialPlan -policyname $dialplan
#Get-mailbox $username | Grant-CSLocationPolicy -policyname $locationpolicy
#Get-mailbox $username | Grant-CSExternalAccessPolicy -policyname $externalaccesspolicy
#USER SUMMARY REPORT
#-------------------
cls
write-host -foregroundcolor Green "Applying final user settings and generating report..."
write-host "`r"
Start-Sleep -s 10
#Active Directory
write-host -foregroundcolor Green "Active Directory Details"
Get-ADUser $username -Server $DC
write-host "`r"
write-host "New user has been added to the following groups:" $RPTGroups
write-host "`r"
#Folders
write-host -foregroundcolor Green "User Directories"
write-host "`r"
write-host "Profile Path is " $win7Profile
write-host "Home Path is " $HomePath
write-host "`r"
#Exchange
write-host -foregroundcolor Green "Exchange Details"
Get-Mailbox $username -DomainController $DC
if ($EnableVoice -eq $true)
{
write-host -foregroundcolor Green "Unified Messaging Details"
get-ummailbox $username -DomainController $DC
}
#Lync
write-host -foregroundcolor Green "Lync Details"
Get-CSUser $username -DomainController $DC
#FINISH
#------
Stop-Transcript
#Email IT report
$MailSubject = "[AUTO] New account setup for " +$firstname+" "+$lastname
$MailBody = "A new user account has been setup for " +$firstname+" "+$lastname+", by "+$ScriptUserName+" on "+(get-date)+". `r
"+$firstname+" was setup with the following information:
Office: "+$office+"
Username: "+$username+"
First Name: "+$firstname+"
Last Name: "+$lastname+"
Initials: "+$initial+"
Departsment: "+$department+"
Role: "+$Role+"
Title: "+$title+"
Setup Same As: "+$SetupSameAs+"
Manager: "+$manager+"
Extension: "+$Extension+"
Mobile: "+$Mobile+"
`r
Please confirm these groups in AD (they have been copied from "+$SameAsDisplayName+", but may need to be changed) `n
"+$RPTGroups+"
`r
IT to complete the following items:
- Manual Steps 1 `r
- Manual Steps 2 `r
- Manual Steps 3 `r
`r
Regards
Admin Scripts"
Send-MailMessage -To $ScriptUserMail -cc $AdminEmail,$EmailCC1,$EmailCC2 -From "[email protected]" -Subject $MailSubject -SmtpServer $exchangeserver -body $MailBody -attachment $LogFile
#Pause for review, then load next line
write-host "`r"
write-host -foregroundcolor Green "User setup finished. Press any key to continue"
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
exit