json - 将 root 添加到 ConvertTo-Json 输出

标签 json powershell

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

现在的输出如下所示:

[
    {
        "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
        }
    ]
]

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

最佳答案

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

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

这将创建如下输出:

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

关于json - 将 root 添加到 ConvertTo-Json 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31794456/

相关文章:

javascript - 使用 angularjs 从 json 文件中删除项目

json - 如何在 bash 中将 JSON 子元素字符串转换为真正的 JSON 元素

ruby-on-rails - 计算数组中的项目跨越数千条记录的 100 条

powershell - 无法在同一个 PowerShell session 中运行同一个脚本两次

javascript - 类似 Python 的完整 Javascript 对象的 pickle

cJSON内存泄漏

regex - PowerShell -replace 获取两个不同字符之间的字符串

function - 另一个PowerShell返回问题

电源外壳 |从 CSV 文件名列表中复制项目,登录到同一 CSV

powershell - GNU Make 和 Windows Powershell : "Remove-Item -ErrorAction Ignore" does not work