|
| 1 | +--- |
| 2 | +description: Reference for the 'intersection' DSC configuration document function |
| 3 | +ms.date: 09/26/2025 |
| 4 | +ms.topic: reference |
| 5 | +title: intersection |
| 6 | +--- |
| 7 | + |
| 8 | +## Synopsis |
| 9 | + |
| 10 | +Returns a single array or object with the common elements from the parameters. |
| 11 | + |
| 12 | +## Syntax |
| 13 | + |
| 14 | +```Syntax |
| 15 | +intersection(value1, value2, ...) |
| 16 | +``` |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +The `intersection()` function takes two or more arrays or objects and returns |
| 21 | +only the elements that exist in all of them. For arrays, it returns elements |
| 22 | +that appear in every array. For objects, it returns key-value pairs where both |
| 23 | +the key and value match across all objects. |
| 24 | + |
| 25 | +All parameters must be the same type - either all arrays or all objects. |
| 26 | +Results are deduplicated, meaning each element appears only once in the output. |
| 27 | + |
| 28 | +Supported types: |
| 29 | + |
| 30 | +- Arrays (elements compared by value) |
| 31 | +- Objects (key-value pairs compared by deep equality) |
| 32 | + |
| 33 | +## Examples |
| 34 | + |
| 35 | +### Example 1 - Find common security groups across environments (arrays) |
| 36 | + |
| 37 | +Use `intersection()` to identify security groups that are consistently applied |
| 38 | +across development, staging, and production environments. This helps ensure |
| 39 | +security policies are uniformly enforced. This example uses |
| 40 | +[`createArray()`][01] to build the security group lists. |
| 41 | + |
| 42 | +```yaml |
| 43 | +# intersection.example.1.dsc.config.yaml |
| 44 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 45 | +resources: |
| 46 | +- name: Common Security Groups |
| 47 | + type: Microsoft.DSC.Debug/Echo |
| 48 | + properties: |
| 49 | + output: |
| 50 | + commonGroups: "[intersection(createArray('admin-access', 'monitoring', 'backup'), createArray('monitoring', 'backup', 'web-access'), createArray('backup', 'monitoring', 'database'))]" |
| 51 | + twoEnvCommon: "[intersection(createArray('admin-access', 'monitoring'), createArray('monitoring', 'audit-log'))]" |
| 52 | +``` |
| 53 | +
|
| 54 | +```bash |
| 55 | +dsc config get --file intersection.example.1.dsc.config.yaml |
| 56 | +``` |
| 57 | + |
| 58 | +```yaml |
| 59 | +results: |
| 60 | +- name: Common Security Groups |
| 61 | + type: Microsoft.DSC.Debug/Echo |
| 62 | + result: |
| 63 | + actualState: |
| 64 | + output: |
| 65 | + commonGroups: |
| 66 | + - monitoring |
| 67 | + - backup |
| 68 | + twoEnvCommon: |
| 69 | + - monitoring |
| 70 | +messages: [] |
| 71 | +hadErrors: false |
| 72 | +``` |
| 73 | +
|
| 74 | +### Example 2 - Identify shared configuration properties (objects) |
| 75 | +
|
| 76 | +Find configuration settings that are identical across multiple service |
| 77 | +instances. This is useful for extracting common configuration into shared |
| 78 | +templates or validating consistency. This example uses [`createObject()`][02] |
| 79 | +to build configuration objects. |
| 80 | + |
| 81 | +```yaml |
| 82 | +# intersection.example.2.dsc.config.yaml |
| 83 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 84 | +resources: |
| 85 | +- name: Shared Config Properties |
| 86 | + type: Microsoft.DSC.Debug/Echo |
| 87 | + properties: |
| 88 | + output: |
| 89 | + commonSettings: "[intersection(createObject('timeout', 30, 'retries', 3, 'region', 'us-east'), createObject('retries', 3, 'ssl', true, 'region', 'us-east'), createObject('region', 'us-east', 'retries', 3, 'logging', 'info'))]" |
| 90 | +``` |
| 91 | + |
| 92 | +```bash |
| 93 | +dsc config get --file intersection.example.2.dsc.config.yaml |
| 94 | +``` |
| 95 | + |
| 96 | +```yaml |
| 97 | +results: |
| 98 | +- name: Shared Config Properties |
| 99 | + type: Microsoft.DSC.Debug/Echo |
| 100 | + result: |
| 101 | + actualState: |
| 102 | + output: |
| 103 | + commonSettings: |
| 104 | + region: us-east |
| 105 | + retries: 3 |
| 106 | +messages: [] |
| 107 | +hadErrors: false |
| 108 | +``` |
| 109 | + |
| 110 | +### Example 3 - Find overlapping server capabilities (arrays with no matches) |
| 111 | + |
| 112 | +Sometimes environments have no common elements, which is valuable information |
| 113 | +for infrastructure planning. This example shows how `intersection()` handles |
| 114 | +arrays with no shared elements. |
| 115 | + |
| 116 | +```yaml |
| 117 | +# intersection.example.3.dsc.config.yaml |
| 118 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 119 | +resources: |
| 120 | +- name: Server Capabilities |
| 121 | + type: Microsoft.DSC.Debug/Echo |
| 122 | + properties: |
| 123 | + output: |
| 124 | + noOverlap: "[intersection(createArray('windows-iis', 'dotnet-core'), createArray('linux-apache', 'php', 'mysql'))]" |
| 125 | + someOverlap: "[intersection(createArray('docker', 'kubernetes', 'monitoring'), createArray('monitoring', 'logging', 'docker'))]" |
| 126 | +``` |
| 127 | + |
| 128 | +```bash |
| 129 | +dsc config get --file intersection.example.3.dsc.config.yaml |
| 130 | +``` |
| 131 | + |
| 132 | +```yaml |
| 133 | +results: |
| 134 | +- name: Server Capabilities |
| 135 | + type: Microsoft.DSC.Debug/Echo |
| 136 | + result: |
| 137 | + actualState: |
| 138 | + output: |
| 139 | + noOverlap: [] |
| 140 | + someOverlap: |
| 141 | + - docker |
| 142 | + - monitoring |
| 143 | +messages: [] |
| 144 | +hadErrors: false |
| 145 | +``` |
| 146 | + |
| 147 | +### Example 4 - Validate compliance across teams (objects) |
| 148 | + |
| 149 | +Use `intersection()` to verify that critical compliance settings are identical |
| 150 | +across different team configurations. Only settings with matching values will |
| 151 | +appear in the result. |
| 152 | + |
| 153 | +```yaml |
| 154 | +# intersection.example.4.dsc.config.yaml |
| 155 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 156 | +resources: |
| 157 | +- name: Compliance Check |
| 158 | + type: Microsoft.DSC.Debug/Echo |
| 159 | + properties: |
| 160 | + output: |
| 161 | + sharedCompliance: "[intersection(createObject('encryption', true, 'backup', 'daily', 'audit', true), createObject('audit', true, 'encryption', true, 'access', 'restricted'), createObject('encryption', true, 'audit', true, 'monitoring', 'enabled'))]" |
| 162 | +``` |
| 163 | + |
| 164 | +```bash |
| 165 | +dsc config get --file intersection.example.4.dsc.config.yaml |
| 166 | +``` |
| 167 | + |
| 168 | +```yaml |
| 169 | +results: |
| 170 | +- name: Compliance Check |
| 171 | + type: Microsoft.DSC.Debug/Echo |
| 172 | + result: |
| 173 | + actualState: |
| 174 | + output: |
| 175 | + sharedCompliance: |
| 176 | + audit: true |
| 177 | + encryption: true |
| 178 | +messages: [] |
| 179 | +hadErrors: false |
| 180 | +``` |
| 181 | + |
| 182 | +## Parameters |
| 183 | + |
| 184 | +### value1 |
| 185 | + |
| 186 | +The first array or object to compare. Required. |
| 187 | + |
| 188 | +```yaml |
| 189 | +Type: array | object |
| 190 | +Required: true |
| 191 | +Position: 1 |
| 192 | +``` |
| 193 | + |
| 194 | +### value2 |
| 195 | + |
| 196 | +The second array or object to compare. Must be the same type as value1. |
| 197 | +Required. |
| 198 | + |
| 199 | +```yaml |
| 200 | +Type: array | object |
| 201 | +Required: true |
| 202 | +Position: 2 |
| 203 | +``` |
| 204 | + |
| 205 | +### Additional values |
| 206 | + |
| 207 | +Additional arrays or objects to include in the intersection. All must be the |
| 208 | +same type. Optional. |
| 209 | + |
| 210 | +```yaml |
| 211 | +Type: array | object |
| 212 | +Required: false |
| 213 | +Position: 3+ |
| 214 | +``` |
| 215 | + |
| 216 | +## Output |
| 217 | + |
| 218 | +Returns an array or object containing only the common elements. The return type |
| 219 | +matches the input type. |
| 220 | + |
| 221 | +```yaml |
| 222 | +Type: array | object |
| 223 | +``` |
| 224 | + |
| 225 | +## Related functions |
| 226 | + |
| 227 | +- [`union()`][00] - Combines all elements from multiple arrays or objects |
| 228 | +- [`contains()`][03] - Checks for presence in arrays/objects/strings |
| 229 | +- [`createArray()`][01] - Creates an array from individual values |
| 230 | +- [`createObject()`][02] - Creates an object from key-value pairs |
| 231 | + |
| 232 | +<!-- Link reference definitions --> |
| 233 | +[00]: ./union.md |
| 234 | +[01]: ./createArray.md |
| 235 | +[02]: ./createObject.md |
| 236 | +[03]: ./contains.md |
0 commit comments