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

**URL:** <https://forums.powershell.org/t/invoke-rest-method-to-add-new-database-and-role-for-existing-user/14457>\
**Category:** PowerShell Help\
**Created:** [May 30, 2020, 4:26pm UTC](https://forums.powershell.org/t/invoke-rest-method-to-add-new-database-and-role-for-existing-user/14457 "2020-05-30T16:26:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pallavik](https://avatars.discourse-cdn.com/v4/letter/p/58956e/32.png) [@pallavik](https://forums.powershell.org/u/pallavik)\
**Post date:** [May 30, 2020, 4:26pm UTC](https://forums.powershell.org/t/invoke-rest-method-to-add-new-database-and-role-for-existing-user/14457/1 "2020-05-30T16:26:09Z")

</div>

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 databasenames@ roles. i used invoke rest method **post** to create a new user as below

project name: project1 -\> usera(username) -\> marketing@read(databasename@rolename)

$newusr = @{

 databaseName = 'admin'

 password = $pwd

 username = usera

 roles = @(

 @{

 databaseName = marketing

 roleName = read

 }

 )

 }

 Json = $newusr | ConvertTo-Json

$newusrUri = "https://cloud.mongodb.com/api/atlas/v1.0/groups/$projectId/databaseUsers"

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 -\> sales@read

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

$updateusrUri = "https://cloud.mongodb.com/api/atlas/v1.0/groups/$projectId/databaseUsers/admin/usera"

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!!

---

<div class="post-metadata">

**Author:** ![pallavik](https://avatars.discourse-cdn.com/v4/letter/p/58956e/32.png) [@pallavik](https://forums.powershell.org/u/pallavik)\
**Post date:** [May 30, 2020, 4:54pm UTC](https://forums.powershell.org/t/invoke-rest-method-to-add-new-database-and-role-for-existing-user/14457/2 "2020-05-30T16:54:00Z")

</div>

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

Existing entry : usera read@marketing

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

$getProjectUri = "https://cloud.mongodb.com/api/atlas/v1.0/groups/5e3c30c1c56c98da14781a87/databaseUsers/admin/usera"

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!!

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [June 1, 2020, 7:53am UTC](https://forums.powershell.org/t/invoke-rest-method-to-add-new-database-and-role-for-existing-user/14457/3 "2020-06-01T07:53:37Z")

</div>

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

[https://powershell.org/forums/topic/invoke-rest-method-in-powershell/](https://powershell.org/forums/topic/invoke-rest-method-in-powershell/)

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:29pm UTC](https://forums.powershell.org/t/invoke-rest-method-to-add-new-database-and-role-for-existing-user/14457/4 "2024-05-16T20:29:26Z")

</div>

Closed on May 16, 2024, 8:29pm UTC


