-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAD User Update for IT.ps1
60 lines (50 loc) · 2.19 KB
/
AD User Update for IT.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
Import-Module ActiveDirectory
$Users = import-csv -path "C:\Users\PMarshall\Documents\My Received Files\TitleImport.csv"
Foreach($User in $Users)
{
$Identity = $User.'Legal First Name'.Substring(0,1) + $User.'Legal Last Name'
Write-Output "Beginning User : $Identity"
Write-Output "Manager = $($User.manager)"
$UserCheck = Get-ADUser $Identity -Properties *
If($UserCheck -eq $Null)
{
Write-Output "User $Identity not found in Active Directory."
Write-Output "---------------------------------"
Continue;
}
#Get the manager DN
$RegexResult = ([regex]::matches($($User.manager), "(.*)(?:,\s)(.)"))
$ManagerSearchName = $RegexResult.groups[2].value + $RegexResult.groups[1].value
$ManagerResult = (Get-ADUser $ManagerSearchName -Properties *).distinguishedname
Write-Output "Manager DN = $ManagerResult"
If($ManagerResult -notmatch $($RegexResult.groups[1].value))
{
Write-Output "Seems like the manager result might not have pulled correctly."
Write-Output "---------------------------------"
Continue;
}
Set-ADUser -Identity "$Identity" `
-Company $($User.'Company Working For') `
-Department $($User.Dept) `
-Title $($User.Title) `
-Manager $ManagerResult
$UpdateResults = Get-ADUser -Identity $Identity -Properties *
If($($UpdateResults.company) -ne $($UserCheck.company))
{
Write-Output "Company Updated! | Old Value : $($UserCheck.company) | New Value $($Updateresults.company)"
}
If($($UpdateResults.department) -ne $($UserCheck.department))
{
Write-Output "Department Updated! | Old Value : $($UserCheck.department) | New Value $($Updateresults.department)"
}
If($($UpdateResults.title) -ne $($UserCheck.title))
{
Write-Output "Title Updated! | Old Value : $($UserCheck.title) | New Value $($Updateresults.title)"
}
If($($UpdateResults.manager) -ne $($UserCheck.manager))
{
Write-Output "Manager Updated! | Old Value : $($UserCheck.manager) | New Value $($Updateresults.manager)"
}
Write-Output "SUCCESS"
Write-Output "---------------------------------"
}