-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGoogleAPIFieldPrimary.ps1
More file actions
338 lines (272 loc) · 11.2 KB
/
GoogleAPIFieldPrimary.ps1
File metadata and controls
338 lines (272 loc) · 11.2 KB
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
# =========================================
# Google Workspace User Attribute Repair
# (Exclaimer fix for missing primary fields)
# =========================================
# SYNOPSIS:
# This script will set the 'primary' flag on a user's 'organizations' entry in Google Directory
# so it is correctly synced with Exclaimer. It uses a service account with domain-wide delegation
# to perform updates via the Admin SDK API.
#
# INSTRUCTIONS:
# Please review the pre requisites in page below:
# https://github.com/exclaimerltd/Internal-Support-Scripts/blob/master/resources/GoogleAPIFieldPrimary.MD
# Push Enter to open it on your default browser, then follow the steps.
# Once completed, enter "C" to continue.
# =========================================
# Check if script is running in PowerShell 7+
if ($PSVersionTable.PSEdition -ne 'Core' -or $PSVersionTable.PSVersion.Major -lt 7) {
Write-Host "This script must be run in PowerShell 7 or later." -ForegroundColor Red
Write-Host "Please run this script using pwsh (PowerShell 7+)." -ForegroundColor Yellow
Write-Host ""
Read-Host "Press Enter to close"
return # Stop script execution without closing the session
}
# Default path: Downloads folder
$Global:FilePath = [System.IO.Path]::Combine([Environment]::GetFolderPath('UserProfile'), 'Downloads')
$LogFile = "GoogleUsersUpdateLog_$(Get-Date -Format 'HHmmss').csv"
# Check if the path exists, if not, use C:\Temp
if (-not (Test-Path -Path $Global:FilePath)) {
$Global:FilePath = "C:\Temp"
if (-not (Test-Path -Path $Global:FilePath)) {
New-Item -Path $Global:FilePath -ItemType Directory -Force | Out-Null
}
}
# Final full path for the log file
$FullLogFilePath = Join-Path $Global:FilePath $LogFile
# Prompt user to review pre-requisites page
Write-Host "`nPlease review the pre requisites in the page below:" -ForegroundColor Cyan
Write-Host "https://github.com/exclaimerltd/Internal-Support-Scripts/blob/master/resources/GoogleAPIFieldPrimary.MD"
Read-Host "Push Enter to open it on your default browser"
Start-Process "https://github.com/exclaimerltd/Internal-Support-Scripts/blob/master/resources/GoogleAPIFieldPrimary.MD"
# Wait for user confirmation
do {
$confirm = Read-Host "Once you completed the steps in the article, please enter 'C' followed by Enter to continue"
} until ($confirm -eq 'C')
function EnsureModule {
param([string]$Name)
# Ensure the required module is installed
if (-not (Get-Module -ListAvailable -Name $Name)) {
Write-Host "Installing required module: $Name"
Install-Module $Name -Scope CurrentUser -Force
}
}
# (rest of your script continues unchanged...)
function Get-GoogleAccessToken {
param(
[Parameter(Mandatory)]
[string]$JsonKeyPath,
[Parameter(Mandatory)]
[string]$AdminUser
)
try {
# Load service account JSON
$json = Get-Content $JsonKeyPath -Raw | ConvertFrom-Json
}
catch {
throw "Unable to read the service account JSON file. Check the file path and file permissions."
}
try {
$now = [DateTimeOffset]::UtcNow.ToUnixTimeSeconds()
$jwtHeader = @{ alg = "RS256"; typ = "JWT" } | ConvertTo-Json -Compress
$jwtClaim = @{
iss = $json.client_email
scope = "https://www.googleapis.com/auth/admin.directory.user"
aud = "https://oauth2.googleapis.com/token"
exp = $now + 3600
iat = $now
sub = $AdminUser
} | ConvertTo-Json -Compress
$headerEncoded = [Convert]::ToBase64String(
[Text.Encoding]::UTF8.GetBytes($jwtHeader)
).TrimEnd("=")
$claimEncoded = [Convert]::ToBase64String(
[Text.Encoding]::UTF8.GetBytes($jwtClaim)
).TrimEnd("=")
$rsa = [System.Security.Cryptography.RSA]::Create()
$rsa.ImportFromPem($json.private_key)
$signature = $rsa.SignData(
[Text.Encoding]::UTF8.GetBytes("$headerEncoded.$claimEncoded"),
[System.Security.Cryptography.HashAlgorithmName]::SHA256,
[System.Security.Cryptography.RSASignaturePadding]::Pkcs1
)
$sigEncoded = [Convert]::ToBase64String($signature).TrimEnd("=")
$jwt = "$headerEncoded.$claimEncoded.$sigEncoded"
}
catch {
throw "Failed to generate the OAuth JWT. Ensure the script is running in PowerShell 7+ and the JSON key is valid."
}
try {
$body = @{
grant_type = "urn:ietf:params:oauth:grant-type:jwt-bearer"
assertion = $jwt
}
$token = Invoke-RestMethod -Method Post `
-Uri "https://oauth2.googleapis.com/token" `
-Body $body `
-ErrorAction Stop
return $token.access_token
}
catch {
$statusCode = $_.Exception.Response.StatusCode.value__ 2>$null
switch ($statusCode) {
400 { $friendly = "Token request rejected. Invalid Token or Service Account is not authorised for domain-wide delegation or the admin email is invalid." }
401 { $friendly = "Authentication failed. The service account key may be invalid or revoked." }
403 { $friendly = "Permission denied. Check domain-wide delegation, OAuth scopes, and ensure the admin user is a Super Admin." }
default { $friendly = "Failed to obtain an access token from Google. Check service account configuration and permissions." }
}
Write-Host "ERROR: $friendly (HTTP $statusCode)" -ForegroundColor Red
exit 1
}
}
function Update-GoogleUser {
param(
[string]$UserEmail,
[string]$AccessToken,
[hashtable]$Payload,
[int]$Count
)
$headers = @{
Authorization = "Bearer $AccessToken"
}
$status = ""
try {
Invoke-RestMethod `
-Method Patch `
-Uri "https://admin.googleapis.com/admin/directory/v1/users/$UserEmail" `
-Headers $headers `
-Body ($Payload | ConvertTo-Json -Depth 5) `
-ContentType "application/json"
$status = "Success"
return $true
}
catch {
$status = "Failed: $($_.Exception.Message)"
return $false
}
finally {
$logEntry = [PSCustomObject]@{
Count = $Count
Timestamp = (Get-Date -Format 'yyyy-MM-dd HH:mm:ss')
UserEmail = $UserEmail
Status = $status
}
if (-not (Test-Path -Path $FullLogFilePath)) {
$logEntry | Export-Csv -Path $FullLogFilePath -NoTypeInformation
}
else {
$logEntry | Export-Csv -Path $FullLogFilePath -NoTypeInformation -Append
}
}
}
# ================== SCRIPT START ==================
EnsureModule -Name "PowerShellGet"
Clear-Host
Write-Host " -----------------------------------------------" -ForegroundColor Cyan
Write-Host " | EXCLAIMER |" -ForegroundColor Yellow
Write-Host " | Google Workspace User Attribute Fix |" -ForegroundColor Yellow
Write-Host " -----------------------------------------------" -ForegroundColor Cyan
Write-Host ""
Start-Sleep -Seconds 1
# Prompt for JSON key file path
do {
$jsonPath = Read-Host "Enter full path to JSON key file (e.g. C:\Temp\my_token.json)"
# Remove surrounding quotes if present
$jsonPath = $jsonPath.Trim('"')
if (-not (Test-Path -Path $jsonPath -PathType Leaf)) {
Write-Host "File not found. Please enter a valid file path." -ForegroundColor Yellow
$jsonPath = $null
}
} until ($jsonPath)
# Prompt for Google Admin email address
$emailRegex = '^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$'
do {
$adminUser = Read-Host "Enter Google Admin email address for delegation"
# Force lowercase
$adminUser = $adminUser.ToLower()
if ($adminUser -notmatch $emailRegex) {
Write-Host "Invalid email address format. Please try again." -ForegroundColor Yellow
$adminUser = $null
}
} until ($adminUser)
$accessToken = Get-GoogleAccessToken -JsonKeyPath $jsonPath -AdminUser $adminUser
$field = "organizations"
Write-Host "`nField to update: $field"
# Hardcoded JSON to set primary=true on first organization
$orgUpdate = ConvertFrom-Json '[{"primary": true}]'
$scope = Read-Host "Update (1) single user or (2) all users?"
Write-Host ""
if ($scope -eq "1") {
$users = @((Read-Host "Enter user email address"))
}
else {
$headers = @{ Authorization = "Bearer $accessToken" }
$users = @()
$pageToken = $null
do {
$uri = "https://admin.googleapis.com/admin/directory/v1/users?customer=my_customer&maxResults=500&query=isSuspended=false"
if ($pageToken) {
$uri += "&pageToken=$pageToken"
}
$response = Invoke-RestMethod -Uri $uri -Headers $headers
if ($response.users) {
$users += $response.users | ForEach-Object { $_.primaryEmail }
}
$pageToken = $response.nextPageToken
}
while ($pageToken)
}
# Count goes here
$totalUsers = $users.Count
$counter = 0
Write-Host "`nTotal users to be processed: $totalUsers`n"
foreach ($user in $users) {
$counter++
Write-Host "`rProcessing ($counter / $totalUsers): $user".PadRight(80) `
-ForegroundColor Yellow -NoNewline
# Fetch current organizations array
$current = Invoke-RestMethod `
-Uri "https://admin.googleapis.com/admin/directory/v1/users/$user" `
-Headers @{ Authorization = "Bearer $accessToken" }
$orgs = @()
if ($current.organizations) { $orgs = $current.organizations }
if ($orgs.Count -eq 0) {
Write-Host "`rSkipping ($counter / $totalUsers): no org - $user".PadRight(80) `
-ForegroundColor DarkYellow -NoNewline
continue
}
# CHECK: already primary?
if ($orgs[0].primary -eq $true) {
Write-Host "`rSkipping ($counter / $totalUsers): already primary - $user".PadRight(80) `
-ForegroundColor DarkYellow -NoNewline
# Optional: log skip
$logEntry = [PSCustomObject]@{
Count = $counter
Timestamp = (Get-Date -Format 'yyyy-MM-dd HH:mm:ss')
UserEmail = $user
Status = "Skipped (already primary)"
}
if (-not (Test-Path $FullLogFilePath)) {
$logEntry | Export-Csv $FullLogFilePath -NoTypeInformation
}
else {
$logEntry | Export-Csv $FullLogFilePath -NoTypeInformation -Append
}
continue
}
# Only update the first organization
foreach ($prop in $orgUpdate[0].PSObject.Properties.Name) {
if (-not $orgs[0].PSObject.Properties[$prop]) {
$orgs[0] | Add-Member -MemberType NoteProperty -Name $prop -Value $orgUpdate[0].$prop -Force
}
else {
$orgs[0].$prop = $orgUpdate[0].$prop
}
}
$payload = @{ organizations = $orgs }
Update-GoogleUser -UserEmail $user -AccessToken $accessToken -Payload $payload -Count $counter | Out-Null
}
Write-Host "`rProcessing completed".PadRight(80) -ForegroundColor Green -NoNewline
# Final message after all users are processed
Write-Host "`n`nUpdate completed. Log of processed users has been saved to:" -ForegroundColor Green
Write-Host $FullLogFilePath -ForegroundColor Cyan
Write-Host "`nAllow Google sync time before validating changes."