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