Skip to content

Latest commit

 

History

History
165 lines (134 loc) · 3.7 KB

createArray.md

File metadata and controls

165 lines (134 loc) · 3.7 KB
description ms.date ms.topic title
Reference for the 'createArray' DSC configuration document function
04/09/2024
reference
createArray

createArray

Synopsis

Returns an array of values from input.

Syntax

createArray(<inputValue>)

Description

The createArray() function returns an array of values from the input values. You can use this function to create arrays of any type. The input values must be of the same type - numbers, strings, objects, or arrays. When the input values are objects or arrays, they do not need be objects with the same properties or arrays of the same type. When the input values are arrays, the function returns an array of arrays.

Examples

Example 1 - Create an array of integers

example synopsis

# createArray.example.1.dsc.config.yaml
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
resources:
- name: Echo array of integers
  type: Test/Echo
  properties:
    output: "[createArray(1, 3, 5)]"
dsc config get --document createArray.example.1.dsc.config.yaml config get
results:
- name: Echo array of integers
  type: Test/Echo
  result:
    actualState:
      output:
      - 1
      - 3
      - 5
messages: []
hadErrors: false

Example 2 - Create an array of arrays

This configuration returns an array where the items in the array are also arrays. The first sub-array contains only integers. The second sub-array contains only strings.

# createArray.example.2.dsc.config.yaml
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
resources:
- name: Create array of arrays
  type: Test/Echo
  properties:
    output: "[createArray(createArray(1,3,5), createArray('a', 'b', 'c'))]"
dsc config get --document createArray.example.2.dsc.config.yaml
results:
- name: Create array of arrays
  type: Test/Echo
  result:
    actualState:
      output:
      - - 1
        - 3
        - 5
      - - a
        - b
        - c
messages: []
hadErrors: false

Example 3 - Create a flattened array of strings

This configuration uses the concat() function to concatenate two newly created arrays of strings. It uses YAML's folded multiline string syntax to make the function more readable.

# createArray.example.3.dsc.config.yaml
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
resources:
- name: Echo flattened array
  type: Test/Echo
  properties:
    output: >-
      [concat(
        createArray('a', 'b', 'c'),
        createArray('d', 'e', 'f')
      )]
dsc config get --document createArray.example.3.dsc.config.yaml
results:
- name: Echo flattened array
  type: Test/Echo
  result:
    actualState:
      output:
      - a
      - b
      - c
      - d
      - e
      - f
messages: []
hadErrors: false

Parameters

inputValue

The createArray() function expects zero or more input values of the same type. Separate each value with a comma. If the type of any input value is different from the first value, DSC returns an error for the function.

Type:         [integer, string, number, object, array]
Required:     false
MinimumCount: 0
MaximumCount: 18446744073709551615

Output

The createArray() function returns an array of values. When the input values are arrays, the returned value is an array of arrays, not a flattened array of the input values. You can return a flattened array of string arrays with the concat() function, as in example 3.

Type: array