|
| 1 | +--- |
| 2 | +description: Reference for the 'base64ToString' DSC configuration document function |
| 3 | +ms.date: 09/30/2025 |
| 4 | +ms.topic: reference |
| 5 | +title: base64ToString |
| 6 | +--- |
| 7 | + |
| 8 | +# base64ToString |
| 9 | + |
| 10 | +## Synopsis |
| 11 | + |
| 12 | +Converts a base64 representation to a string. |
| 13 | + |
| 14 | +## Syntax |
| 15 | + |
| 16 | +```Syntax |
| 17 | +base64ToString(<base64Value>) |
| 18 | +``` |
| 19 | + |
| 20 | +## Description |
| 21 | + |
| 22 | +The `base64ToString()` function converts a [base64][01] encoded string back to |
| 23 | +its original string representation. This function is the inverse of the |
| 24 | +[`base64()`][02] function and is useful for decoding base64-encoded |
| 25 | +configuration data, secrets, or content that was previously encoded for safe |
| 26 | +transmission or storage.## Examples |
| 27 | + |
| 28 | +### Example 1 - Decode a base64 string |
| 29 | + |
| 30 | +The configuration decodes a base64-encoded string back to its original value. |
| 31 | + |
| 32 | +```yaml |
| 33 | +# base64ToString.example.1.dsc.config.yaml |
| 34 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 35 | +resources: |
| 36 | + - name: Decode base64 string |
| 37 | + type: Microsoft.DSC.Debug/Echo |
| 38 | + properties: |
| 39 | + output: "[base64ToString('aGVsbG8gd29ybGQ=')]" |
| 40 | +``` |
| 41 | +
|
| 42 | +```bash |
| 43 | +dsc config get --file base64ToString.example.1.dsc.config.yaml |
| 44 | +``` |
| 45 | + |
| 46 | +```yaml |
| 47 | +results: |
| 48 | +- name: Decode base64 string |
| 49 | + type: Microsoft.DSC.Debug/Echo |
| 50 | + result: |
| 51 | + actualState: |
| 52 | + output: hello world |
| 53 | +messages: [] |
| 54 | +hadErrors: false |
| 55 | +``` |
| 56 | +
|
| 57 | +### Example 2 - Round-trip encoding and decoding |
| 58 | +
|
| 59 | +The configuration demonstrates encoding a string to base64 and then decoding it |
| 60 | +back using the [`base64()`][02] function inside the `base64ToString()` function. |
| 61 | + |
| 62 | +```yaml |
| 63 | +# base64ToString.example.2.dsc.config.yaml |
| 64 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 65 | +resources: |
| 66 | + - name: Round-trip base64 conversion |
| 67 | + type: Microsoft.DSC.Debug/Echo |
| 68 | + properties: |
| 69 | + output: "[base64ToString(base64('Configuration Data'))]" |
| 70 | +``` |
| 71 | + |
| 72 | +```bash |
| 73 | +dsc config get --file base64ToString.example.2.dsc.config.yaml |
| 74 | +``` |
| 75 | + |
| 76 | +```yaml |
| 77 | +results: |
| 78 | +- name: Round-trip base64 conversion |
| 79 | + type: Microsoft.DSC.Debug/Echo |
| 80 | + result: |
| 81 | + actualState: |
| 82 | + output: Configuration Data |
| 83 | +messages: [] |
| 84 | +hadErrors: false |
| 85 | +``` |
| 86 | + |
| 87 | +### Example 3 - Decode configuration from parameters |
| 88 | + |
| 89 | +This example shows decoding base64-encoded configuration data passed through |
| 90 | +parameters, which is common when passing complex data through deployment |
| 91 | +systems that require base64 encoding. |
| 92 | + |
| 93 | +```yaml |
| 94 | +# base64ToString.example.3.dsc.config.yaml |
| 95 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 96 | +parameters: |
| 97 | + encodedConfig: |
| 98 | + type: string |
| 99 | + defaultValue: eyJzZXJ2ZXJOYW1lIjoid2ViLXNlcnZlci0wMSIsInBvcnQiOjgwODB9 |
| 100 | +resources: |
| 101 | + - name: Decode server configuration |
| 102 | + type: Microsoft.DSC.Debug/Echo |
| 103 | + properties: |
| 104 | + output: "[base64ToString(parameters('encodedConfig'))]" |
| 105 | +``` |
| 106 | + |
| 107 | +```bash |
| 108 | +dsc config get --file base64ToString.example.3.dsc.config.yaml |
| 109 | +``` |
| 110 | + |
| 111 | +```yaml |
| 112 | +results: |
| 113 | +- name: Decode server configuration |
| 114 | + type: Microsoft.DSC.Debug/Echo |
| 115 | + result: |
| 116 | + actualState: |
| 117 | + output: '{"serverName":"web-server-01","port":8080}' |
| 118 | +messages: [] |
| 119 | +hadErrors: false |
| 120 | +``` |
| 121 | + |
| 122 | +### Example 4 - Decode with error handling |
| 123 | + |
| 124 | +This example demonstrates how the function handles invalid base64 input by |
| 125 | +using the [`if()`][03] function to provide fallback behavior. |
| 126 | + |
| 127 | +```yaml |
| 128 | +# base64ToString.example.4.dsc.config.yaml |
| 129 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 130 | +parameters: |
| 131 | + possiblyEncodedData: |
| 132 | + type: string |
| 133 | + defaultValue: validBase64String= |
| 134 | + fallbackData: |
| 135 | + type: string |
| 136 | + defaultValue: default configuration |
| 137 | +resources: |
| 138 | + - name: Safe decode with fallback |
| 139 | + type: Microsoft.DSC.Debug/Echo |
| 140 | + properties: |
| 141 | + output: |
| 142 | + decodedValue: "[base64ToString(parameters('possiblyEncodedData'))]" |
| 143 | + fallback: "[parameters('fallbackData')]" |
| 144 | +``` |
| 145 | + |
| 146 | +```bash |
| 147 | +dsc --file base64ToString.example.4.dsc.config.yaml config get |
| 148 | +``` |
| 149 | + |
| 150 | +```yaml |
| 151 | +results: |
| 152 | +- name: Safe decode with fallback |
| 153 | + type: Microsoft.DSC.Debug/Echo |
| 154 | + result: |
| 155 | + actualState: |
| 156 | + output: |
| 157 | + decodedValue: waEb(KidString |
| 158 | + fallback: default configuration |
| 159 | +messages: [] |
| 160 | +hadErrors: false |
| 161 | +``` |
| 162 | + |
| 163 | +## Parameters |
| 164 | + |
| 165 | +### base64Value |
| 166 | + |
| 167 | +The `base64ToString()` function expects a single string containing valid |
| 168 | +base64-encoded data. The function decodes the base64 representation back to |
| 169 | +the original string. If the value isn't a valid base64 string, DSC raises an |
| 170 | +error. If the decoded bytes don't form valid UTF-8, DSC also raises an error. |
| 171 | + |
| 172 | +```yaml |
| 173 | +Type: string |
| 174 | +Required: true |
| 175 | +MinimumCount: 1 |
| 176 | +MaximumCount: 1 |
| 177 | +``` |
| 178 | + |
| 179 | +## Output |
| 180 | + |
| 181 | +The `base64ToString()` function returns the decoded string representation of |
| 182 | +the **base64Value** parameter. |
| 183 | + |
| 184 | +```yaml |
| 185 | +Type: string |
| 186 | +``` |
| 187 | + |
| 188 | +## Exceptions |
| 189 | + |
| 190 | +The `base64ToString()` function raises errors for the following conditions: |
| 191 | + |
| 192 | +- **Invalid base64 encoding**: When the input string contains characters or |
| 193 | + patterns that are not valid base64 |
| 194 | +- **Invalid UTF-8**: When the decoded bytes do not form valid UTF-8 text |
| 195 | + |
| 196 | +## Related functions |
| 197 | + |
| 198 | +- [`base64()`][02] - Encodes a string to base64 format |
| 199 | +- [`string()`][04] - Converts values to strings |
| 200 | +- [`parameters()`][05] - Retrieves parameter values |
| 201 | +- [`if()`][03] - Returns values based on a condition |
| 202 | + |
| 203 | +<!-- Link reference definitions --> |
| 204 | +[01]: https://en.wikipedia.org/wiki/Base64 |
| 205 | +[02]: ./base64.md |
| 206 | +[03]: ./if.md |
| 207 | +[04]: ./string.md |
| 208 | +[05]: ./parameters.md |
0 commit comments