【问题标题】:Create a json document based on existing document with powershell 5.1使用 powershell 5.1 基于现有文档创建 json 文档
【发布时间】:2020-06-25 13:17:18
【问题描述】:

我正在尝试构建一个 json 文档,该文档从现有的基本 json 文档中获取其结构、列名和一些值,并将其他值设置为在 foreach-object 循环中通过 powershell 5.1 分配的变量。

我尝试编辑基本 json 的副本并完全创建一个新的,我可以接近我想要的,但不完全是。

一个缩减的示例 json 文档是:

[
    {
        "name": "Application1",
        "tags": ["monitor"],
        "description": "Get information about application 1.",
        "query":
        [
            {                                                         
                "target": {                                                             
                    "version": ["v1","v2","v3","v4","v5"],
                    "platform": ["Windows","Linux"]
                },
                "implementation": {                                                      
                    "action": "do something"
                }
            }
        ]
    },
    {
        "name": "Application2",
        "tags": ["monitor"],
        "description": "Get information about application 2",
        "query":
        [
            {                                                     
                "target": {                                                             
                    "version": ["v1","v2","v3"],
                    "platform": ["Windows"]
                },
                "implementation": {                                                    
                    "action": "do something for v1, v2 and v3"
                }
            },
            {                                                     
                "target": {                                                             
                    "version": ["v4","v5"],
                    "platform": ["Windows"]
                },
                "implementation": {                                                    
                    "action": "do something for v4 and v5"
                }
            }
        ]
    }
]

我的脚本目前看起来像:

$BaseJson = (Get-Content "$SourcePath\probes.json" | ConvertFrom-Json)

$ClientConfig = @"
{
    "targetHost": "$DigitalSymphonyHost",
    "targetPort": "$DigitalSymphonyPort",
    "source": "$TargetSqlInstance",
    "sensor": {}
}
"@

$ClientJson = ConvertFrom-Json -InputObject $ClientConfig

$BaseJson | Where-Object {$_.tags -contains "monitor"} | ForEach-Object {
    # For each sensor in the base-sensors json document
    $SensorName = $_.name 
    $Severity = 2
    $Version = $_.query.target.version
    $Platform = $_.query.target.platform
    $Query = $_.query.implementation.action

    $Sensor =@"
                {
                    "severity": "$Severity",
                    "sensorId": "$SensorId",
                    "type": "$Type",
                    "target": {
                        "version": "$Version",
                        "platform": "$Platform",
                        "engineEdition": "$Edition"
                    },
                    "implementation": {
                        "query": "$Query"
                    }
                }
"@
    $ClientJson.sensor | Add-Member -Name $SensorName -Value (ConvertFrom-Json $Sensor) -MemberType NoteProperty
}

$ClientJson | ConvertTo-Json | Out-File "$DestinationPath\client-sensors.json"

我想要的结果是按照以下方式添加$SensorId

[
    {
        "targetHost":  "servername",
        "targetPort":  "port",
        "source":  "servername",
        "sensor":  [{
            "name": "Application1",
            "sensorId": "<value_from_$SensorId>",
            "tags": [],
            "description": "Get information about application 1.",
            "query":
            [
                {                                                         
                    "target": {                                                             
                        "version": ["v1","v2","v3","v4","v5"],
                        "platform": ["Windows","Linux"]
                    },
                    "implementation": {                                                      
                        "action": "do something"
                    }
                }
            ]
        },
        {
            "name": "Application2",
            "sensorId": "<value_from_$SensorId>",
            "tags": [],
            "description": "Get information about application 2",
            "query":
            [
                {                                                     
                    "target": {                                                             
                        "version": ["v1","v2","v3"],
                        "platform": ["Windows"]
                    },
                    "implementation": {                                                    
                        "action": "do something for v1, v2 and v3"
                    }
                },
                {                                                     
                    "target": {                                                             
                        "version": ["v4","v5"],
                        "platform": ["Windows"]
                    },
                    "implementation": {                                                    
                        "action": "do something for v4 and v5"
                    }
                }
            ]
        }]
    }
]

我目前的结果是

{
    "targetHost":  "servername",
    "targetPort":  "port",
    "source":  "server",
    "sensor":  {
                   "applicationname1":  {
                                    "tags": ["monitor],
                                    "description": "Get information about application 2",
                                    "sensorId":  "420",
                                    "target":  "@{version=v1,v2,v3,v4,v5; platform=Windows Windows;}",
                                    "action":  "do something for v4 and v5"
                                }
                            }
}

【问题讨论】:

  • 这是什么不做它应该做的?你想要的输出是什么?
  • 我在问题中添加了更多细节。

标签: json powershell


【解决方案1】:

我不确定你从哪里得到sensorID,但我会更像下面这样:

param(
    $SourcePath = $PSScriptRoot,
    $DestinationPath = $PSScriptRoot,
    $DigitalSymphonyHost = 'SomeHost',
    $DigitalSymphonyPort = '8765',
    $TargetSqlInstance
)

$BaseObj = (Get-Content "$SourcePath\probes.json" | ConvertFrom-Json)
$Client = [PSCustomObject] @{
    targetHost = $DigitalSymphonyHost
    targetPort = $DigitalSymphonyPort
    source = $TargetSqlInstance
    sensor = @()
}

$BaseObj | Where-Object {$_.tags -contains "monitor"} | ForEach-Object {
    $sensor = $_

    # TODO: How are you getting sensorId?
    $sensor | Add-Member -Name 'sensorId' -Value 777 -MemberType NoteProperty    
    $Client.sensor += $sensor 
}

ConvertTo-Json @($Client) -Depth 10 | Out-File "$DestinationPath\client-sensors.json" -Force

这将为您提供您正在寻找的 JSON。

【讨论】:

    猜你喜欢
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2018-04-30
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    相关资源
    最近更新 更多