cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Change settings via API using powershell

Mario_DT
Visitor

Hello Community,

I am trying to set the monitoring mode for a specific host using API with powershell. With cURL was very easy, with Powershell i am struggling. I have created the script below, but i get the following error:

Invoke-RestMethod : {"error":{"code":403,"message":"Token is missing required scope. Use one of: settings.write (Write
settings)"}}

Unfortunately, I didn't manage to find an example with a POST using Powershell.

Kind regards,

Mario

 

$Token = 'dt0c01.RM2ATDEG5773MBHGFGHDFGDFGHDGHKFDGHFDGHFDGFDGFDGHDFKDGHDHGDFGDGDGHDGHDG'

$Body = @{
schemaId = 'builtin:host.monitoring.mode'
schemaVersion = '1.3'
scope = 'HOST-5F30AHGJHKGHJGHK'
value = @{
monitoringMode = 'FULL_STACK'
}
}


$Params = @{
Uri = 'https://{environmentid}.live.dynatrace.com/api/v2/settings/objects'
Method = 'POST'
Body = '[' + ($Body | ConvertTo-Json) + ']'
Headers = @{
"Content-Type" = 'application/json; charset=utf-8'
"Authorization" = 'Api-Token' + ' ' + $Token
}

}

Invoke-RestMethod @Params

2 REPLIES 2

cbaldi
Contributor

Your answer is in the error message. The token you are using needs to have the "settings.write" scope. You need to get another token with that scope.

You can also omit the '+' in your Authorization header for easier reading and just do

"Authorization" = "Api-Token $Token"

Hi, Thanks for your reply and for mentioning about the API token. I used a GET example for validating the authentication part, then I forgot to amend the token when i was testing the POST scenario. Now I am able to execute the script, but I had to use my original setting, it didn't work with just 'Api-Token $Token'.

"Authorization" = 'Api-Token' + ' ' + $Token

Featured Posts