使用Invoke-RestMethod自定义对象

时间:2019-06-06 13:11:00

标签: json rest azure-monitoring powershell-core

我正在使用Invoke-RestMethod(通过PowerShell 6.2.1)查询Azure Monitor Alerts:

nextLink value
-------- -----
         {@{properties=; id=/subscriptions/MySubscriptionID/providers/Microsoft.AlertsManagement/alerts/97fb8c3e-1e5b-4df5-9daa-46d8b115572f; type=Microsoft.Alert…

当前我在Azure中有2个打开的CPU警报:

enter image description here

运行cmdlet时,我可以看到返回的自定义对象:

targetResourceName :
targetResourceType :
alertState         :
severity           :
name               : Percentage CPU greater than 95

targetResourceName :
targetResourceType :
alertState         :
severity           :
name               : Percentage CPU greater than 95

但是,当我尝试为有效负载中的每个警报打印特定对象时,我得到的唯一值是 name 自定义对象:

{
  "nextLink": "",
  "value": [
    {
      "properties": {
        "essentials": {
          "severity": "Sev3",
          "signalType": "Metric",
          "alertState": "New",
          "monitorCondition": "Fired",
          "monitorService": "Platform",
          "targetResource": "/subscriptions/MySubscriptionID/resourcegroups/MyResourceGroup/providers/microsoft.compute/virtualmachines/myshobtestvm2",
          "targetResourceName": "myshobtestvm2",
          "targetResourceGroup": "MyResourceGroup",
          "targetResourceType": "virtualmachines",
          "sourceCreatedId": "MySubscriptionID_MyResourceGroup_microsoft.insights_metricAlerts_Percentage CPU greater than 95_-847848048",
          "smartGroupId": "9f422583-bbd8-4b71-a2fa-4e7651b64ada",
          "smartGroupingReason": "Similar to other alerts in the group",
          "alertRule": "/subscriptions/MySubscriptionID/resourceGroups/MyResourceGroup/providers/microsoft.insights/metricAlerts/Percentage CPU greater than 95",
          "startDateTime": "2019-06-06T11:49:31.0380889Z",
          "lastModifiedDateTime": "2019-06-06T11:49:31.0380889Z",
          "lastModifiedUserName": "System",
          "actionStatus": {
            "isSuppressed": false
          },
          "description": "{\n\"text test\":\"#alertrulename fired with #searchresultcount over threshold of #thresholdvalue.\"\n\n}"
        }
      },
      "id": "/subscriptions/MySubscriptionID/providers/Microsoft.AlertsManagement/alerts/97fb8c3e-1e5b-4df5-9daa-46d8b115572f",
      "type": "Microsoft.AlertsManagement/alerts",
      "name": "Percentage CPU greater than 95"
    },
    {
      "properties": {
        "essentials": {
          "severity": "Sev3",
          "signalType": "Metric",
          "alertState": "New",
          "monitorCondition": "Fired",
          "monitorService": "Platform",
          "targetResource": "/subscriptions/MySubscriptionID/resourcegroups/MyResourceGroup/providers/microsoft.compute/virtualmachines/myshobtestvm",
          "targetResourceName": "myshobtestvm",
          "targetResourceGroup": "MyResourceGroup",
          "targetResourceType": "virtualmachines",
          "sourceCreatedId": "MySubscriptionID_MyResourceGroup_microsoft.insights_metricAlerts_Percentage CPU greater than 95_1621321698",
          "smartGroupId": "9f422583-bbd8-4b71-a2fa-4e7651b64ada",
          "alertRule": "/subscriptions/MySubscriptionID/resourceGroups/MyResourceGroup/providers/microsoft.insights/metricAlerts/Percentage CPU greater than 95",
          "startDateTime": "2019-06-06T11:17:35.7461221Z",
          "lastModifiedDateTime": "2019-06-06T11:17:35.7461221Z",
          "lastModifiedUserName": "System",
          "actionStatus": {
            "isSuppressed": false
          },
          "description": "{\n\"text test\":\"#alertrulename fired with #searchresultcount over threshold of #thresholdvalue.\"\n\n}"
        }
      },
      "id": "/subscriptions/MySubscriptionID/providers/Microsoft.AlertsManagement/alerts/b929895f-c7c8-45c2-a6a7-1afe0048b30f",
      "type": "Microsoft.AlertsManagement/alerts",
      "name": "Percentage CPU greater than 95"
    }
  ]
}

这是一个阶段,因为如果我将输出转换回json,我将看到两个警报的所有对象值:

{{1}}

0 个答案:

没有答案