Forum Discussion

EntilZha's avatar
EntilZha
Iron Contributor
Jun 24, 2021

Variable in Graph Request Body (PowerShell)

Working off the following URL: https://docs.microsoft.com/en-us/graph/api/passwordauthenticationmethod-resetpassword?view=graph-rest-beta&tabs=http

 

 

When I execute the following command in my PowerShell script it works flawlessly

$PWCBody = '{
"newPassword" : "ssd$$FGW!!",
"forceChangePasswordNextSignIn" : true
}'


$PWCURI = "https://graph.microsoft.com/beta/users/XXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/authentication/passwordMethods/XXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/resetPassword"
$PWChange = Invoke-RestMethod -Uri $PWCURI -Headers $HeaderDelegate -body $PWCbody -Method POST -ContentType "application/json"

 

However, when I change the Request Body newPassword to variable I get a (403) Bad Request. 

 

$PWCBody = '{

"newPassword" : $password,
"forceChangePasswordNextSignIn" : true
}'

 

How can i handle a variable in a Requested Body that's surrounded by single quotes?

 

Thank You,

 

-Larry 

 

  • SOLUTION: I had to add " | ConvertTo-Json" at the end Request Body
    $PWCBody = @{
    newPassword = $password
    forceChangePasswordNextSignIn = $true
    } | ConvertTo-Json
    • EntilZha's avatar
      EntilZha
      Iron Contributor
      Thank You for responding Vasil...
      I tried using that format
      $PWCBody = @{
      "newPassword" = $password
      }

      Still getting the following message "Invoke-RestMethod : The remote server returned an error: (400) Bad Request."

      Thank You,

      -Larry

Resources