Hi All,
I am trying to convert below curl coomand into powershell.
curl --user “{PUBLIC-KEY}:{PRIVATE-KEY}” --digest
–header “Accept: application/json”
–header “Content-Type: application/json”
–include
–request POST “https://cloud.mongodb.com/api/atlas/v1.0/groups/{PROJECT-ID}/databaseUsers”
–data ’
{
“databaseName”: “admin”,
“password”: “changeme123”,
“roles”: [{
“databaseName”: “sales”,
“roleName”: “readWrite”
}, {
“databaseName”: “marketing”,
“roleName”: “read”
}],
“username”: “david”
}’
I converted above curl into Restmethod to execute in powershell
$Json = '{"databaseName":"admin","password":"changeme123","username":"sample","roles":[{"databaseName":"sales","roleName":"read",}]}'
Invoke-RestMethod -Uri $getProjectUri -Body $Json -ContentType Application/Json -Headers @{Authorization = "Basic $base64AuthInfo"} -Credential $credential -Method Post
Here is the Error Message :
Invoke-RestMethod : {"detail":"Received JSON for the roles attribute does not match expected
format.","error":400,"errorCode":"INVALID_JSON_ATTRIBUTE","parameters":["roles"],"reason":"Bad Request"}
At C:\Users\pik0097\powershellscript\a3.ps1:10 char:3
+ Invoke-RestMethod -Uri $getProjectUri -Body $Json -ContentType Appl ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
My Analysis:
I am passing -> "roles":[{"databaseName":"sales","roleName":"read",}] ->in below json variable,which is being passed as json but as per microsoft documentation , expected type for roles is string array
roles.databaseName=sales , roles.roleName=read
Please suggest
$Json = '{"databaseName":"admin","password":"changeme123","username":"sample","roles":[{"databaseName":"sales","roleName":"read",}]}'
My advise don’t try to format JSON manually in PowerShell. Build your data as hashtables and use Convertto-JSON as in:
$DataToSend = @{
databaseName = 'admin'
password = 'changeme123'
username = 'sample'
roles = @(
@{
databaseName = 'sales'
roleName = 'read'
}
@{
databaseName = 'marketing'
roleName = 'read'
}
)
}
$Json = $DataToSend | ConvertTo-Json
$Json
and your JSON object would look like:
{
"username": "sample",
"databaseName": "admin",
"password": "changeme123",
"roles": [
{
"databaseName": "sales",
"roleName": "read"
},
{
"databaseName": "marketing",
"roleName": "read"
}
]
}
Thank you so much @Sam boutrous , it is working , you made my day
MongoDB is a pretty popular database, there is probably already pre-built powershell modules:
Hi All,
I used below code to add user named sample for sales database with read access
$DataToSend = @{
databaseName = ‘admin’
password = ‘changeme123’
username = ‘sample’
roles = @(
@{
databaseName = ‘sales’
roleName = ‘read’
}
)
}
$Json = $DataToSend | ConvertTo-Json
$Json
If i have to append a new role for the same user “sample” for “marketing” database with readWrite access , can you please suggest. I tried below approaches and failed.
- with patch command I found that existing sales db is being replaced with marketing db instead of append/ adding marketing db to same user.
- With post command I see an error "username exists" , as user name "sample" already exists .
What should be done if i have to add/append a new database along with role for an
existing user with an existing db and role ,please provide suggestions .
That is a MongoDB question, specifically the api: https://api.mongodb.com/