-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenable.ps1
185 lines (162 loc) · 5.56 KB
/
enable.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
#region Initialize default properties
$config = ConvertFrom-Json $configuration
$p = $person | ConvertFrom-Json
$pp = $previousPerson | ConvertFrom-Json
$pd = $personDifferences | ConvertFrom-Json
$m = $manager | ConvertFrom-Json
$aRef = $accountReference | ConvertFrom-Json;
$success = $False
$auditLogs = [Collections.Generic.List[PSCustomObject]]@()
#endregion Initialize default properties
#region Support Functions
function Get-GoogleAccessToken() {
### exchange the refresh token for an access token
$requestUri = "https://www.googleapis.com/oauth2/v4/token"
$refreshTokenParams = @{
client_id=$config.clientId;
client_secret=$config.clientSecret;
redirect_uri=$config.redirectUri;
refresh_token=$config.refreshToken;
grant_type="refresh_token"; # Fixed value
};
$response = Invoke-RestMethod -Method Post -Uri $requestUri -Body $refreshTokenParams -Verbose:$false
$accessToken = $response.access_token
#Add the authorization header to the request
$authorization = [ordered]@{
Authorization = "Bearer $accesstoken";
'Content-Type' = "application/json; charset=utf-8";
Accept = "application/json";
}
$authorization
}
function Get-GoogleOuExists {
param (
[Parameter(Mandatory)]
[string]$orgUnitPath,
[bool]$createOuIfNotExists = $false,
[Parameter(Mandatory)]
$authorization
)
$googleOuExists = $false
$splat = @{
Uri = ("https://www.googleapis.com/admin/directory/v1/customer/my_customer/orgunits{0}" -f $orgUnitPath)
Method = 'GET'
Headers = $authorization
Verbose = $False
ErrorAction = 'Stop'
}
# API will error if target OU does not exist.
try {
$response = Invoke-RestMethod @splat
Write-Information ("Get-GoogleOuExists: Target OU {0} exists." -f $orgUnitPath)
$googleOuExists = $true
} catch {
if ($createOuIfNotExists -eq $true) {
# Create the target OU
try {
$leafOU = $orgUnitPath.split("/")[-1]
$parentOU = $orgUnitPath.replace("/$leafOu","")
$splat = @{
Uri = "https://www.googleapis.com/admin/directory/v1/customer/my_customer/orgunits"
Method = 'POST'
Headers = $authorization
Verbose = $true
ErrorAction = 'Stop'
Body = "{
'name':'$leafOU',
'parentOrgUnitPath': '$parentOU'
}"
}
$response = Invoke-RestMethod @splat
Write-Information ("Get-GoogleOuExists: Created organizational unit {0}." -f $orgUnitPath)
$googleOuExists = $true
} catch {
Write-Information ("Get-GoogleOuExists: Failed to create organizational unit {0}. Verify parent path exists." -f $orgUnitPath)
Write-Error $_
}
} else {
Write-Information ("Get-GoogleOuExists: Target OU {0} does not exist." -f $orgUnitPath)
}
}
return $googleOuExists
}
#endregion Support Functions
#region Change mapping here
$defaultOrgUnitPath = "/Employees"
#Target OrgUnitPath
$calcOrgUnitPath = ("{0}/{1}" -f
$defaultOrgUnitPath,
$p.PrimaryContract.Department.ExternalID
)
Write-Information ("Target OU: {0}" -f $calcOrgUnitPath)
#Change mapping here
$account = @{
suspended = $False
orgUnitPath = $calcOrgUnitPath
# includeInGlobalAddressList = $True
}
#endregion Change mapping here
#region Execute
try{
#Add the authorization header to the request
$authorization = Get-GoogleAccessToken
# Verify Target OU Exists. Else, use Default OU
$targetOuExists = Get-GoogleOuExists -orgUnitPath $calcOrgUnitPath -createOuIfNotExists $false -authorization $authorization
if ($targetOuExists -eq $true) {
$account.orgUnitPath = $calcOrgUnitPath
} else {
Write-Information ("Target OU Not found. Using Default OU: {0}" -f $defaultOrgUnitPath)
$account.orgUnitPath = $defaultOrgUnitPath
}
#Send User Update
if(-Not($dryRun -eq $True)) {
# Get Previous Account
$splat = @{
Uri = "https://www.googleapis.com/admin/directory/v1/users/$($aRef)"
Method = 'GET'
Headers = $authorization
Verbose = $False
}
$previousAccount = Invoke-RestMethod @splat
$splat = @{
body = [System.Text.Encoding]::UTF8.GetBytes(($account | ConvertTo-Json -Depth 10))
Uri = "https://www.googleapis.com/admin/directory/v1/users/$($aRef)"
Method = 'PUT'
Headers = $authorization
Verbose = $False
}
$updatedAccount = Invoke-RestMethod @splat
#Write-Information ("Response: {0}" -f ($response | ConvertTo-Json -Depth 50))
$auditLogs.Add([PSCustomObject]@{
Action = "EnableAccount"
Message = "Enabled/Updated account with PrimaryEmail $($updatedAccount.primaryEmail) in OrgUnit [$($updatedAccount.orgUnitPath)]"
IsError = $false;
});
}
else {
$updatedAccount = $account;
}
$success = $True
}catch{
$auditLogs.Add([PSCustomObject]@{
Action = "EnableAccount"
Message = "Error enabling/updating account with PrimaryEmail $($previousAccount.primaryEmail) - Error: $($_)"
IsError = $true;
});
Write-Error $_
Write-Error $_.ErrorDetails.Message
}
#endregion Execute
#region Build up result
$result = [PSCustomObject]@{
Success = $success
AccountReference = $aRef
AuditLogs = $auditLogs;
Account = $updatedAccount
PreviousAccount = $previousAccount
ExportData = [PSCustomObject]@{
OrgUnitPath = $updatedAccount.orgUnitPath
}
}
Write-Output ($result | ConvertTo-Json -Depth 10)
#endregion Build up result