Skip to content

Instantly share code, notes, and snippets.

@panreel
Last active March 4, 2019 08:01
Show Gist options
  • Save panreel/de99cd1096d09190e6021e8b756cd586 to your computer and use it in GitHub Desktop.
Save panreel/de99cd1096d09190e6021e8b756cd586 to your computer and use it in GitHub Desktop.
Export members from a Workplace group to a XLSX file

Export members from a group

This PowerShell script allows to export the members of a Workplace group to a XLSX file.

Setup

  • Create a new Custom Integration in the Workplace Admin Panel: Create a custom Integration.
    This requires at least "Manage Groups" and "Read User Emails" permissions. Take note of the Access Token.

  • Create a file named accessToken.js with the following content:

    {
          "accessToken" : "YOUR-ACCESS-TOKEN"
    }
  • Find your GroupId. Go in a browser to the Workplace group you would like to extract members from and take note of the GroupId from the URL you see in the browser bar:

    https://INSTANCE-NAME.facebook.com/groups/GROUP-ID

Run

  • Run the script by passing the GroupId and accessToken.js file as input:

    ./exportGroupMembers.ps1 -GroupId THIS-IS-A-GROUP-ID -WPAccessToken accessToken.js

    Here are the details of the passed params:

    Parameter Description Type Required
    GroupId The ID of the group String Yes
    WPAccessToken The path for the JSON file with the access token String Yes
  • A file named members-[GroupId].xlsx will be created in the same folder where your run the script. It will have Name, Id, Email, Administator columns.

param(
[Parameter(Mandatory=$true, HelpMessage='The ID of the Workplace Group you would like to export')] [string]$GroupId,
[Parameter(Mandatory=$true, HelpMessage='Path for your Workplace access token in .json format {"accessToken" : 123xyz}')] [string]$WPAccessToken
)
#Install ImportExcel Module
If(!(Get-module ImportExcel)){Install-Module ImportExcel -scope CurrentUser}
#Read JSON Access Token
try {
$global:token = (Get-Content $WPAccessToken | Out-String | ConvertFrom-Json -ErrorAction Stop).accessToken
Write-Host -NoNewLine "Access Token JSON File: "
Write-Host -ForegroundColor Green "OK, Read!"
}
catch {
#Handle exception when passed file is not JSON
Write-Host -ForegroundColor Red "Fatal Error when reading JSON file. Is it correctly formatted? {'accessToken' : 123xyz}"
exit;
}
#Get specific group in the community via Graph API
try {
$global:members = @()
$next = "https://graph.facebook.com/$GroupId/members/?fields=name,id,email,administrator"
do {
$results = Invoke-RestMethod -Uri ($next) -Headers @{Authorization = "Bearer " + $global:token}
if ($results) {
$global:members += $results.data
if($results.paging.cursors.after) {
$after = $results.paging.cursors.after
$next = "https://graph.facebook.com/$GroupId/members/?fields=name,id,email,administrator&after=$after"
}
else {$next = $null}
}
else {$next = $null}
} while($next)
} catch {
#Handle exception when having errors from Graph API
Write-Host -ForegroundColor Red "Fatal Error when getting group members from API. Is the GroupId you passed correct? Are API permissions correct?"
exit;
}
#Add members to XLSX
try {
#Clean email-less fields
$($global:members | Where-Object {$_.email.endswith("emailless.facebook.com")}).email = $null
#Format property names
$global:members | `
ForEach-Object -Process {$_} | `
Select-Object -property `
@{N='Full Name';E={$_.name}}, `
@{N='Id';E={$_.id}}, `
@{N='Email';E={$_.email}}, `
@{N='Administrator';E={$_.Administrator}} | `
Export-Excel "./members-$GroupId.xlsx" -NoNumberConversion *
Write-Host -NoNewLine "Users written to XLSX: "
Write-Host -ForegroundColor Green "OK, Written!"
} catch {
#Handle exception when writing to output XLSX
Write-Host -ForegroundColor Red "Fatal Error when writing to XLSX file!"
exit;
}
@meder147
Copy link

meder147 commented Mar 4, 2019

I've get an error when trying to write the XLSX file - the error messages says

The property 'email' cannot be found on this object. Verify that the property exists and can be set.

Any idea how to fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment