Powershell ConvertTo-Json键小写

时间:2018-04-29 14:12:41

标签: json powershell lowercase

运行此代码

get-service | Select-Object -Property displayname, status, name | convertTo-Json

结果例如在这样的输出中:

{
    "DisplayName":  "Adobe Acrobat Update Service",
    "Status":  4,
    "Name":  "AdobeARMservice"
},
{
    "DisplayName":  "Adobe Flash Player Update Service",
    "Status":  1,
    "Name":  "AdobeFlashPlayerUpdateSvc"
},

是否可以以小写形式返回键?

2 个答案:

答案 0 :(得分:5)

您可以使用计算属性:

get-service | Select-Object -Property @{n='displayname';e={$_.displayname.tolower()}}, status, name | convertTo-Json

这样做会改变输出格式的方式

PS。你可以重复所有属性 PPS。 https://blogs.technet.microsoft.com/josebda/2014/04/19/powershell-tips-for-building-objects-with-custom-properties-and-special-formatting/

答案 1 :(得分:1)

您可以使用正则表达式来解决要求:

#Create a variable with the info
$json = Get-Service | Select-Object -Property displayname, status, name | convertTo-Json
#Use RegEx
[regex]::Replace(
    $json,
    '(?<=")(\w+)(?=":)',
    {$args[0].Groups[1].Value.ToLower()}
)

输出如下:

[
    {
        "displayname":  "Application Identity",
        "status":  1,
        "name":  "AppIDSvc"
    },
    {
        "displayname":  "Application Information",
        "status":  4,
        "name":  "Appinfo"
    },