# Add loop task to enable or disable host monitoring

**URL:** <https://community.dynatrace.com/t/add-loop-task-to-enable-or-disable-host-monitoring/46197>\
**Category:** Automations\
**Tags:** workflows\
**Created:** [June 19, 2024, 12:06am UTC](https://community.dynatrace.com/t/add-loop-task-to-enable-or-disable-host-monitoring/46197 "2024-06-19T00:06:26Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Smalhotra89](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Smalhotra89](https://community.dynatrace.com/u/Smalhotra89)\
**Post date:** [June 19, 2024, 12:06am UTC](https://community.dynatrace.com/t/add-loop-task-to-enable-or-disable-host-monitoring/46197/1 "2024-06-19T00:06:26Z")

</div>

Hi team,

I want to use loop task for a list of hosts to enable or disable monitoring in Dynatrace. I would like to have multiple hosts to be executed with single workflow for http\_request.

```markup
[
{
"externalId": "string",
"schemaId": "builtin:host.monitoring",
"schemaVersion":"1.4",
"scope": "HOST-7AA30C2D50D93E12",
"value": {
"enabled": false
}
}
]

```

---

<div class="post-metadata">

**Author:** ![sinisa\_zubic](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/sinisa_zubic/32/32498_2.png) [@sinisa\_zubic](https://community.dynatrace.com/u/sinisa_zubic)\
**Post date:** [June 19, 2024, 4:41pm UTC](https://community.dynatrace.com/t/add-loop-task-to-enable-or-disable-host-monitoring/46197/2 "2024-06-19T16:41:32Z")

</div>

Hi&nbsp;[@Smalhotra89](https://community.dynatrace.com/user/77190)

Yes this is possible with the loop task. Before you start you need to generate an “Access Token” with the scopes&nbsp;“Read settings” and “Write settings”.

Then you can start creating the workflow. First you need to fetch all settings objects for the list of hosts you want to disable or enable with a http request task. The list of hosts is put in the URL and is comma separated. In the header section you add the Autorization details,&nbsp;please have a look at following screenshot

 ![sinisa_zubic_0-1718814871834.png](https://global.discourse-cdn.com/dynatrace/original/2X/9/9d06c7ce2f51e236d276e16dc9fa5baeef6925dd.png)

in the subsequent http request task you need to configure the loop task like this

 ![sinisa_zubic_1-1718814949557.png](https://global.discourse-cdn.com/dynatrace/original/2X/b/b46dab59ee81999376054894799a7c08edc8a8b6.png)

The input for the tasks should be that

 ![sinisa_zubic_2-1718815087103.png](https://global.discourse-cdn.com/dynatrace/original/2X/5/570d2ac415aec3a731aecfea0ce6f460e49ca9b8.png)

For this approach you have to create the token and manage it yourself.

But you could also do the same thing with a javascript task with custom javascript code, the benefit there would be that you don’t need to create a token for changing the settings. Since the used sdks underneath managed this for you.

this is the snippet for the javascript task

```markup
// optional import of sdk modules
import { execution } from '@dynatrace-sdk/automation-utils';
import { actionExecution } from "@dynatrace-sdk/automation-utils";

import { SettingsObject, settingsObjectsClient } from "@dynatrace-sdk/client-classic-environment-v2";

export default async function ({ execution_id, action_execution_id }) {
  // your code goes here
  // e.g. get the current execution
  const ex = await execution(execution_id);
  const actionEx = await actionExecution(action_execution_id);
  const scopes = actionEx.loopItem["host"];
  //console.log(scopes);
  const settingsObjects = await settingsObjectsClient.getSettingsObjects({
      schemaIds: "builtin:host.monitoring",
      scopes,
      fields: "objectId,value,summary,searchSummary,created,modifid,createdBy,modifiedBy,author,updateToken,scope,modificationInfo,schemaId,schemaVersion,externalId"
    });
  //console.log(settingsObjects);
  if (settingsObjects.items.length > 0){
    const settingsObject = (settingsObjects.items[0]);
    const settingsObjectResponse = await settingsObjectsClient.putSettingsObjectByObjectId({
        objectId: settingsObject.objectId,
        body: {
          updateToken: settingsObject.updateToken,
          value: {
            "enabled": false
          }
        }
      });
    return { settingsObjectResponse };
  }
}

```

 ![sinisa_zubic_3-1718815258779.png](https://global.discourse-cdn.com/dynatrace/original/3X/9/7/97d357b6b6620ea5e83e16606900685325f52cff.png)

Best,  
Sini

---

<div class="post-metadata">

**Author:** ![Smalhotra89](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Smalhotra89](https://community.dynatrace.com/u/Smalhotra89)\
**Post date:** [June 20, 2024, 3:44am UTC](https://community.dynatrace.com/t/add-loop-task-to-enable-or-disable-host-monitoring/46197/3 "2024-06-20T03:44:17Z")

</div>

Thanks Sini,&nbsp;javascript task works well.

I was unable to use the http request with loop task as it was unable to read allobjects.json.item in loop. I am not sure if I did something wrong but the first task to get all objects task ran through fine.

---

<div class="post-metadata">

**Author:** ![vs185250](https://avatars.discourse-cdn.com/v4/letter/v/aca169/32.png) [@vs185250](https://community.dynatrace.com/u/vs185250)\
**Post date:** [August 31, 2024, 1:48am UTC](https://community.dynatrace.com/t/add-loop-task-to-enable-or-disable-host-monitoring/46197/4 "2024-08-31T01:48:51Z")

</div>

How do I iterate through all values from a fetch query using javascript?

 ![vs185250_0-1725068819983.png](https://global.discourse-cdn.com/dynatrace/original/2X/9/99f7bded7a1c98c89b1d471861d04546505468e7.png)

 ![vs185250_1-1725068883642.png](https://global.discourse-cdn.com/dynatrace/original/3X/c/8/c87c2e9df55b3711ddb0ad4f26571f6ebb89656c.png)

 ![vs185250_2-1725068924616.png](https://global.discourse-cdn.com/dynatrace/original/3X/d/0/d0cc95db1dbfc3048d18204f5d587362bca46bfd.png)

---

<div class="post-metadata">

**Author:** ![vs185250](https://avatars.discourse-cdn.com/v4/letter/v/aca169/32.png) [@vs185250](https://community.dynatrace.com/u/vs185250)\
**Post date:** [August 31, 2024, 1:49am UTC](https://community.dynatrace.com/t/add-loop-task-to-enable-or-disable-host-monitoring/46197/5 "2024-08-31T01:49:44Z")

</div>

here is the loop item

 ![vs185250_3-1725068975651.png](https://global.discourse-cdn.com/dynatrace/original/1X/d9b1ffdb51f6ed8b93eef27f6e728c0cbec9787c.png)

---

<div class="post-metadata">

**Author:** ![vs185250](https://avatars.discourse-cdn.com/v4/letter/v/aca169/32.png) [@vs185250](https://community.dynatrace.com/u/vs185250)\
**Post date:** [August 31, 2024, 1:53am UTC](https://community.dynatrace.com/t/add-loop-task-to-enable-or-disable-host-monitoring/46197/6 "2024-08-31T01:53:25Z")

</div>

nvm. figured it out

 ![vs185250_4-1725069193027.png](https://global.discourse-cdn.com/dynatrace/original/3X/4/5/457e720cb692b35586afcc7cfce1eb527b108bfa.png)

---

<div class="post-metadata">

**Author:** ![vs185250](https://avatars.discourse-cdn.com/v4/letter/v/aca169/32.png) [@vs185250](https://community.dynatrace.com/u/vs185250)\
**Post date:** [August 31, 2024, 2:15am UTC](https://community.dynatrace.com/t/add-loop-task-to-enable-or-disable-host-monitoring/46197/7 "2024-08-31T02:15:59Z")

</div>

this worked for me and disabled monitoring on all hosts

 ![vs185250_5-1725070526855.png](https://global.discourse-cdn.com/dynatrace/original/3X/1/7/17bec901213b44ce7dff64c6f19ed9401464e1c8.png)

---

<div class="post-metadata">

**Author:** ![Smalhotra89](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Smalhotra89](https://community.dynatrace.com/u/Smalhotra89)\
**Post date:** [August 31, 2024, 2:20am UTC](https://community.dynatrace.com/t/add-loop-task-to-enable-or-disable-host-monitoring/46197/8 "2024-08-31T02:20:32Z")

</div>

Thank You, I made it work for all tagged hosts.

---

<div class="post-metadata">

**Author:** ![DineshP](https://avatars.discourse-cdn.com/v4/letter/d/9fc29f/32.png) [@DineshP](https://community.dynatrace.com/u/DineshP)\
**Post date:** [February 15, 2025, 7:00am UTC](https://community.dynatrace.com/t/add-loop-task-to-enable-or-disable-host-monitoring/46197/9 "2025-02-15T07:00:03Z")

</div>

Hi&nbsp;&nbsp;[@vs185250](https://community.dynatrace.com/user/47021)&nbsp;,[@Smalhotra89](https://community.dynatrace.com/user/77190)

I hope you guys are doing well.

I have replicated the above use case in my environment and it is not getting executed and requesting your help here.

Do I have to pass any bearer token ?

Thanks&nbsp;

 ![Fetch Query.png](https://global.discourse-cdn.com/dynatrace/original/3X/6/4/6484c66f8db76dcf37b4787b301da80a6f2a0b3c.png)  
 ![js_loop task.png](https://global.discourse-cdn.com/dynatrace/original/2X/6/68533b0b415c4d11c146de9676213082563e2b66.png)  
 ![Bearer Token.png](https://global.discourse-cdn.com/dynatrace/original/3X/5/5/55918c30e4257abe98037e1e2be2dff2262b44e3.png)  
 ![Error.png](https://global.discourse-cdn.com/dynatrace/original/3X/b/2/b2c3cd5ad9f457f3c3cbc3e77f53f239273cb341.png)  
 ![JS_Code.png](https://global.discourse-cdn.com/dynatrace/original/3X/b/c/bcc3453c16c05dc8946aa0b36406e8b892a84243.png)

---

<div class="post-metadata">

**Author:** ![erh\_inetum](https://avatars.discourse-cdn.com/v4/letter/e/48db29/32.png) [@erh\_inetum](https://community.dynatrace.com/u/erh_inetum)\
**Post date:** [February 17, 2025, 6:51am UTC](https://community.dynatrace.com/t/add-loop-task-to-enable-or-disable-host-monitoring/46197/10 "2025-02-17T06:51:11Z")

</div>

Hi&nbsp;[@DineshP](https://community.dynatrace.com/user/86799),

In this case you have to use API token instead Bearer Token.

The APIs using in this workflow requeries API token.

Hope it helps.

Regards,

Elena.

---

<div class="post-metadata">

**Author:** ![DineshP](https://avatars.discourse-cdn.com/v4/letter/d/9fc29f/32.png) [@DineshP](https://community.dynatrace.com/u/DineshP)\
**Post date:** [February 17, 2025, 6:58am UTC](https://community.dynatrace.com/t/add-loop-task-to-enable-or-disable-host-monitoring/46197/11 "2025-02-17T06:58:37Z")

</div>

Hi [@erh\_inetum](https://community.dynatrace.com/user/40722)&nbsp;,

Thanks for the assistance, It would be great if you can help with some steps to proceed further to achieve the same.

Regards

DP

---

<div class="post-metadata">

**Author:** ![erh\_inetum](https://avatars.discourse-cdn.com/v4/letter/e/48db29/32.png) [@erh\_inetum](https://community.dynatrace.com/u/erh_inetum)\
**Post date:** [February 17, 2025, 7:30am UTC](https://community.dynatrace.com/t/add-loop-task-to-enable-or-disable-host-monitoring/46197/12 "2025-02-17T07:30:47Z")

</div>

Hi,

You have to use this header:

headers: {  
‘Accept’: ‘application/json; charset=utf-8’,  
‘Authorization’: ‘Api-Token dt0c01.XXXXXXXXXXXXXXXXX’  
}

like in this screenshot

 ![erh_inetum_0-1739777398740.png](https://global.discourse-cdn.com/dynatrace/original/2X/8/88cd1aee535ac41e8e2e1bea9f1e4f70e44b9745.png)

sent by [@sinisa\_zubic](https://community.dynatrace.com/user/38283)&nbsp;in the first reply of this post.

Hope it helps.

Regards,

Elena.

---

<div class="post-metadata">

**Author:** ![CTull](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@CTull](https://community.dynatrace.com/u/CTull)\
**Post date:** [February 2, 2026, 7:20pm UTC](https://community.dynatrace.com/t/add-loop-task-to-enable-or-disable-host-monitoring/46197/13 "2026-02-02T19:20:54Z")

</div>

I have the same ask, can someone upload a workflow that uses javascript or api method to enable or disable hosts that belong to a certain host group?

---

<div class="post-metadata">

**Author:** ![CTull](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@CTull](https://community.dynatrace.com/u/CTull)\
**Post date:** [February 2, 2026, 7:58pm UTC](https://community.dynatrace.com/t/add-loop-task-to-enable-or-disable-host-monitoring/46197/14 "2026-02-02T19:58:42Z")

</div>

Batch file for now:

@echo off  
setlocal ENABLEDELAYEDEXPANSION

REM ==== Config ====  
set DT\_ENV\_URL=https://{environmentid}.live.dynatrace.com  
set DT\_API\_TOKEN=dt0c01.YOURLONGA$$TOKENNAME  
if “%DT\_API\_TOKEN%”==“” (  
echo ERROR: Please set DT\_API\_TOKEN environment variable.  
exit /b 1  
)

set ENDPOINT=%DT\_ENV\_URL%/api/v2/settings/objects

REM ==== Host list ====

REM ++++Exported List from Dynatrace Infrastructure and Operations App++++

set HOSTS= ^  
HOST-xxxaaa ^  
HOST-xxxfffff ^  
HOST-xxxddd ^  
HOST-xxx

for %%H in (%HOSTS%) do (&nbsp; &nbsp; echo Posting for %%H …&nbsp; &nbsp; set PAYLOAD=[{““schemaId””:““builtin:host.monitoring””,““schemaVersion””:““1.4"”,”“scope”“:”“%%H”“,”“value”“:{”“enabled”":false}}]&nbsp; &nbsp; curl -s -X POST ^&nbsp; &nbsp; &nbsp; &nbsp; -H “Accept: application/json; charset=utf-8” ^&nbsp; &nbsp; &nbsp; &nbsp; -H “Content-Type: application/json; charset=utf-8” ^&nbsp; &nbsp; &nbsp; &nbsp; -H “Authorization: Api-Token %DT\_API\_TOKEN%” ^&nbsp; &nbsp; &nbsp; &nbsp; -d “!PAYLOAD!” ^&nbsp; &nbsp; &nbsp; &nbsp; “%ENDPOINT%”&nbsp; &nbsp; echo.)endlocal
