Question:
I am running the following PowerShell code, I need to keep the order of the original hash table keys.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
function New-GetServiceConnectionTask( $serviceConnectionId) { @{ environment = @{ } taskId = '0a9fdc5e-3f3a-4d3d-9f63-a4f007f9a1fe' version = "1.*" name = 'Get Service Endpoint Credentials' refName = '' enabled = $true alwaysRun = $false continueOnError = $false timeoutInMinutes = 0 definitionType = 'task' overrideInputs = @{ } condition = 'succeeded()' inputs = @{ connectedServiceNameARM = $serviceConnectionId } } } New-GetServiceConnectionTask xxx | ConvertTo-Json -Depth 99 |
The function returns
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
{ "version": "1.*", "refName": "", "definitionType": "task", "overrideInputs": {}, "name": "Get Service Endpoint Credentials", "environment": {}, "inputs": { "connectedServiceNameARM": "xxx" }, "timeoutInMinutes": 0, "taskId": "0a9fdc5e-3f3a-4d3d-9f63-a4f007f9a1fe", "enabled": true, "condition": "succeeded()", "continueOnError": false, "alwaysRun": false } |
Is there any option to keep the order of the original hash table keys?
Answer:
You can create an ordered hash by including [ordered] in front of the @ symbol like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[ordered]@{ environment = @{ } taskId = '0a9fdc5e-3f3a-4d3d-9f63-a4f007f9a1fe' version = "1.*" name = 'Get Service Endpoint Credentials' refName = '' enabled = $true alwaysRun = $false continueOnError = $false timeoutInMinutes = 0 definitionType = 'task' overrideInputs = @{ } condition = 'succeeded()' inputs = @{ connectedServiceNameARM = $serviceConnectionId } } |