Skip to content

Instantly share code, notes, and snippets.

@panreel
Created November 8, 2018 16:47
Show Gist options
  • Save panreel/62f41b3bdd66ed02127e694feda1029e to your computer and use it in GitHub Desktop.
Save panreel/62f41b3bdd66ed02127e694feda1029e to your computer and use it in GitHub Desktop.
Activate users in bulk by using SCIM API

Activate Workplace users in bulk with a XSLX file in input

This PowerShell script allows to activate Workplace users in bulk.

Setup

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

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

    {
          "accessToken" : "YOUR-ACCESS-TOKEN"
    }
  • Export the list of users you'd like to activate: As an admin, you can export deactivated users' details by going to the People Tab in the Admin Portal > Filter in the search bar "Account status" is "deactivated" > Click on the three dots button on the right > Export employee information of {X} people. A mail message with the XLSX file with the requested information will be sent to your email address.

Note: Reactivating a user will reset their manager field.

Run

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

    ./activateInBulk.ps1 -WPExportedUsers workplace_users.xlsx -WPAccessToken accessToken.js -Interactive

    Here are the details of the passed params:

    Parameter Description Type Required
    WPExportedUsers The path for the XLSX file with the exported user data String Yes
    WPAccessToken The path for the JSON file with the access token String Yes
    Interactive If the script should display a message for each operation Switch No
param(
[Parameter(Mandatory=$true, HelpMessage='Path of the user export with the list of the users you would like to activate')] [string]$WPExportedUsers,
[Parameter(Mandatory=$true, HelpMessage='Path for your Workplace access token in .json format {"accessToken" : 123xyz}')] [string]$WPAccessToken,
[switch]$Interactive
)
#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;
}
#Read XLSX Export File
try {
#Read users from XLSX file
$global:xslxUsers = Import-Excel -Path $WPExportedUsers
Write-Host -NoNewLine "Workplace Users File: "
Write-Host -ForegroundColor Green "OK, Read!"
}
catch {
#Handle exception when unable to read file
Write-Host -ForegroundColor Red "Fatal Error when reading XLSX file. Is it the Workplace users export file?"
exit;
}
#Init Counters
$total = 0;
$activated = 0;
$errors = 0;
Foreach($u in $global:xslxUsers) {
$uid = $u."User Id"
$uname = $u."Full Name"
$total++
If($Interactive.IsPresent) {
Write-Host -NoNewLine [$uid/$uname] ->
}
#Craft a Body
$body = (@{
schemas=@("urn:scim:schemas:core:1.0")
active=$true
} | ConvertTo-Json)
try {
#Update User via SCIM API
$fbuser = Invoke-RestMethod -Method PUT -URI ("https://www.facebook.com/scim/v1/Users/" + $uid) -Headers @{Authorization = "Bearer " + $global:token} -ContentType "application/json" -Body $body
If($Interactive.IsPresent) {
Write-Host -ForegroundColor Green " Activated"
}
$activated++
}
catch {
$errors++
# Dig into the exception and print error message
$status = $_.Exception.Response.StatusCode.value__
$err = $_.Exception.Response.StatusCode
$msg = ($_.ErrorDetails.Message | ConvertFrom-Json).Errors[0].description
If($Interactive.IsPresent) {
Write-Host -ForegroundColor Red " KO ($status): $err - $msg"
} Else {
Write-Host -NoNewLine [$uid/$uname] ->
Write-Host -ForegroundColor Red "KO ($status): $err - $msg"
}
}
}
Write-Host "---------------------------------------------------------------------------------------------------------"
Write-Host -NoNewLine -ForegroundColor Yellow "Summary "
Write-Host "- Total User: $total - Activated ($activated), Errors ($errors)"
Write-Host "---------------------------------------------------------------------------------------------------------"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment