为必须重复具有相同NoteProperty名称的PSCustomobject更新JSON

时间:2018-11-23 23:34:58

标签: json powershell

曾经努力为具有相同NoteProperty名称重复的PSCustomobject更新JSON。这是为了通过Webhook自动更新多个Azure经典警报规则。

这是在同时具有Webhook和电子邮件动作的情况下,规则JSON中的Actions对象的外观。此示例是使用Azure门户UI创建的

"actions":  [
                                   {
                                       "$type":  "Microsoft.WindowsAzure.Management.Monitoring.Alerts.Models.RuleWebhookAction, Microsoft.WindowsAzure.Management.Mon.Client",
                                       "odata.type":  "Microsoft.Azure.Management.Insights.Models.RuleWebhookAction",
                                       "serviceUri":  "http://www.webhooktest.com",
                                       "properties":  null
                                   },
                                   {
                                       "$type":  "Microsoft.WindowsAzure.Management.Monitoring.Alerts.Models.RuleEmailAction, Microsoft.WindowsAzure.Management.Mon.Client",
                                       "odata.type":  "Microsoft.Azure.Management.Insights.Models.RuleEmailAction",
                                       "sendToServiceOwners":  false,
                                       "customEmails":  [
                                                            "email2@domain.com",
                                                            "email1@domain.com" ]
                                    }
          ]

默认情况下,仅启用电子邮件-请参阅以下示例。要添加一个Webhook,我必须添加另一个$ type和odata.type,它们是Note属性

"actions":  [
                                   {
                                       "$type":  "Microsoft.WindowsAzure.Management.Monitoring.Alerts.Models.RuleEmailAction, Microsoft.WindowsAzure.Management.Mon.Client",
                                       "odata.type":  "Microsoft.Azure.Management.Insights.Models.RuleEmailAction",
                                       "sendtoserviceowners":  true,
                                       "customEmails":  [
                                                            "email2@domain.com",
                                                            "email1@domain.com" 
                                                        ]
                                   }
                               ]

    TypeName: System.Management.Automation.PSCustomObject

Name                MemberType   Definition                                                                                                                           
----                ----------   ----------                                                                                                                           
Equals              Method       bool Equals(System.Object obj)                                                                                                       
GetHashCode         Method       int GetHashCode()                                                                                                                    
GetType             Method       type GetType()                                                                                                                       
ToString            Method       string ToString()                                                                                                                    
$type               NoteProperty string $type=Microsoft.WindowsAzure.Management.Monitoring.Alerts.Models.RuleEmailAction, Microsoft.WindowsAzure.Management.Mon.Client
customEmails        NoteProperty Object[] customEmails=System.Object[]                                                                                                
odata.type          NoteProperty string odata.type=Microsoft.Azure.Management.Insights.Models.RuleEmailAction                                                         
sendToServiceOwners NoteProperty bool sendToServiceOwners=False

该电子邮件操作的$ type和odata.type注释属性已经存在,因此添加另一个(如下)将失败,并且如果使用-Force,它将覆盖该电子邮件操作的$ type。

$action | Add-Member -NotePropertyName '$type' -NotePropertyValue "Microsoft.WindowsAzure.Management.Monitoring.Alerts.Models.RuleWebhookAction, Microsoft.WindowsAzure.Management.Mon.Client"

我一直在坚持如何在不覆盖电子邮件的情况下添加webhook操作。预先感谢!

0 个答案:

没有答案