How To Post A Message In Yammer Group Using PowerShell

In this blog, you will see how to post a message in a Yammer group 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 PostMessage.ps1.

  1. # Input Parameters  
  2. $developerToken = "12240-*****iPR2NWpZVtnbXYw"  
  3. $groupID="15653442"  
  4. $uri="https://www.yammer.com/api/v1/messages.json"  
  5. $headers = @{ Authorization=("Bearer " + $developerToken) }  
  6. $body=@{group_id="15653442";body="This message is posted using PowerShell."}     
  7.   
  8. # Invoke Web Request  
  9. $webRequest = Invoke-WebRequest –Uri $uri –Method POST -Headers $headers -Body $body  
  10.   
  11. Check whether the status code is 201 created  
  12. if ($webRequest.StatusCode -eq 201) {  
  13.     write-host -ForegroundColor Green "Message posted successfully."     
  14. }  
  15. else {  
  16.     Write-Host -ForegroundColor Yellow "An error has occurred: " + $webRequest.StatusCode + " Description " + $webRequest.Status  
  17. }  

Open PowerShell window and run the following command.

  1. >cd "<folderlocation>"  

folderlocation – PostMessage.ps1 file location

Run the following command

  1. >.\PostMessage.ps1  

Thus in this blog, you saw how to post a message in Yammer group using PowerShell.