将root添加到ConvertTo-Json输出

时间:2015-08-03 18:38:32

标签: json powershell

如何为root返回的数组添加Get-EventLog命名空间?

现在输出如下:

[
    {
        "MachineName":  "xxx",
        "Data":  [

                 ],
        "Index":  555606,
        "Category":  "(0)",
        "CategoryNumber":  0,
        "EventID":  1022,
        "EntryType":  1,
        "Message":  "some error",
        "Source":  ".NET Runtime",
        "ReplacementStrings":  [
                                   "some error"
                               ],
        "InstanceId":  1022,
        "TimeGenerated":  "\/Date(1438626271000)\/",
        "TimeWritten":  "\/Date(1438626271000)\/",
        "UserName":  "ccc",
        "Site":  null,
        "Container":  null
    },
    {
        "MachineName":  "xxx",
        "Data":  [

                 ],
        "Index":  555603,
        "Category":  "(0)",
        "CategoryNumber":  0,
        "EventID":  1022,
        "EntryType":  1,
        "Message":  ".some error",
        "Source":  ".NET Runtime",
        "ReplacementStrings":  [
                                   "some error"
                               ],
        "InstanceId":  1022,
        "TimeGenerated":  "\/Date(1438626101000)\/",
        "TimeWritten":  "\/Date(1438626101000)\/",
        "UserName":  "ccc",
        "Site":  null,
        "Container":  null
    }
]

这就是我想得到的结果:

[
    EventlogEntry:
    [
        {
            "MachineName":  "xxx",
            "Data":  [

                     ],
            "Index":  555606,
            "Category":  "(0)",
            "CategoryNumber":  0,
            "EventID":  1022,
            "EntryType":  1,
            "Message":  "some error",
            "Source":  ".NET Runtime",
            "ReplacementStrings":  [
                                       "some error"
                                   ],
            "InstanceId":  1022,
            "TimeGenerated":  "\/Date(1438626271000)\/",
            "TimeWritten":  "\/Date(1438626271000)\/",
            "UserName":  "ccc",
            "Site":  null,
            "Container":  null
        },
        {
            "MachineName":  "xxx",
            "Data":  [

                     ],
            "Index":  555603,
            "Category":  "(0)",
            "CategoryNumber":  0,
            "EventID":  1022,
            "EntryType":  1,
            "Message":  ".some error",
            "Source":  ".NET Runtime",
            "ReplacementStrings":  [
                                       "some error"
                                   ],
            "InstanceId":  1022,
            "TimeGenerated":  "\/Date(1438626101000)\/",
            "TimeWritten":  "\/Date(1438626101000)\/",
            "UserName":  "ccc",
            "Site":  null,
            "Container":  null
        }
    ]
]

如何以所需方式格式化输出?

1 个答案:

答案 0 :(得分:1)

不确定这是否是您正在寻找的东西,但也许可以这样做。

ConvertTo-Json @{EventLogEntry = (Get-EventLog -LogName Application -ComputerName "xxx" -Newest 2 -EntryType Error)}

这将创建一个输出:

{
    EventLogEntry: [
        { ... },
    ]
}