group

Bulk create Yammer groups using Windows PowerShell

Hello again,

Recently I’ve been playing with Yammer. One of my customers requested a way to bulk import Yammer groups including group membership from their Active Directory. The first part was simply to export their Active Directory group membership data into CSV, something that has been covered in many technical blog posts before so we’re not going to cover it here. The other part was to import the CSV data into Yammer.

The process consists of two major steps. The first step is to login to Yammer using Windows PowerShell by creating an access token. Yammer uses REST API. There is a beautiful blog post by Dave Young that I’ve used here. The second step is the groups creation.

First, let us start by registering a Yammer to be able to generate an access token for Windows PowerShell to use.

  1. Open the Yammer Portal and Login using a verified admin account
  2. Expand the dotted menu and choose “APPS”
    Screen1
  3. Click on “My Apps” button
    Screen2
  4.  Click on “Register New App” button
    Screen3
  5. Supply the values as the example below (set the values relative to your tenant)
    Screen4
  6. Once you are done, make sure you copy the “Client ID”, “Client secret”, and “Expected redirect” values into the “Token Creator.ps1” script
    Screen5

The “Token Creator.ps1” which was inspired by Dave Young in his post here goes like this:

$clientID = "KKiVk**********A0lWA"
$clientsecret = "JTINYDBQ********************UUyz4AiNV8xg"
$RedirURL = "https://www.yammer.com/*****.com"
 
$ie = New-Object -ComObject internetexplorer.application
$ie.Visible = $true
$ie.Navigate2("https://www.yammer.com/dialog/oauth?client_id=$clientID&redirect_uri=$RedirURL")

Sleep 100

$UrlTidy = $ie.LocationURL -match 'code=(......................)';
$Authcode = $Matches[1]
$ie = New-Object -ComObject internetexplorer.application
$ie.Visible = $true
$ie.Navigate2("https://www.yammer.com/oauth2/access_token.json?client_id=$clientID&client_secret=$clientsecret&code=$Authcode")

The script will open a new IE window (requires PowerShell 3.0 or above) and navigate to a special URL that allows your Office365 user account trust the Yammer App you just created. After that, another IE window will appear that will prompt you to download your access token. Download the file and rename it to “access_token.json”.

You can then edit the script “Group Creator.ps1” with the location of the new access token, and the csv file with the following format:

Screen-Shot-2016-06-06-at-11.59.04-AM

The script will create a group with the specified name and add the semicolon separated users in the “Members” field (user email) as members in the group. In case a “GroupId” was supplied, the script will assume that the group is already created, and will attempt to append the users to the group members.

Note that you can get the group Id of any existing group by navigating to the group’s page and copying the feed Id value from the navigation bar.

Capture

The script “Group Creator.ps1” goes like this:

 $TokenPath = 'C:\Workspace\access_token.json'
$CSVPath = 'C:\Workspace\Yammer Groups.csv'

###Reading token###############################################################################################
$Openjson = $(Get-Content $TokenPath ) -join "`n" | ConvertFrom-Json
$token = $Openjson.access_token.token
###############################################################################################################

###Creating header#############################################################################################
$Headers = @{
"Accept" = "*/*"
"Authorization" = "Bearer "+$token
"accept-encoding" = "gzip"
"content-type"="application/json"
}
###############################################################################################################


###Reading groups##############################################################################################
$GroupItems = Import-Csv -Path $CSVPath

foreach ($GroupItem in $GroupItems)
{
###Creating Yammer group#######################################################################################
$Group = $GroupItem.Group

if ($GroupItem.Type -eq 'Private')
{
$GroupType = $true
}
else
{
$GroupType = $false
}

$CreateGroupUri = "https://www.yammer.com/api/v1/groups.json?name=$Group&private=$GroupType"

$CreateGroupResponse = Invoke-RestMethod -Method Post -Uri $CreateGroupUri -Header $Headers
###############################################################################################################

###Enumerating members#########################################################################################
$Members = $GroupItem.Members.Split(';')

foreach ($Member in $Members)
{
###Adding users to Yammer group################################################################################
$GroupID = $CreateGroupResponse.id
$UserEmail = $Member

$GroupMembershipUri = "https://www.yammer.com/api/v1/group_memberships.json?group_id=$GroupID&email=$UserEmail"

$GroupMembershipResponse = Invoke-RestMethod -Method Post -Uri $GroupMembershipUri -Header $Headers
###############################################################################################################
}
###############################################################################################################
}
############################################################################################################### 

The script may generate errors if a group with the same name already exists, or the user is already a member of that group. These errors can be ignored.

 

I hope you can find this useful.

Hazem Elshabini