-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetManager.ps1
70 lines (64 loc) · 2.47 KB
/
SetManager.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
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string[]]
$InputFiles,
[Parameter(Mandatory = $false)]
[string]
$Server = "dcc-h-dc01.ad.cannabis.ca.gov",
[Parameter(Mandatory = $false)]
[pscredential]
$Credential = (Get-Credential)
)
BEGIN {
$Exports = $InputFiles | ForEach-Object { Import-Csv $_ }
$Exports_Name_Hash = $Exports | Group-Object -Property Name -AsHashTable
function CheckManager([string]$manager){
$manager_upn = $Exports_Name_Hash[$manager].UserPrincipalName
if(-not $manager_upn){
Write-Host "Manager $manager not found." -ForegroundColor Red
return $false
}
$manager_upn = ($manager_upn -split '@')[0] + "@cannabis.ca.gov"
$Manager_ADUser = Get-ADUser -Filter "UserPrincipalName -eq '$manager_upn'" -Server $Server
if (-not $Manager_ADUser) {
Write-Host "Manager $manager_upn not found." -ForegroundColor Red
return $false
}
if ( ($Manager_ADUser | Measure-Object | Select-Object -ExpandProperty Count) -ne 1 ) {
Write-Host "Manager $manager_upn found multiple." -ForegroundColor Red
return $false
}
return $Manager_ADUser.DistinguishedName
}
}
PROCESS {
foreach ($User in $Exports) {
$UPN = ($User.UserPrincipalName -split '@')[0] + '@cannabis.ca.gov'
$Manager = $User.Manager
if ([string]::IsNullOrWhiteSpace($Manager)) {
# Write-Host "User $UPN has no manager defined." -ForegroundColor Yellow
Continue
}
$ADUser = Get-ADUser -Filter "UserPrincipalName -eq '$UPN'" -Server $Server -Properties Manager
if (-not $ADUser) {
Write-Host "User $UPN not found." -ForegroundColor Red
Continue
}
if ( ($ADUser | Measure-Object | Select-Object -ExpandProperty Count) -ne 1 ) {
Write-Host "User $UPN found multiple." -ForegroundColor Red
Continue
}
if ($ADUser.Manager){
Write-Host "User $UPN already has manager set." -ForegroundColor Cyan
continue
}
$manager_dn = CheckManager -manager $Manager
if (-not $manager_dn){
Write-Host "`tUser $UPN has no manager $($ADUser.Manager)"
continue
}
$ADUser | Set-ADUser -Server $Server -Credential $Credential -Manager $manager_dn
Write-Host "Set manager for user $UPN." -ForegroundColor Green
}
}