gpt4 book ai didi

powershell - ConvertTo-Json如何保持哈希表字段的顺序

转载 作者:行者123 更新时间:2023-12-03 00:56:55 26 4
gpt4 key购买 nike

我正在运行以下PowerShell代码,我需要保持原始哈希表键的顺序。

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

函数返回
{
"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
}

是否有任何选项可以保持原始哈希表键的顺序?

最佳答案

您可以通过在@符号前面添加[ordered]来创建有序哈希,如下所示:

[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
}
}

关于powershell - ConvertTo-Json如何保持哈希表字段的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61804610/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com