Invoke Rest Method to add new database and role for existing user

Welcome Forums General PowerShell Q&A Invoke Rest Method to add new database and role for existing user

Viewing 2 reply threads
  • Author
    Posts
    • #232159
      Participant
      Topics: 11
      Replies: 9
      Points: 110
      Rank: Participant

      Reg :how to append or add a database , role for existing user using invoke rest method in powershell?

      I have a project named project1 in Atlas mongodb. Inside project 1 , i have username and [email protected] roles. i used invoke rest method post to create a new user as below

      project name: project1 -> usera(username) -> [email protected]([email protected])

      $newusr = @{
              databaseName = ‘admin’
              password     = $pwd
              username     = usera
              roles        = @(
                  @{
                      databaseName = marketing
                      roleName     = read
                  }
              )
          }
          Json = $newusr | ConvertTo-Json
      Invoke-RestMethod -Method POST -Uri $newusrUri -Headers @{Authorization = “Basic $base64AuthInfo”} -ContentType “application/json” -Credential $credential -Body $Json

      Now , for usera(existing user), i want to add a new database with roles -> [email protected]

      I used Get Method to check for existence of user in the project; if the user exists , i am trying to add or append new db with roles.I used POST method and it failed as user already exists. i tried below PATCH command , which actually modified the existing database&roles instead of adding new database&roles for same user

        $update = @{
              roles        = @(
              @{
                  databaseName = $dbname
                  roleName     = $rolename
              }
          )
      }
      $updateJson = $update | ConvertTo-Json
      Invoke-RestMethod -Method PATCH -Uri $updateusrUri -Headers @{Authorization = “Basic $base64AuthInfo”} -ContentType “application/json” -Credential $credential -Body $updateJson
      Please provide your suggestions or which method should we use while adding a new database , roles to existing user using invoke rest method in power shell. Thank you!!
      • This topic was modified 1 month, 1 week ago by pk30.
    • #232168
      Participant
      Topics: 11
      Replies: 9
      Points: 110
      Rank: Participant

      I tried adding new database with roles for existing user using put method

      Existing entry : usera [email protected]

      New Entry with Put Method : I tried below command

      $params = @{
          “databaseName”=”admin”
          “password”=”changeme123”
          “username”=”usera”
          “roles”= @(
              @{
                  “databaseName”=”sales”;
                  “roleName”=”read”
                              }
          )
      }
      $body = ConvertTo-Json -InputObject $params
      Invoke-RestMethod -Method put -Uri $getProjectUri -Headers @{Authorization = “Basic $base64AuthInfo”} -ContentType “application/json” -Credential $credential -Body $body
      Error Message : Invoke-RestMethod : The remote server returned an error: (405) Method Not Allowed.
      Please help in adding a new database with role for existing user , Thankyou!!
    • #232471
      Participant
      Topics: 12
      Replies: 1643
      Points: 2,665
      Helping Hand
      Rank: Community Hero

      Refer to the API documentation, like posted in your other thread asking the same thing:

      invoke rest method in powershell

Viewing 2 reply threads
  • You must be logged in to reply to this topic.