|
| 1 | +--- |
| 2 | +description: Reference for the 'format' DSC configuration document function |
| 3 | +ms.date: 02/28/2025 |
| 4 | +ms.topic: reference |
| 5 | +title: format |
| 6 | +--- |
| 7 | + |
| 8 | +# format |
| 9 | + |
| 10 | +## Synopsis |
| 11 | + |
| 12 | +Returns a formatted string that uses placeholders to insert values. |
| 13 | + |
| 14 | +## Syntax |
| 15 | + |
| 16 | +```Syntax |
| 17 | +format(<formatString>, <arg1>, <arg2>, ...) |
| 18 | +``` |
| 19 | + |
| 20 | +## Description |
| 21 | + |
| 22 | +The `format()` function returns a string that includes formatted values using a template and |
| 23 | +placeholders. Each placeholder in the template string is replaced with the corresponding argument |
| 24 | +value. Placeholders are specified with curly braces around the zero-based index of the argument. |
| 25 | + |
| 26 | +## Examples |
| 27 | + |
| 28 | +### Example 1 - Format a simple string |
| 29 | + |
| 30 | +The configuration formats a string with two placeholders. |
| 31 | + |
| 32 | +```yaml |
| 33 | +# format.example.1.dsc.config.yaml |
| 34 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 35 | +resources: |
| 36 | + - name: Echo formatted string |
| 37 | + type: Microsoft.DSC.Debug/Echo |
| 38 | + properties: |
| 39 | + output: "[format('Hello, {0}! Today is {1}.', 'World', 'Monday')]" |
| 40 | +``` |
| 41 | +
|
| 42 | +```bash |
| 43 | +dsc config get --file format.example.1.dsc.config.yaml |
| 44 | +``` |
| 45 | + |
| 46 | +```yaml |
| 47 | +results: |
| 48 | +- name: Echo formatted string |
| 49 | + type: Microsoft.DSC.Debug/Echo |
| 50 | + result: |
| 51 | + actualState: |
| 52 | + output: Hello, World! Today is Monday. |
| 53 | +messages: [] |
| 54 | +hadErrors: false |
| 55 | +``` |
| 56 | +
|
| 57 | +### Example 2 - Format a string with parameters |
| 58 | +
|
| 59 | +The configuration uses other functions within the `format()` function to build a dynamic message. |
| 60 | + |
| 61 | +```yaml |
| 62 | +# format.example.2.dsc.config.yaml |
| 63 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 64 | +parameters: |
| 65 | + username: |
| 66 | + type: string |
| 67 | + defaultValue: Mikey |
| 68 | + hour: |
| 69 | + type: string |
| 70 | + defaultValue: "09" |
| 71 | + minute: |
| 72 | + type: string |
| 73 | + defaultValue: "30" |
| 74 | +resources: |
| 75 | + - name: Echo dynamic formatted string |
| 76 | + type: Microsoft.DSC.Debug/Echo |
| 77 | + properties: |
| 78 | + output: "[format('Hello, {0}! The time is {1}:{2}.', parameters('username'), parameters('hour'), parameters('minute'))]" |
| 79 | +``` |
| 80 | + |
| 81 | +```bash |
| 82 | +dsc --file format.example.2.dsc.config.yaml config get |
| 83 | +``` |
| 84 | + |
| 85 | +```yaml |
| 86 | +results: |
| 87 | +- metadata: |
| 88 | + Microsoft.DSC: |
| 89 | + duration: PT0.0185508S |
| 90 | + name: Echo dynamic formatted string |
| 91 | + type: Microsoft.DSC.Debug/Echo |
| 92 | + result: |
| 93 | + actualState: |
| 94 | + output: Hello, Mikey! The time is 09:30. |
| 95 | +messages: [] |
| 96 | +hadErrors: false |
| 97 | +``` |
| 98 | + |
| 99 | +## Parameters |
| 100 | + |
| 101 | +### formatString |
| 102 | + |
| 103 | +The `format()` function requires a template string that includes placeholders for argument values. |
| 104 | +Placeholders use the format `{n}`, where `n` is the zero-based index of the argument to insert. |
| 105 | + |
| 106 | +```yaml |
| 107 | +Type: string |
| 108 | +Required: true |
| 109 | +MinimumCount: 1 |
| 110 | +MaximumCount: 1 |
| 111 | +``` |
| 112 | + |
| 113 | +### arguments |
| 114 | + |
| 115 | +The function accepts one or more arguments of any type to insert into the formatted string. |
| 116 | + |
| 117 | +```yaml |
| 118 | +Type: any |
| 119 | +Required: true |
| 120 | +MinimumCount: 1 |
| 121 | +MaximumCount: unlimited |
| 122 | +``` |
| 123 | + |
| 124 | +## Output |
| 125 | + |
| 126 | +The `format()` function returns a string where each placeholder in the **formatString** is replaced |
| 127 | +with the corresponding argument value. |
| 128 | + |
| 129 | +```yaml |
| 130 | +Type: string |
| 131 | +``` |
0 commit comments