Skip to content

Commit 886b315

Browse files
committed
Add invite_members_to_org.ps1 script
1 parent c216345 commit 886b315

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#Requires -version 7
2+
3+
<#
4+
.SYNOPSIS
5+
Invites GitHub Enterprise members to an organization
6+
7+
.DESCRIPTION
8+
This script runs a batch organization invitation process for a given list of GitHub enterprise consumed licenses.
9+
10+
The input is a CSV file with a column named "Handle or email", such as can be exported from the Enterprise settings > Enterprise licensing page. Users with appropriate permissions can export the CSV file, edit it in their favorite spreadsheet to select emails to invite, then use this script to invite them to an org.
11+
12+
.PARAMETER LicensesFile
13+
The path of the consumed licenses CSV.
14+
15+
.PARAMETER Organization
16+
The name of the organization to invite members to.
17+
18+
.PARAMETER PAT
19+
The personal access token. It must have "admin:org" scope to be authorized for the operation.
20+
21+
.EXAMPLE
22+
.\invite_members_to_org.ps1 -LicensesFile .\consumed_licenses.csv -Organization my-organization -PAT xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
23+
#>
24+
25+
param (
26+
[string] [Parameter(Mandatory=$true)] $LicensesFile,
27+
[string] [Parameter(Mandatory=$true)] $Organization,
28+
[string] [Parameter(Mandatory=$true)] $PAT
29+
)
30+
31+
Import-Csv $LicensesFile | ForEach-Object {
32+
Write-Host "---------------------------------------------"
33+
34+
$Body = @{}
35+
if ($_."Handle or email" -Match "@") {
36+
Write-Host "Inviting email $($_."Handle or email")..."
37+
$Body.email = $_."Handle or email"
38+
} else {
39+
Write-Host "Inviting handle $($_."Handle or email")..."
40+
$HandleIdRequest = Invoke-RestMethod -SkipHttpErrorCheck -Uri "https://api.github.com/users/$($_."Handle or email")"
41+
if ($null -ne $HandleIdRequest.id) {
42+
Write-Host "> Handle id is $($HandleIdRequest.id)" -ForegroundColor 'green'
43+
} else {
44+
Write-Host "> Handle id not found" -ForegroundColor 'red'
45+
}
46+
$Body.invitee_id = $HandleIdRequest.id
47+
}
48+
49+
$headers = @{
50+
"Accept" = "application/vnd.github.v3+json"
51+
"Authorization" = "token $($PAT)"
52+
}
53+
54+
$InvitationRequest = Invoke-RestMethod -StatusCodeVariable "StatusCode" -SkipHttpErrorCheck -Uri "https://api.github.com/orgs/$($Organization)/invitations" -Method Post -Headers $headers -Body ($body | ConvertTo-Json)
55+
if ($StatusCode -eq 201) {
56+
Write-Host "> Success!" -ForegroundColor 'green'
57+
} else {
58+
Write-Host "> Error!" -ForegroundColor 'red'
59+
Write-Host "> Status code: $($StatusCode)" -ForegroundColor 'red'
60+
Write-Host "> $($InvitationRequest | ConvertTo-Json)" -ForegroundColor 'red'
61+
}
62+
}
63+
64+
Write-Host "---------------------------------------------"
65+
Write-Host "End of file"

0 commit comments

Comments
 (0)