Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am working on some automation to set out of office for people using PowerShell. Im running into a problem but im not sure where im going wrong.

When i run the script to disable the out of office it throws an error saying Empty Payload. JSON content expected

#Setup out of office using graph

#Connect to MS Graph using seamless auth with registered application and client ID secret
# Application (client) ID, tenant Name and secret
$clientId = "xxxxxxx" #application ID
$tenantName = "xxxxxxxx"
$clientSecret = "xxxxxxxx"

$ReqTokenBody = @{
    Grant_Type    = "client_credentials"
    Scope         = "https://graph.microsoft.com/.default"
    client_Id     = $clientID
    Client_Secret = $clientSecret
} 

$TokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token" -Method POST -Body $ReqTokenBody

#user being processed
$upn = "[email protected]"

#out of office settings
$oof = @"
    "@odata.context": "https://graph.microsoft.com/v1.0/users/$upn/mailboxSettings",
    "automaticRepliesSetting": {
        "status": "disabled"
    }

"@

$oof = $oof | ConvertTo-Json

$apiUrl = "https://graph.microsoft.com/beta/users/$upn/mailboxSettings" 

Invoke-RestMethod -Headers @{Authorization = "Bearer $($Tokenresponse.access_token)"} -Uri $apiUrl -UseBasicParsing -Method patch -ContentType "application/json" -Body $oof -Verbose

This is the error that is generated

Invoke-RestMethod : {
    "error": {
      "code": "BadRequest",
      "message": "Empty Payload. JSON content expected.",
      "innerError": {
        "date": "2021-01-11T14:04:16",
        "request-id": "xxxx",
        "client-request-id": "xxxxx"
      }
    }
  }
  At line:33 char:1
  + Invoke-RestMethod -Headers @{Authorization = "Bearer $($Tokenresponse ...
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
      + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
759 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try to add brackets {}:

$oof = @"
{
  "@odata.context": "https://graph.microsoft.com/v1.0/users/$upn/mailboxSettings",
  "automaticRepliesSetting": {
    "status": "disabled"
   }
}
"@

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...