In this blog, you will see how to get messages for a specific topic in Yammer using PowerShell.

Prerequisites

Go to https://www.yammer.com/client_applications and register an app.

Once the app is registered, generate a developer token.

Copy the below script and paste it in a notepad. Save the file as GetMessages.ps1.

  1. # Input Parameters
  2. $developerToken = "12240-*****PR2NWpZVtnbXYw"
  3. $topicID="32397927"
  4. $uri="https://www.yammer.com/api/v1/messages/about_topic/"+$topicID+".json"
  5. $headers = @{ Authorization=("Bearer " + $developerToken) }
  6. Function GetMessages($pageNo)
  7. {
  8. # Invoke Web Request
  9. $webRequest = Invoke-WebRequest –Uri $uri –Method Get -Headers $headers
  10. # Check whether the status code is 200
  11. if ($webRequest.StatusCode -eq 200) {
  12. # Converts a JSON-formatted string to a custom object or a hash table.
  13. $results = $webRequest.Content | ConvertFrom-Json
  14. # Loop through all the messages
  15. $results.messages | ForEach-Object {
  16. $message = $_
  17. #Display the message ID and Created date
  18. write-host -ForegroundColor Green "Message ID: " $message.id " Created_at: " $message.created_at
  19. }
  20. # Check if there are more items available
  21. if($results.more_available)
  22. {
  23. GetUsers($pageNo+1)
  24. }
  25. }
  26. else {
  27. Write-Host -ForegroundColor Yellow "An error has occurred: " + $webRequest.StatusCode + " Description " + $webRequest.Status
  28. }
  29. }
  30. # Call the function
  31. GetMessages(1)

Open PowerShell window and run the following command.

  1. >cd "folderlocation>"

folderlocation – GetMessages.ps1 file location

Run the following command

  1. >.\GetMessages.ps1

Thus in this blog, you saw how to get messages for a specific topic in Yammer using PowerShell.