Export Yammer user profile photos with PowerShell

Yammer allows you to export the users to a CSV file, but that comes without there profile picture. But there is a Yammer API, so with PowerShell and the data from the CSV file we are able to get the profile pictures as well.

Export the Yammer Users CSV file

First we need a CSV file with all the users, there for you need to be a network admin withing you Yammer site. Go to Settings > Network admin > Export Users and Export all the users.

When you open the CSV file, you will see a column with a link to the Yammer API for each user. When you open this url in you browser you will get an XML file with the users data.

In that XML file is a attribute, mugshot-url, that is the profile picture that we want.

Connect to Yammer API

Before we can interact with the Yammer API we first need to register an App so we can authorize the requests.  Patrick Lamber as written a great post about this, you can find it here. In short do the following:

  1. Make sure you are logged-in as admin to your Yammer
  2. Go to the following address: https://www.yammer.com/client_applications
  3. Register you application, you can use face address
  4. Once registered, click Generate a developer token for this application

When done copy the generated (bearer) token somewhere, we need it later.

Creating the PowerShell script

So we have a CSV file with all the users, a link to their XML file and we have a bearer token to authenticate. Time to start scripting:

Step 1 – Import the CSV file and filter the users

I added an file so you can select all users or only the active ones.

#Set some variables
$csvFile = "C:\temp\yammer\Users.csv"
$exportFolder = "c:\temp\yammer\users\"
$allUsers = $false

#Import CSV
$csv = Import-Csv $csvFile -Delimiter ","
$state = if ($allUsers) { '*' } else { 'active' }

#Filter users
$users = $csv | Where-Object state -Like $state

Step 2 – Create the REST API header

To download the XML file we need to be authenticated, we can do this with the bearer token that we got earlier from the Yammer Developer console.

#Connect to Yammer API
$baererToken = "455423-asdwewqADSLKSAHDL" #Fake token 😉

$headers = @{ Authorization=("Bearer " + $baererToken) }

Step 3 – Get the XML file

For each user we need to get the XML file. The invoke-webrequest returns a string, so we convert it to XML to make it easier to work with.

$response = Invoke-WebRequest -Uri $user.api_url –Method Get -Headers $headers -ContentType "application/xml"
$xml = [xml]$response

Step 4 – Download the Profile Photo

We download the photo with a webrequest:

Invoke-WebRequest -Uri $xml.response.'mugshot-url' -OutFile $exportFolder\$xml.response.name –Method Get -Headers $headers

Step 5 – Putting it al together

The code snippets above needs to put together, you want to loop trough the CSV users, create the output location based on there name, maybe request a different size then the default 48×48 and check if they have a photo set.

So when we combine all the peaces together we get the following script:

$csvFile = "C:\temp\yammer\Users.csv"
$exportFolder = "c:\temp\yammer\users\"
$allUsers = $false

$csv = Import-Csv $csvFile -Delimiter ","
$state = if ($allUsers) { '*' } else { 'active' }

#filter users
$users = $csv | Where-Object state -Like $state

#connect to Yammer API
$baererToken = "455423-asdwewqADSLKSAHDL" #Fake token 😉

$headers = @{ Authorization=("Bearer " + $baererToken) }

foreach ($user in $users)
{
	#set output filename
	$output = $exportFolder + $user.name + '.png'
	
	#get xml
	$response = Invoke-WebRequest -Uri $user.api_url –Method Get -Headers $headers -ContentType "application/xml"
	$xml = [xml]$response

	#get profile address from xml file
	$photoUrl = $xml.response.'mugshot-url'

	$url = $photoUrl.replace('48x48','256x256')
	
	#download photo
	if ($photoUrl) {
		
		#Check if it's a user picture or placeholder
		if ($photoUrl -Match 'no_photo') {
			#Do something else
		}else{
			Invoke-WebRequest -Uri $url -OutFile $output –Method Get -Headers $headers
		}
		
	}
}

 

 

Leave a Comment

0 Shares
Tweet
Pin
Share
Share