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:
- Make sure you are logged-in as admin to your Yammer
- Go to the following address: https://www.yammer.com/client_applications
- Register you application, you can use face address
- 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.
1. #Set some variables 2. $csvFile = "C:\temp\yammer\Users.csv" 3. $exportFolder = "c:\temp\yammer\users\" 4. $allUsers = $false 5. 6. #Import CSV 7. $csv = Import-Csv $csvFile -Delimiter "," 8. $state = if ($allUsers) { '*' } else { 'active' } 9. 10. #Filter users 11. $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.
1. #Connect to Yammer API 2. $baererToken = "455423-asdwewqADSLKSAHDL" #Fake token 😉 3. 4. $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.
1. $response = Invoke-WebRequest -Uri $user.api_url –Method Get -Headers $headers -ContentType "application/xml" 2. $xml = [xml]$response
Step 4 – Download the Profile Photo
We download the photo with a webrequest:
1. 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:
1. $csvFile = "C:\temp\yammer\Users.csv" 2. $exportFolder = "c:\temp\yammer\users\" 3. $allUsers = $false 4. 5. $csv = Import-Csv $csvFile -Delimiter "," 6. $state = if ($allUsers) { '*' } else { 'active' } 7. 8. #filter users 9. $users = $csv | Where-Object state -Like $state 10. 11. #connect to Yammer API 12. $baererToken = "455423-asdwewqADSLKSAHDL" #Fake token 😉 13. 14. $headers = @{ Authorization=("Bearer " + $baererToken) } 15. 16. foreach ($user in $users) 17. { 18. #set output filename 19. $output = $exportFolder + $user.name + '.png' 20. 21. #get xml 22. $response = Invoke-WebRequest -Uri $user.api_url –Method Get -Headers $headers -ContentType "application/xml" 23. $xml = [xml]$response 24. 25. #get profile address from xml file 26. $photoUrl = $xml.response.'mugshot-url' 27. 28. $url = $photoUrl.replace('48x48','256x256') 29. 30. #download photo 31. if ($photoUrl) { 32. 33. #Check if it's a user picture or placeholder 34. if ($photoUrl -Match 'no_photo') { 35. #Do something else 36. }else{ 37. Invoke-WebRequest -Uri $url -OutFile $output –Method Get -Headers $headers 38. } 39. 40. } 41. }