我创建了三个对象,其中包含已解析的 Json 文件中的数据。
#Get list of synthetic monitors by name
$executedMonitors = $json.result.data.dimensionMap."dt.entity.synthetic_test.name"
Write-Output $executedMonitors
#Get list of HTTP monitors by name
$httpExecutedMonitors = $json.result.data.dimensionMap."dt.entity.http_check.name"
Write-Output $httpExecutedMonitors
#Get list of synthetic & http monitors values
$Values = @($json.result.data.values)
Log "Values of executed synthetics: ($Values)"
起初,我只使用对象 $execulatedMonitors 和 $Values 并将所有值添加到自定义 ps 对象中,如下所示,然后格式化为 HTML 表。
$i = 0
$report =
if ($null -ne $executedMonitors) {
foreach ($executedMonitor in $executedMonitors) {
[pscustomobject] @{
Application = $executedMonitor
Status = $Values[$i++].passFail
}
}
}
现在,我想将 $httpExecutedMonitors 中的附加值添加到 $report 对象“Application”属性中,但如果不将多个附加值打印到 HTML 表中,则无法执行此操作。
我尝试通过创建数组并尝试添加新对象来将附加值添加到“应用程序”属性:
$i = 0
$report =@()
$report +=
if ($null -ne $executedMonitors) {
foreach ($executedMonitor in $executedMonitors) {
[pscustomobject] @{
Application = $executedMonitor
Status = $Values[$i++].passFail
}
}
}
#testing
$report +=
foreach ($httpmonitor in $httpExecutedMonitors) {
New-Object -TypeName psobject -Property @{Application = $httpmonitor}
}
以及创建两个单独的 psobject,然后尝试组合。
包含首次创建的对象的示例 json 文件:
{
"totalCount": 168,
"nextPageKey": null,
"resolution": "Inf",
"result": [
{
"metricId": "(builtin:synthetic.browser.success:filter(and(or(in(\"dt.entity.synthetic_test\",entitySelector(\"type(synthetic_test),tag(~\"API~\")\"))))):splitBy(\"dt.entity.synthetic_test\"):names:sort(dimension(\"dt.entity.synthetic_test.name\",ascending))):names",
"dataPointCountRatio": 2.004E-4,
"dimensionCountRatio": 0.00167,
"data": [
{
"dimensions": [
"ADR Timeliness Calculator",
"SYNTHETIC_TEST-1E31363E09760539"
],
"dimensionMap": {
"dt.entity.synthetic_test": "SYNTHETIC_TEST-1E31363E09760539",
"dt.entity.synthetic_test.name": "ADR Timeliness Calculator"
},
"timestamps": [
1711512000000
],
"values": [
2
]
}
]
},
{
"metricId": "(builtin:synthetic.http.resultStatus:filter(and(or(in(\"dt.entity.http_check\",entitySelector(\"type(http_check),tag(~\"API~\")\"))))):splitBy(\"dt.entity.http_check\"):names:sort(dimension(\"dt.entity.http_check.name\",ascending)):limit(20)):names",
"dataPointCountRatio": 1.2E-6,
"dimensionCountRatio": 1.0E-5,
"data": [
{
"dimensions": [
"SNAP",
"HTTP_CHECK-BD16645521BF003A"
],
"dimensionMap": {
"dt.entity.http_check.name": "SNAP",
"dt.entity.http_check": "HTTP_CHECK-BD16645521BF003A"
},
"timestamps": [
1711512000000
],
"values": [
1
]
}
]
}
]
}
我不确定是否需要更改对象 $execulatedMonitors & $httpExecutedMonitors & $Values 如何使用 Json 中的附加 metricID 数据进行配置,或者是否可以调整我的 pscustomobject。
你从哪里得到
.PassFail
?
也就是说:
$result=foreach ($Data in $Json.result.data) {
$Application = ($Data.dimensionMap.'dt.entity.synthetic_test') ? $Data.dimensionMap.'dt.entity.synthetic_test' : $Data.dimensionMap.'dt.entity.http_check.name'
if ($Application) {
[pscustomobject]@{
Application = $Application
Status = $Data.Values[0]
}
}
}
$result