|
| 1 | +--- |
| 2 | +description: Reference for the 'string' DSC configuration document function |
| 3 | +ms.date: 08/09/2025 |
| 4 | +ms.topic: reference |
| 5 | +title: string |
| 6 | +--- |
| 7 | + |
| 8 | +# string |
| 9 | + |
| 10 | +## Synopsis |
| 11 | + |
| 12 | +Converts a value to a string representation. |
| 13 | + |
| 14 | +## Syntax |
| 15 | + |
| 16 | +```Syntax |
| 17 | +string(<value>) |
| 18 | +``` |
| 19 | + |
| 20 | +## Description |
| 21 | + |
| 22 | +The `string()` function converts a value of any type to its string |
| 23 | +representation. This is useful for formatting output, concatenating values, or |
| 24 | +ensuring consistent data types. Arrays and objects are converted to JSON |
| 25 | +strings, while primitive types are converted to their standard string |
| 26 | +representations. |
| 27 | + |
| 28 | +## Examples |
| 29 | + |
| 30 | +### Example 1 - Convert integers to strings |
| 31 | + |
| 32 | +The following example shows how to convert numbers to strings for display |
| 33 | +purposes. |
| 34 | + |
| 35 | +```yaml |
| 36 | +# string.example.1.dsc.config.yaml |
| 37 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 38 | +parameters: |
| 39 | + serverCount: |
| 40 | + type: int |
| 41 | + defaultValue: 42 |
| 42 | + memorySize: |
| 43 | + type: int |
| 44 | + defaultValue: 16 |
| 45 | +resources: |
| 46 | +- name: Convert numbers |
| 47 | + type: Microsoft.DSC.Debug/Echo |
| 48 | + properties: |
| 49 | + output: |
| 50 | + serverCountString: "[string(parameters('serverCount'))]" |
| 51 | + memorySizeString: "[string(parameters('memorySize'))]" |
| 52 | + literalNumber: "[string(123)]" |
| 53 | +``` |
| 54 | +
|
| 55 | +```bash |
| 56 | +dsc config get --file string.example.1.dsc.config.yaml |
| 57 | +``` |
| 58 | + |
| 59 | +```yaml |
| 60 | +results: |
| 61 | +- name: Convert numbers |
| 62 | + type: Microsoft.DSC.Debug/Echo |
| 63 | + result: |
| 64 | + actualState: |
| 65 | + output: |
| 66 | + serverCountString: '42' |
| 67 | + memorySizeString: '16' |
| 68 | + literalNumber: '123' |
| 69 | +messages: [] |
| 70 | +hadErrors: false |
| 71 | +``` |
| 72 | +
|
| 73 | +### Example 2 - Convert arrays and objects to JSON strings |
| 74 | +
|
| 75 | +The following example shows how arrays and objects are converted to JSON |
| 76 | +strings. |
| 77 | +
|
| 78 | +```yaml |
| 79 | +# string.example.2.dsc.config.yaml |
| 80 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 81 | +parameters: |
| 82 | + serverList: |
| 83 | + type: array |
| 84 | + defaultValue: |
| 85 | + - web01 |
| 86 | + - web02 |
| 87 | + - db01 |
| 88 | + config: |
| 89 | + type: object |
| 90 | + defaultValue: |
| 91 | + timeout: 30 |
| 92 | + retries: 3 |
| 93 | + enabled: true |
| 94 | +resources: |
| 95 | +- name: Convert collections |
| 96 | + type: Microsoft.DSC.Debug/Echo |
| 97 | + properties: |
| 98 | + output: |
| 99 | + serversJson: "[string(parameters('serverList'))]" |
| 100 | + configJson: "[string(parameters('config'))]" |
| 101 | + arrayLiteral: "[string(createArray('a', 'b'))]" |
| 102 | +``` |
| 103 | +
|
| 104 | +```bash |
| 105 | +dsc config get --file string.example.3.dsc.config.yaml |
| 106 | +``` |
| 107 | + |
| 108 | +```yaml |
| 109 | +results: |
| 110 | +- metadata: |
| 111 | + Microsoft.DSC: |
| 112 | + duration: PT0.1452881S |
| 113 | + name: Convert collections |
| 114 | + type: Microsoft.DSC.Debug/Echo |
| 115 | + result: |
| 116 | + actualState: |
| 117 | + output: |
| 118 | + serversJson: '["web01","web02","db01"]' |
| 119 | + configJson: '{"timeout":30,"retries":3,"enabled":true}' |
| 120 | + arrayLiteral: '["a","b"]' |
| 121 | +messages: [] |
| 122 | +hadErrors: false |
| 123 | +``` |
| 124 | +
|
| 125 | +### Example 3 - Building formatted messages |
| 126 | +
|
| 127 | +The following example shows a practical use case for building formatted |
| 128 | +messages using string conversion. |
| 129 | +
|
| 130 | +```yaml |
| 131 | +# string.example.3.dsc.config.yaml |
| 132 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 133 | +parameters: |
| 134 | + deploymentId: |
| 135 | + type: int |
| 136 | + defaultValue: 12345 |
| 137 | + isProduction: |
| 138 | + type: bool |
| 139 | + defaultValue: false |
| 140 | + serverCount: |
| 141 | + type: int |
| 142 | + defaultValue: 3 |
| 143 | +resources: |
| 144 | +- name: Build status message |
| 145 | + type: Microsoft.DSC.Debug/Echo |
| 146 | + properties: |
| 147 | + output: |
| 148 | + deploymentInfo: "[concat('Deployment ', string(parameters('deploymentId')), ' running in ', if(parameters('isProduction'), 'production', 'development'), ' mode')]" |
| 149 | + serverMessage: "[concat('Managing ', string(parameters('serverCount')), ' server(s)')]" |
| 150 | + statusFlag: "[concat('Production: ', string(parameters('isProduction')))]" |
| 151 | +``` |
| 152 | +
|
| 153 | +```bash |
| 154 | +dsc config get --file string.example.4.dsc.config.yaml |
| 155 | +``` |
| 156 | + |
| 157 | +```yaml |
| 158 | +results: |
| 159 | +- name: Build status message |
| 160 | + type: Microsoft.DSC.Debug/Echo |
| 161 | + result: |
| 162 | + actualState: |
| 163 | + output: |
| 164 | + deploymentInfo: Deployment 12345 running in development mode |
| 165 | + serverMessage: Managing 3 server(s) |
| 166 | + statusFlag: 'Production: false' |
| 167 | +messages: [] |
| 168 | +hadErrors: false |
| 169 | +``` |
| 170 | +
|
| 171 | +### Example 4 - String conversion for logging |
| 172 | +
|
| 173 | +The following example demonstrates converting various data types for logging |
| 174 | +purposes. |
| 175 | +
|
| 176 | +```yaml |
| 177 | +# string.example.4.dsc.config.yaml |
| 178 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 179 | +parameters: |
| 180 | + timestamp: |
| 181 | + type: int |
| 182 | + defaultValue: 1691596800 |
| 183 | + errorCode: |
| 184 | + type: int |
| 185 | + defaultValue: 404 |
| 186 | + metadata: |
| 187 | + type: object |
| 188 | + defaultValue: |
| 189 | + source: "api" |
| 190 | + level: "error" |
| 191 | +resources: |
| 192 | +- name: Generate log entry |
| 193 | + type: Microsoft.DSC.Debug/Echo |
| 194 | + properties: |
| 195 | + output: |
| 196 | + logEntry: "[concat('[', string(parameters('timestamp')), '] ERROR ', string(parameters('errorCode')), ': ', string(parameters('metadata')))]" |
| 197 | +``` |
| 198 | +
|
| 199 | +```bash |
| 200 | +dsc config get --file string.example.5.dsc.config.yaml |
| 201 | +``` |
| 202 | + |
| 203 | +```yaml |
| 204 | +results: |
| 205 | +- name: Generate log entry |
| 206 | + type: Microsoft.DSC.Debug/Echo |
| 207 | + result: |
| 208 | + actualState: |
| 209 | + output: |
| 210 | + logEntry: '[1691596800] ERROR 404: {"level":"error","source":"api"}' |
| 211 | +messages: [] |
| 212 | +hadErrors: false |
| 213 | +``` |
| 214 | +
|
| 215 | +## Parameters |
| 216 | +
|
| 217 | +### value |
| 218 | +
|
| 219 | +The value to convert to a string. |
| 220 | +
|
| 221 | +```yaml |
| 222 | +Type: [string, number, bool, null, array, object] |
| 223 | +Required: true |
| 224 | +``` |
| 225 | +
|
| 226 | +The `string()` function accepts exactly one input value of any type. The |
| 227 | +conversion behavior depends on the input type: |
| 228 | + |
| 229 | +- **String**: Returns the string unchanged |
| 230 | +- **Number**: Converts to decimal string representation |
| 231 | +- **Boolean**: Converts to "true" or "false" |
| 232 | +- **Null**: Converts to "null" |
| 233 | +- **Array**: Converts to JSON array string |
| 234 | +- **Object**: Converts to JSON object string |
| 235 | + |
| 236 | +## Output |
| 237 | + |
| 238 | +The `string()` function returns the string representation of the input value. |
| 239 | + |
| 240 | +```yaml |
| 241 | +Type: string |
| 242 | +``` |
| 243 | + |
| 244 | +## Related functions |
| 245 | + |
| 246 | +- [`concat()`][00] - Concatenates strings together |
| 247 | +- [`int()`][01] - Converts a string to an integer |
| 248 | +- [`bool()`][02] - Converts a string to a boolean |
| 249 | +- [`base64()`][03] - Encodes a string to Base64 format |
| 250 | +- [`length()`][04] - Returns the length of a string |
| 251 | + |
| 252 | +<!-- Link reference definitions --> |
| 253 | +[00]: ./concat.md |
| 254 | +[01]: ./int.md |
| 255 | +[02]: ./bool.md |
| 256 | +[03]: ./base64.md |
| 257 | +[04]: ./length.md |
0 commit comments