|
| 1 | +--- |
| 2 | +description: Reference for the 'array' DSC configuration document function |
| 3 | +ms.date: 08/12/2025 |
| 4 | +ms.topic: reference |
| 5 | +title: array |
| 6 | +--- |
| 7 | + |
| 8 | +# array |
| 9 | + |
| 10 | +## Synopsis |
| 11 | + |
| 12 | +Creates an array from the given elements (strings, numbers, arrays, or objects). |
| 13 | + |
| 14 | +## Syntax |
| 15 | + |
| 16 | +```Syntax |
| 17 | +array(<element1>, <element2>, ...) |
| 18 | +``` |
| 19 | + |
| 20 | +## Description |
| 21 | + |
| 22 | +The `array()` function creates an array from the provided arguments, allowing |
| 23 | +mixed data types within the same array. Unlike `createArray()` which requires |
| 24 | +all elements to be the same type, `array()` accepts any combination of strings, |
| 25 | +numbers, arrays, and objects as arguments. |
| 26 | + |
| 27 | +This function is useful when you need to combine different types of data into a |
| 28 | +single collection, such as mixing configuration values, computed results, and |
| 29 | +structured metadata in deployment scenarios. |
| 30 | + |
| 31 | +## Examples |
| 32 | + |
| 33 | +### Example 1 - Build a deployment plan |
| 34 | + |
| 35 | +This example demonstrates combining different data types to create a comprehensive |
| 36 | +deployment configuration. The array contains an existing server list, a numeric |
| 37 | +batch size, and a configuration object with deployment strategy details. |
| 38 | + |
| 39 | +```yaml |
| 40 | +# array.example.1.dsc.config.yaml |
| 41 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 42 | +parameters: |
| 43 | + webServers: |
| 44 | + type: array |
| 45 | + defaultValue: |
| 46 | + - web01 |
| 47 | + - web02 |
| 48 | + batchSize: |
| 49 | + type: int |
| 50 | + defaultValue: 2 |
| 51 | +resources: |
| 52 | +- name: Deployment Plan |
| 53 | + type: Microsoft.DSC.Debug/Echo |
| 54 | + properties: |
| 55 | + output: "[array(parameters('webServers'), parameters('batchSize'), createObject('strategy', 'blue-green'))]" |
| 56 | +``` |
| 57 | +
|
| 58 | +```bash |
| 59 | +dsc config get --file array.example.1.dsc.config.yaml |
| 60 | +``` |
| 61 | + |
| 62 | +```yaml |
| 63 | +results: |
| 64 | +- name: Deployment Plan |
| 65 | + type: Microsoft.DSC.Debug/Echo |
| 66 | + result: |
| 67 | + actualState: |
| 68 | + output: |
| 69 | + - - web01 |
| 70 | + - web02 |
| 71 | + - 2 |
| 72 | + - strategy: blue-green |
| 73 | +messages: [] |
| 74 | +hadErrors: false |
| 75 | +``` |
| 76 | +
|
| 77 | +### Example 2 - Compose mixed telemetry payload parts |
| 78 | +
|
| 79 | +This example shows how to construct a telemetry payload by combining a string |
| 80 | +identifier, structured metadata object, and numeric status code into a single |
| 81 | +array for logging or monitoring systems. |
| 82 | +
|
| 83 | +```yaml |
| 84 | +# array.example.2.dsc.config.yaml |
| 85 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 86 | +parameters: |
| 87 | + correlationId: |
| 88 | + type: string |
| 89 | + defaultValue: ABC123 |
| 90 | +resources: |
| 91 | +- name: Telemetry Payload |
| 92 | + type: Microsoft.DSC.Debug/Echo |
| 93 | + properties: |
| 94 | + output: |
| 95 | + payload: "[array(parameters('correlationId'), createObject('severity', 'info'), 200)]" |
| 96 | +``` |
| 97 | +
|
| 98 | +```bash |
| 99 | +dsc config get --file array.example.2.dsc.config.yaml |
| 100 | +``` |
| 101 | + |
| 102 | +```yaml |
| 103 | +results: |
| 104 | +- name: Telemetry Payload |
| 105 | + type: Microsoft.DSC.Debug/Echo |
| 106 | + result: |
| 107 | + actualState: |
| 108 | + output: |
| 109 | + payload: |
| 110 | + - ABC123 |
| 111 | + - severity: info |
| 112 | + - 200 |
| 113 | +messages: [] |
| 114 | +hadErrors: false |
| 115 | +``` |
| 116 | +
|
| 117 | +### Example 3 - Combine generated and literal collections |
| 118 | +
|
| 119 | +This example demonstrates nesting arrays and objects within a parent array, |
| 120 | +showing how `array()` can combine results from other DSC functions like |
| 121 | +`createArray()` and `createObject()` with literal values. |
| 122 | + |
| 123 | +```yaml |
| 124 | +# array.example.3.dsc.config.yaml |
| 125 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 126 | +resources: |
| 127 | +- name: Combined Collections |
| 128 | + type: Microsoft.DSC.Debug/Echo |
| 129 | + properties: |
| 130 | + output: |
| 131 | + combined: "[array(createArray('a','b'), array(1,2), createObject('k','v'))]" |
| 132 | +``` |
| 133 | + |
| 134 | +```bash |
| 135 | +dsc config get --file array.example.3.dsc.config.yaml |
| 136 | +``` |
| 137 | + |
| 138 | +```yaml |
| 139 | +results: |
| 140 | +- name: Combined Collections |
| 141 | + type: Microsoft.DSC.Debug/Echo |
| 142 | + result: |
| 143 | + actualState: |
| 144 | + output: |
| 145 | + combined: |
| 146 | + - - a |
| 147 | + - b |
| 148 | + - - 1 |
| 149 | + - 2 |
| 150 | + - k: v |
| 151 | +messages: [] |
| 152 | +hadErrors: false |
| 153 | +``` |
| 154 | + |
| 155 | +## Parameters |
| 156 | + |
| 157 | +### element1, element2, ... |
| 158 | + |
| 159 | +The elements to include in the array. |
| 160 | + |
| 161 | +```yaml |
| 162 | +Type: string, number, array, or object |
| 163 | +Required: false |
| 164 | +Multiplicity: 0 or more |
| 165 | +``` |
| 166 | + |
| 167 | +Each element provided as an argument will be added to the resulting array in the |
| 168 | +order specified. All elements must be one of the four accepted types: string, |
| 169 | +number (integer), array, or object. Boolean and null values are not supported. |
| 170 | + |
| 171 | +## Output |
| 172 | + |
| 173 | +Returns a new array containing the provided elements in order. |
| 174 | + |
| 175 | +```yaml |
| 176 | +Type: array |
| 177 | +``` |
| 178 | + |
| 179 | +## Related functions |
| 180 | + |
| 181 | +- [`createArray()`][00] - Creates a homogeneous array (all elements same type) |
| 182 | +- [`createObject()`][01] - Builds an object from key/value pairs |
| 183 | +- [`first()`][02] - Gets the first element from an array |
| 184 | +- [`indexOf()`][03] - Finds the index of an item in an array |
| 185 | + |
| 186 | +<!-- Link reference definitions --> |
| 187 | +[00]: ./createArray.md |
| 188 | +[01]: ./createObject.md |
| 189 | +[02]: ./first.md |
| 190 | +[03]: ./indexOf.md |
0 commit comments