|
| 1 | +--- |
| 2 | +description: Reference for the 'contains' DSC configuration document function |
| 3 | +ms.date: 08/08/2025 |
| 4 | +ms.topic: reference |
| 5 | +title: contains |
| 6 | +--- |
| 7 | + |
| 8 | +# contains |
| 9 | + |
| 10 | +## Synopsis |
| 11 | + |
| 12 | +Checks whether a collection contains a specific value or whether a string |
| 13 | +contains a substring. |
| 14 | + |
| 15 | +## Syntax |
| 16 | + |
| 17 | +```Syntax |
| 18 | +contains(<collection>, <value>) |
| 19 | +``` |
| 20 | + |
| 21 | +## Description |
| 22 | + |
| 23 | +The `contains()` function checks whether a collection (array, object, or |
| 24 | +string) contains a specific value, returning `true` if it does and `false` |
| 25 | +otherwise. For arrays, it checks if the value exists as an element. For |
| 26 | +objects, it checks if the value exists as a property key or value. For |
| 27 | +strings, it checks if the value exists as a substring. |
| 28 | + |
| 29 | +The function accepts string and number values for the search parameter when |
| 30 | +used with arrays, objects, or strings. |
| 31 | + |
| 32 | +## Examples |
| 33 | + |
| 34 | +### Example 1 - Check array for values |
| 35 | + |
| 36 | +The following example shows how to check if an array contains specific values. |
| 37 | + |
| 38 | +```yaml |
| 39 | +# contains.example.1.dsc.config.yaml |
| 40 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 41 | +parameters: |
| 42 | + myArray: |
| 43 | + type: array |
| 44 | + defaultValue: |
| 45 | + - apple |
| 46 | + - banana |
| 47 | + - 42 |
| 48 | + - true |
| 49 | +resources: |
| 50 | +- name: Check array contents |
| 51 | + type: Microsoft.DSC.Debug/Echo |
| 52 | + properties: |
| 53 | + output: |
| 54 | + hasApple: "[contains(parameters('myArray'), 'apple')]" |
| 55 | + hasOrange: "[contains(parameters('myArray'), 'orange')]" |
| 56 | + hasNumber42: "[contains(parameters('myArray'), 42)]" |
| 57 | + hasNumber99: "[contains(parameters('myArray'), 99)]" |
| 58 | +``` |
| 59 | +
|
| 60 | +```bash |
| 61 | +dsc config get --file contains.example.1.dsc.config.yaml |
| 62 | +``` |
| 63 | + |
| 64 | +```yaml |
| 65 | +results: |
| 66 | +- name: Check array contents |
| 67 | + type: Microsoft.DSC.Debug/Echo |
| 68 | + result: |
| 69 | + actualState: |
| 70 | + output: |
| 71 | + hasApple: true |
| 72 | + hasOrange: false |
| 73 | + hasNumber42: true |
| 74 | + hasNumber99: false |
| 75 | +messages: [] |
| 76 | +hadErrors: false |
| 77 | +``` |
| 78 | +
|
| 79 | +### Example 2 - Check object for keys and values |
| 80 | +
|
| 81 | +The following example shows how to check if an object contains specific keys |
| 82 | +or values. |
| 83 | +
|
| 84 | +```yaml |
| 85 | +# contains.example.2.dsc.config.yaml |
| 86 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 87 | +parameters: |
| 88 | + myObject: |
| 89 | + type: object |
| 90 | + defaultValue: |
| 91 | + name: John |
| 92 | + age: 30 |
| 93 | + city: Seattle |
| 94 | +resources: |
| 95 | +- name: Check object contents |
| 96 | + type: Microsoft.DSC.Debug/Echo |
| 97 | + properties: |
| 98 | + output: |
| 99 | + hasNameKey: "[contains(parameters('myObject'), 'name')]" |
| 100 | + hasEmailKey: "[contains(parameters('myObject'), 'email')]" |
| 101 | + hasSeattleValue: "[contains(parameters('myObject').city, 'Seattle')]" |
| 102 | + hasAge30Value: "[contains(parameters('myObject').age, 30)]" |
| 103 | +``` |
| 104 | +
|
| 105 | +```bash |
| 106 | +dsc config get --file contains.example.2.dsc.config.yaml |
| 107 | +``` |
| 108 | + |
| 109 | +```yaml |
| 110 | +results: |
| 111 | +- name: Check object contents |
| 112 | + type: Microsoft.DSC.Debug/Echo |
| 113 | + result: |
| 114 | + actualState: |
| 115 | + output: |
| 116 | + hasNameKey: true |
| 117 | + hasEmailKey: false |
| 118 | + hasSeattleValue: true |
| 119 | + hasAge30Value: true |
| 120 | +messages: [] |
| 121 | +hadErrors: false |
| 122 | +``` |
| 123 | +
|
| 124 | +### Example 3 - Check string for substrings |
| 125 | +
|
| 126 | +The following example shows how to check if a string contains specific |
| 127 | +substrings. |
| 128 | +
|
| 129 | +```yaml |
| 130 | +# contains.example.3.dsc.config.yaml |
| 131 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 132 | +parameters: |
| 133 | + myString: |
| 134 | + type: string |
| 135 | + defaultValue: "Hello DSC 123" |
| 136 | +resources: |
| 137 | +- name: Check string contents |
| 138 | + type: Microsoft.DSC.Debug/Echo |
| 139 | + properties: |
| 140 | + output: |
| 141 | + hasHello: "[contains(parameters('myString'), 'Hello')]" |
| 142 | + hasDSC: "[contains(parameters('myString'), 'DSC')]" |
| 143 | + hasNumber: "[contains(parameters('myString'), '123')]" |
| 144 | + hasXYZ: "[contains(parameters('myString'), 'XYZ')]" |
| 145 | +``` |
| 146 | +
|
| 147 | +```bash |
| 148 | +dsc config get --file contains.example.3.dsc.config.yaml |
| 149 | +``` |
| 150 | + |
| 151 | +```yaml |
| 152 | +results: |
| 153 | +- name: Check string contents |
| 154 | + type: Microsoft.DSC.Debug/Echo |
| 155 | + result: |
| 156 | + actualState: |
| 157 | + output: |
| 158 | + hasHello: true |
| 159 | + hasDSC: true |
| 160 | + hasNumber: true |
| 161 | + hasXYZ: false |
| 162 | +messages: [] |
| 163 | +hadErrors: false |
| 164 | +``` |
| 165 | +
|
| 166 | +## Parameters |
| 167 | +
|
| 168 | +### collection |
| 169 | +
|
| 170 | +The collection to search in (array, object, or string). |
| 171 | +
|
| 172 | +```yaml |
| 173 | +Type: [array, object, string] |
| 174 | +Required: true |
| 175 | +``` |
| 176 | +
|
| 177 | +### value |
| 178 | +
|
| 179 | +The value to search for. Must be a string or number. |
| 180 | +
|
| 181 | +```yaml |
| 182 | +Type: [string, number] |
| 183 | +Required: true |
| 184 | +``` |
| 185 | +
|
| 186 | +The `contains()` function expects exactly two input values. The first |
| 187 | +parameter is the collection to search, and the second is the value to find. |
| 188 | +Complex objects and arrays cannot be used as search values. |
| 189 | + |
| 190 | +## Output |
| 191 | + |
| 192 | +The `contains()` function returns `true` if the collection contains the |
| 193 | +specified value and `false` otherwise. |
| 194 | + |
| 195 | +```yaml |
| 196 | +Type: bool |
| 197 | +``` |
| 198 | + |
| 199 | +<!-- Link reference definitions --> |
0 commit comments