How To Get Messages For A Specific Topic In Yammer Using PowerShell

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.   
  7. Function GetMessages($pageNo)  
  8. {  
  9.     # Invoke Web Request  
  10.     $webRequest = Invoke-WebRequest –Uri $uri –Method Get -Headers $headers  
  11.   
  12.     # Check whether the status code is 200  
  13.     if ($webRequest.StatusCode -eq 200) {  
  14.   
  15.         # Converts a JSON-formatted string to a custom object or a hash table.   
  16.         $results = $webRequest.Content | ConvertFrom-Json          
  17.   
  18.         # Loop through all the messages  
  19.         $results.messages | ForEach-Object {  
  20.             $message = $_   
  21.   
  22.             #Display the message ID and Created date  
  23.             write-host -ForegroundColor Green "Message ID: " $message.id " Created_at: " $message.created_at  
  24.               
  25.         }  
  26.   
  27.         # Check if there are more items available      
  28.         if($results.more_available)      
  29.         {      
  30.             GetUsers($pageNo+1)      
  31.         }    
  32.     }  
  33.     else {  
  34.         Write-Host -ForegroundColor Yellow "An error has occurred: " + $webRequest.StatusCode + " Description " + $webRequest.Status  
  35.     }  
  36. }     
  37.       
  38. # Call the function      
  39. 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.