Forum Discussion

EntilZha's avatar
EntilZha
Iron Contributor
Jun 24, 2021
Solved

Variable in Graph Request Body (PowerShell)

Working off the following URL: https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fgraph%2Fapi%2Fpasswordauthenticationmethod-resetpassword%3Fview%3Dgraph-rest-beta%26tabs%3Dhttp&data=04%7C01%7CJonesL%40duvalschools.org%7C997660b94b6e446de25508d9370bfc97%7C8deb1d4dd0a44d0489aef7076cbaa9fb%7C1%7C0%7C637601346885178418%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=ZVLJ3SH%2FHpLXNgV1uJAZqdCpLfvKqy%2Bb3kn7epZeUDA%3D&reserved=0

 

 

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

7 Replies

    • 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